【问题标题】:Click a random row in a table cypress单击表柏树中的随机行
【发布时间】:2019-01-30 18:22:15
【问题描述】:

我的应用中有一个页面向用户显示了突出的缺陷。我想让柏树点击其中一行,这会将您带到详细信息页面。

我可以使用

来选择表格行
cy.get('[data-cy=faultsTable] tr').then(($tr)=>{

})

这正确地得到了我预期的 4 个元素。但我不知道如何随机选择其中一个,因为 .then 想要依次执行每个。

我想要

a) 从 b) 单击该行。

如有任何建议,将不胜感激

【问题讨论】:

    标签: automated-tests cypress


    【解决方案1】:

    可能有更好的方法来解决这个问题,但以下是我的想法。如果defect Id 记录的数量较少并且是静态的,我的意思是从表中,您可以将这些缺陷ID 传递到一个数组中并随机返回defect 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 但是如果表内有大量defect 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”的最佳做法是什么?
    • 可以是 helperFunction.jscommonFunction.js 你喜欢的名字..
    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 2021-10-19
    • 2022-11-10
    • 2022-11-25
    • 1970-01-01
    • 2022-11-30
    • 2021-12-05
    • 2021-06-09
    相关资源
    最近更新 更多