Cypress - 如何在表中获取动态值

问题描述 投票:0回答:2

我正在尝试从当前为“62”但会更改的表中获取动态值。我试过:

   cy.get('tbody>tr').eq(0)
   .find("td").eq(1)
   .then(text => {
    const rowText = text;
    cy.log(rowText)

But it's outputing
<td.MuiTableCell-root.MuiTableCell-body.MuiTableCell-alignCenter.MuiTableCell-sizeMedium.css-1cjito2> instead of 62

无法发布图片所以这是它的链接https://i.stack.imgur.com/g10ZQ.png

automation cypress
2个回答
0
投票

你的

.find('td').eq(1)
产生的变量不是文本变量,而是一个JQuery元素。您需要抽象该值。您可以通过
cy.invoke()
或使用 JQuery
.text()
函数来做到这一点。

cy.get('tbody>tr').eq(0)
   .find("td").eq(1)
   .invoke('text')
   .then(text => {
     cy.log(text);
   });

cy.get('tbody>tr').eq(0)
   .find("td").eq(1)
   .then($el => {
     cy.log($el.text());
   });

0
投票

对于动态文本(一段时间后更改),您需要使用

.should()
语句申请重试。

接受的答案不会那样做,所以可能会不稳定。

这是一个更可靠的测试。

cy.get('tbody tr').eq(0)
  .find('td').eq(1)
  .should('have.text', newText)   // assert text after change, retries until true
  .then($cell => {
    cy.log('Cell @0,1 text:', $cell.text())
  })

或与

invoke()

cy.get('tbody tr').eq(0)
  .find('td').eq(1)
  .invoke('text')
  .should('eq', newText)   // assert text after change, retries until true
  .then(cellText => {
    cy.log('Cell @0,1 text:', cellText)
  })
© www.soinside.com 2019 - 2024. All rights reserved.