如何使用webdriver.io单击网页上的所有锚点元素

时间:2019-04-03 18:13:41

标签: javascript automated-tests 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
    })

  })
})

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

1 个答案:

答案 0 :(得分:0)

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
    } 

  })
})