Home > Software design >  How to check many urls in one test?
How to check many urls in one test?

Time:07-31

I have a list of urls and need to check the same element on every of them. But there are 2 conditions:

  1. all must be done in 1 it()
  2. test must not fail after the first wrong url, but must check all of them and show result only after list finishing

I tried to do it by loop and try-catch (because there is not soft asserting by default), but then the test always was passed, even when text from element was wrong (as I undestand it's because of asynchronicing).

    let errorUrls = []
  
    for (let url in data.PRODUCTS_PAGES) {
        cy.visit(url)
        cy.get('div.configurator-controls span').invoke('text')
            .then(text => {
                try {
                    expect(text).to.equal(data.PRODUCTS_PAGES[url])
                } catch (e) {
                    errorUrls.push(e.message)
                }
            })
    }

    expect(errorUrls).to.be.empty

How can I do this?

CodePudding user response:

If you don't want to fail, don't use the expect() in the loop.

Note, all URL's must be same-origin.

let errorUrls = []

for (let url in data.PRODUCTS_PAGES) {
  cy.visit(data.PRODUCTS_PAGES[url])

  cy.get('div.configurator-controls span').invoke('text')
    .then(text => {
      if (text !== data.PRODUCTS_PAGES[url]) {
        errorUrls.push(e.message)
      }
    })
}

cy.then(() => {
  cy.log(errorUrls)
  expect(errorUrls).to.have.length(0)
})

CodePudding user response:

You can do something like this:

let errorUrls = []

for (let url in data.PRODUCTS_PAGES) {
  cy.visit(data.PRODUCTS_PAGES[url])
  cy.get('div.configurator-controls span').then(($ele) => {
    if ($ele.text() == data.PRODUCTS_PAGES[url]) {
      expect($ele.text()).to.equal(data.PRODUCTS_PAGES[url])
    } else {
      errorUrls.push(data.PRODUCTS_PAGES[url])
    }
  })
}
cy.then(() => {
  expect(errorUrls).to.be.empty
})
  • Related