如何解决量角器找不到h1元素
我正在用量角器创建我的第一个黄瓜测试。当浏览器启动并导航到/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')
})
},
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。