【问题标题】:How to click on all anchor elements on a webpage using webdriver.io如何使用 webdriver.io 点击网页上的所有锚元素
【发布时间】:2019-04-03 18:13:40
【问题描述】:

我正在尝试浏览此网址 - 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
    })

  })
})

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

【问题讨论】:

    标签: javascript automated-tests webdriver-io


    【解决方案1】:

    forEach() 方法不适用于返回 Promise 的函数。您可以使用它for ... of 构造。另外请记住,如果您在 async 模式下使用 webdriver-io,您应该始终解析返回 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
        } 
    
      })
    })
    

    【讨论】:

    • 感谢工作!您知道如何从测试中获取浏览器上的控制台错误吗?我正在尝试验证任何测试中都没有控制台错误?有什么想法吗?
    • 最好创建一个新问题,但在我看来,这对 UI 测试来说是错误的检查。你不应该检查它。
    • 谢谢!你能解释一下为什么这样做不好吗?
    • 因为 UI 检查很昂贵并且可能会变得不稳定。以这种方式检查控制台错误与使用蒸汽锤敲碎坚果一样:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多