在赛普拉斯中如何计算表中的行数?

问题描述

我正在与Cypress进行E2E测试。 我必须计算表中不同情况下的行数。

在普通的javascript中,我只是写这个来获取列的总行数

document.getElementById('hoMetable').getElementsByTagName("tr").length-1

不幸的是,在赛普拉斯,我收到以下错误消息:

 document.getElementById('hoMetable').getElementsByTagName("tr").length-1

VM298:1 Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
    at <anonymous>:1:38
(anonymous) @ VM298:1

我试图这样数,但一无所获

cy.get('hoMetable').find('tr').each(function(row,i){
        expect(i)
})

$Chainer {userInvocationStack: "",specWindow: Window,chainerId: "chainer99",firstCall: false,useInitialStack: false}
chainerId: "chainer99"
firstCall: false
specWindow: Window {parent: Window,opener: null,top: Window,length: 0,frames: Window,…}
useInitialStack: false
userInvocationStack: ""
__proto__: Object

我不知道它在哪里返回行数

解决方法

我认为您的方法是正确的,您可以使用each()。您可以为每个列表添加另一个参数,这将为您提供行数计数。您可以检查cypress document for each()。确保定位器正确。

cy.get('#hometable > tr').each(function(row,i,list) {}).then(function(list) {
      //list will give you the count of tr
    }