javascript - 如何使用 webdriver.io 点击网页上的所有锚元素
问题描述
我正在尝试浏览此 url - https://gallant-meitner-86c223.netlify.com/#/,检查a[href]
页面上链接的数量,单击它们并验证此人是否存在
wdio.conf.js
exports.config = {
runner: 'local',
specs: [
'test/e2e/**/*.js'
],
maxInstances: 10,
capabilities: [{
maxInstances: 5,
browserName: 'firefox'
}],
logLevel: 'info',
bail: 0,
baseUrl: 'http://localhost:8080',
waitforTimeout: 10000,
connectionRetryTimeout: 90000,
connectionRetryCount: 3,
services: ['selenium-standalone'],
framework: 'jasmine',
reporters: ['spec', 'junit'],
jasmineNodeOpts: {
defaultTimeoutInterval: 60000,
}
我的测试
describe('Home', () => {
it('should get count of the links and click on them and verify the person exists', async () => {
await browser.url('/')
const title = await browser.getTitle()
expect(title).toBe('Force Vue Awakens')
// This doesn't work as well
const links = $$('#links a')
const count = links.length
expect(count).toBe(10)
links.forEach((link) => {
link.click()
// Dont know how to verify each person after clicking the link
})
})
})
我是自动化测试和网络驱动程序的新手。任何帮助或小提琴将不胜感激!
解决方案
forEach()
方法不适用于返回 Promise 的函数。您可以用于它的for ... of
建设。还要记住,如果你webdriver-io
在async
模式下使用,你应该总是解析返回 Promise 的方法。看一下重构测试:
describe('Home', () => {
it('should get count of the links and click on them and verify the person exists', async () => {
await browser.url('/')
const title = await browser.getTitle()
expect(title).toBe('Force Vue Awakens')
const links = await $$('#links a') //here you missed await
const count = links.length
expect(count).toBe(10)
const beforeUrl = await browser.getUrl()
for (let link of links) {
await link.click();
const afterUrl = await browser.getUrl()
expect(afterUrl).not.Equal(beforeUrl) // here use your assertion library
}
})
})
推荐阅读
- angular - Angular向子模块注入不同的服务实现
- python - 重命名同一文件夹中的图像(新名称+图像大小)时出现问题
- django - Django 不断在虚拟环境中制作额外的迁移文件
- django - 如何在 django rest 框架中解析 JSON
- angular - 角primeng fullcalendar句柄更改事件日期
- eclipse - Eclipse 2019 12 Runtime Environment 缺少 Tomcat 9.0
- javascript - 在没有任何库的情况下更改 node.js 上的 .png 图像
- java - 如何在java中从其他类初始化静态最终变量
- angular - Angular 8和组件之间的路由而不会丢失状态
- python - python请求给