vendredi 30 novembre 2018

Cypress how can I use the number of elements on a page in a function

I’m new to cypress and am trying to learn it by converting some of our current behat tests to cypress, one of our scenarios checks article links on multiple ‘list’ pages of a website with a step definition of ‘And I click random article.

this step definition,

  • gets the number of article links on the page
  • uses this as the upper end of a range for a random number
  • this random number is then used to target the corresponding article link

using cypress with the cucumber plugin, I have this semi-working in with

Then(/^I click on a random article$/, () => {

let num = Math.floor(Math.random() * 10)

cy.get(‘.article_link’)
    .eq(num).click()
})

The issue is that the number of the articles on the page can vary but I haven’t found a way to pass this varying number of the articles to the Math function and am instead using a ‘semi’ safe upper limit of 10.




Aucun commentaire:

Enregistrer un commentaire