【问题标题】:How to click on random element "N" number of times?如何点击随机元素“N”次?
【发布时间】:2021-05-25 13:13:05
【问题描述】:

要求是点击随机元素 n 次。 (4次) 这些元素有共同的 CSS 选择器[class*='SelectionContent']。 我的想法是抓取并存储数组中的所有元素,然后使用Math.random function单击它我使用此代码作为参考

const months = ["January", "February", "March", "April", "May", "June", "July"]; const random = Math.floor(Math.random() * months.length); console.log(random, months[random]);

但问题是我如何点击元素 4 次?

我尝试了什么 var items = Array(cy.get('[class*="style__SelectionContent"]')) Math.floor(Math.random() * items.length).click({ multiple: true });

【问题讨论】:

  • 把它放在一个循环中,直到达到次数为止。
  • click on element 4 times - 呼叫.click() 4 次。将随机元素生成器移动到函数并调用它 4 次。
  • 这里的问题是如果我通过{ multiple: true } 然后 cypress 尝试点击所有元素
  • var items = cy.get() 在 Cypress 中不起作用,学习如何使用链。
  • @CherryDT 你是对的,我重做了我的测试,一个简单的 for 循环就可以了,它显然是一个线程,每个 exec 等待前一个线程的结束。我删除了我的答案,这无关紧要

标签: javascript cypress


【解决方案1】:

只需一个简单的forLoop即可完成

for (let i = 0; i < 4; i++) {
   cy.get(element).click()
   cy.wait(100) //this is in case there is some rendering after click
}

或者recursion

function click4 (element, i = 0) {
   if (i>=4) { return }
   cy.get(element).click()
   cy.wait(100) //this is in case there is some rendering after click
   i++
   click4(element, i)
}
click4(element)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2012-02-16
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多