Javascript 如何从Cypress中的数组中获取随机项?

Javascript 如何从Cypress中的数组中获取随机项?,javascript,arrays,cypress,Javascript,Arrays,Cypress,我正在尝试编写一个Cypress测试,从每页5个问题中随机选择一个单选按钮答案 it('selects random radio buttons',() => { cy.get('@mat-radio-group') .children() .each(($matRadioGroup) => { cy.get($matRadioGroup).children() if($matRadioGroup.

我正在尝试编写一个
Cypress
测试,从每页
5个
问题中随机选择一个单选
按钮
答案

it('selects random radio buttons',() => {           
cy.get('@mat-radio-group')
     .children() 
     .each(($matRadioGroup) => {
         cy.get($matRadioGroup).children()
            if($matRadioGroup.children <= 5) {
                   .random function?
                   .click()
            }
        })

// This code clicks through all of the buttons on the page and leaves selected the last button for every question regardless of the randomizing visible answers (Does not randomize the button selection)

cy.get('@mat-radio-group')
   .children() 
   .each(($matRadioGroup) => {
     cy.get($matRadioGroup)
         .children()
         .eq(0)
         .click()
我试图通过编程方式在
1-5
之间选择一个数字并单击一个来实现这一点。可以从
JavaScript
中的
数组中获取随机项,那么如何在
Cypress
中实现这一点呢

这是我正在使用的
array
示例:

var myArray = ["Apples", "Bananas", "Pears"];
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
由于
Cypress
仍处于初级阶段,我很难找到关于如何设置条件语句的示例。根据我对
Cypress
中变量使用的理解,如果变量可见,则无需对其进行定义即可访问

此外,我还遇到了一些问题,比如如何使用适当的条件语句来随机选择一个单选按钮,在该单选按钮中,问题将每页
5个
问题的可见答案随机化

it('selects random radio buttons',() => {           
cy.get('@mat-radio-group')
     .children() 
     .each(($matRadioGroup) => {
         cy.get($matRadioGroup).children()
            if($matRadioGroup.children <= 5) {
                   .random function?
                   .click()
            }
        })

// This code clicks through all of the buttons on the page and leaves selected the last button for every question regardless of the randomizing visible answers (Does not randomize the button selection)

cy.get('@mat-radio-group')
   .children() 
   .each(($matRadioGroup) => {
     cy.get($matRadioGroup)
         .children()
         .eq(0)
         .click()
it('选择随机单选按钮',()=>{
cy.get(“@mat无线电组”)
.儿童()
.每个($matRadioGroup)=>{
cy.get($matRadioGroup).children()
如果($matRadioGroup.children){
cy.get($matRadioGroup)
.儿童()
.eq(0)
。单击()

应该有一种方法可以做到这一点,而不必在Cypress中使用
if
语句。我是一名初学者,因此非常感谢任何提示或建议!

只是一些其他想法。由于它们都是单选
按钮
,因此无需创建
数组
,然后随机选择其中一个ode>数组

您还可以在
0
4
之间创建一个随机数,然后将其与
eq()
的用法结合使用

您已经了解了如何随机选择数组项,就像随机选择数字一样:
Math.floor(Math.random()*5)

因此,如果我将代码的最后一部分添加到随机选择中,它看起来如下所示:

cy.get(“@mat无线电组”)
.儿童()
.每个($matRadioGroup)=>{
cy.get($matRadioGroup)
.儿童()
.eq(数学地板(数学随机()*5))
。单击()

只是一些其他想法。由于它们都是单选的
按钮
,因此无需创建
数组
,然后随机选择其中一个
数组

您还可以在
0
4
之间创建一个随机数,然后将其与
eq()
的用法结合使用

您已经了解了如何随机选择数组项,就像随机选择数字一样:
Math.floor(Math.random()*5)

因此,如果我将代码的最后一部分添加到随机选择中,它看起来如下所示:

cy.get(“@mat无线电组”)
.儿童()
.每个($matRadioGroup)=>{
cy.get($matRadioGroup)
.儿童()
.eq(数学地板(数学随机()*5))
。单击()

感谢您的回复。使用此方法时,我收到CypressError:Timed out retrying:预期会找到元素:“1”,但从未找到它。从元素中查询:。在额外运行时,该元素编号确实会更改/随机化。我相信此单选按钮组还包含字符串文本问题,因此可能这就是问题所在。感谢您的回复eply.当使用这种方式时,我得到了CypressError:超时重试:预期会找到元素:“1”,但从未找到它。从元素中查询:。在额外运行时,该元素编号确实会更改/随机化。我相信此单选按钮组还包含字符串文本问题,所以这可能就是问题所在?