首页 > 解决方案 > 如何使用 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
    })

  })
})

我是自动化测试和网络驱动程序的新手。任何帮助或小提琴将不胜感激!

标签: javascriptautomated-testswebdriver-io

解决方案


forEach()方法不适用于返回 Promise 的函数。您可以用于它的for ... of建设。还要记住,如果你webdriver-ioasync模式下使用,你应该总是解析返回 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
    } 

  })
})

推荐阅读