如何从赛普拉斯的阵列中获得随机物品? [英] How to get a random item from an array in Cypress?

查看:58
本文介绍了如何从赛普拉斯的阵列中获得随机物品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个测试,该测试每页从每组 5 个问题中选择一个随机的 button 单选答案.

I'm attempting to write a Cypress test that selects a random radio button answer from each group of 5 questions per page.

我正在尝试通过以编程方式在 1-5 之间选择一个数字并单击一个来做到这一点.可以从 JavaScript 中的 array 中获得随机项,那么如何在 Cypress 中做到这一点?

I'm trying to do this by programmatically choosing a number between 1- 5 and clicking on one. It is possible to get a random item from an array in JavaScript, so how to do this in Cypress?

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

This is the array example that I am using::

var myArray = ["Apples", "Bananas", "Pears"];
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];

由于 Cypress 仍处于起步阶段,因此我很难找到有关如何设置条件语句的示例.据我了解,在 Cypress 中使用变量时,如果变量是可见的,则无需定义即可对其进行访问.

Since Cypress is still in its infancy, I am having trouble finding examples on how to set up conditional statements. From what I understand of variable use in Cypress, if the variable is visible, it can be accessed without the necessity of defining it.

此外,我在选择适当的条件语句时遇到了问题,即随机选择一个单选按钮,其中问题将每个 5 3-5 可见答案随机化>每页的问题.

Also, I am having issues coming up with the proper conditional statement to randomly select a radio-button where the question randomizes 3-5 visible answers per each of the 5 questions per page.

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()

应该有一种方法,而不必在Cypress中过度使用 if 语句.我是一名初学者,所以任何提示或建议都将不胜感激!

There should be a way of doing this without the frowned-upon use of if statements in Cypress. I am a beginner developer so any tips or advice would be greatly appreciated!

推荐答案

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

just some other thought. Since they are all radio buttons, there is no need to create an array and randomly select one of that array.

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

What you also can do is create a random number between 0 and 4. Then combine it with the usage of eq().

您已经找到了如何随机选择数组项的方法,与随机选择数字的方法相同: Math.floor(Math.random()* 5).

You already found out how you can select an array-item randomly, same way you can select a number randomly: Math.floor(Math.random() * 5).

因此,如果我使用代码的最后一部分并添加随机选择,则它看起来像这样:

So if I take your last part of the code and add the random selection it looks like this:

cy.get('@mat-radio-group')
   .children() 
   .each(($matRadioGroup) => {
     cy.get($matRadioGroup)
         .children()
         .eq(Math.floor(Math.random() * 5))
         .click()

这篇关于如何从赛普拉斯的阵列中获得随机物品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆