量角器找不到 h1 元素

Protractor not finding h1 element

我正在用量角器创建我的第一个 Cucumber 测试。当浏览器启动并导航到 /login 时,它应该会找到 h1 标记并获取文本。这似乎不起作用。在视觉上,浏览器启动并转到登录页面,但它似乎甚至在加载该路由的组件之前就关闭了。然后我收到以下错误:

1) Scenario: Login Page # e2e/src/features/login-page.feature:3
   ✔ Before # e2e/src/steps/login-page.steps.ts:9
   ✔ Given I am on the login page # e2e/src/steps/login-page.steps.ts:13
   ✖ Then I should see the login title # e2e/src/steps/login-page.steps.ts:17
       NoSuchElementError: No element found using locator: By(css selector, h1)

我的功能是这样的:

Feature: Go to the login page
  @Regression
  Scenario: Login Page
    Given I am on the login page
    Then I should see the login title

我的步骤是这样的:

import { expect } from 'chai';
import { Before, Given, Then } from 'cucumber';
import { browser, by, element } from 'protractor';
import { LoginPage } from '../pages/login-page.po';

Given('I am on the login page', async () => {
  await browser.get('/login');
});

Then('I should see the login title', async () => {
  expect(await element(by.css('h1')).getText()).to.equal('Login');
});

如果仅通过 <tag> 定位元素,您应该使用 by.tagName() 定位器

expect(await element(by.tagName('h1')).getText()).to.equal('Login');

我的修复似乎是修改 onPrepare 来做一些额外的等待(这是我在旧的 github comment 中找到的)

  onPrepare() {
    browser.manage().timeouts().pageLoadTimeout(40000)
    browser.manage().timeouts().implicitlyWait(25000)
    // If you need to navigate to a page which does not use Angular,
    // you can turn off waiting for Angular
    browser.ignoreSynchronization = true
    browser.get('https://localhost:4200')
    browser.waitForAngular()
    require('ts-node').register({
      project: path.join(__dirname, './tsconfig.e2e.json')
    })
  },