Automated tests 单击表格中的随机行

Automated tests 单击表格中的随机行,automated-tests,cypress,Automated Tests,Cypress,我的应用程序中有一个页面显示用户的突出缺陷。我想要一棵柏树点击其中一行,这将带你进入一个详细信息页面 我可以使用 cy.get('[data-cy=faultsTable] tr').then(($tr)=>{ }) 这正确地为我获得了预期的4个元素。但我不知道如何随机选择其中一个,因为。然后,我想依次对每一个执行 我想 a) 从中获取缺陷id b) 单击该行 如果您有任何建议,我们将不胜感激。也许有更好的方法解决这个问题,但以下是我的想法。如果缺陷Id记录的数量较少并且是静态的,我的

我的应用程序中有一个页面显示用户的突出缺陷。我想要一棵柏树点击其中一行,这将带你进入一个详细信息页面

我可以使用

cy.get('[data-cy=faultsTable] tr').then(($tr)=>{

})
这正确地为我获得了预期的4个元素。但我不知道如何随机选择其中一个,因为。然后,我想依次对每一个执行

我想

a) 从中获取缺陷id b) 单击该行


如果您有任何建议,我们将不胜感激。

也许有更好的方法解决这个问题,但以下是我的想法。如果
缺陷Id
记录的数量较少并且是静态的,我的意思是从表中,您可能会将这些缺陷Id传递到一个数组中,并随机返回
缺陷Id

选项:1 我在
/support
文件夹中得到了一个
sample.js
文件,我在其中添加了
randomDefectId
函数

module.exports ={
    randomDefectId: function(){
        var defect= ['10756', '10780', '19001', '21007', '25001', '27001'];
        var item = defect[Math.floor(Math.random()*defect.length)];
        return item;
    }

} 
然后我将它们导入到我的测试规范中

var rand = require('../../support/sample.js');
下面是我的测试,我将
rand.randomDefectId()
接收到
const
ranNumber

 describe('Get the defect id', function(){
    it('Check whether the defect id', function(){
         const ranNumber = rand.getRandomNumber(); 
         cy.visit('/');         
         console.log("Some number:"+ranNumber );
          cy.get('#tableID>tbody>tr>td').contains(ranNumber).click()
          // rest of the test step continues here...

        })
    })
选项:2 但是,如果表中有大量的
缺陷id
,那么您需要一种动态方法来获取缺陷id,我没有尝试下面的脚本,但是您可以尝试一下

randomDefectId: function(){
        let table = undefined;
        Cypress.$("#tableID>tbody>tr").each(function() {           
          var newArr = Cypress.$(this).find("td:last-child").html();          
          table = newArr;  
        });        
        return table;
    }

如果有更好的方法实现这一点,我想知道并分享

如果我创建一个函数来随机化一些东西,那么命名“sample.js”的最佳实践是什么?可能是name
helperFunction.js
commonFunction.js
你喜欢的东西。。