将从 .invoke() 返回的值分配给 Cypress 中的变量

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

我正在使用以下代码从 < td > 获取文本值:

 let serviceName;
    cy.get('tr').eq(1).then(row => {
        cy.wrap(row).find('td').eq(1).invoke('prop', 'innerText').then(val => { serviceName = val });
    })

它在某些页面上工作正常,但在具有相同表格类型/类/等的其他页面上工作不佳。 前端是用 Angular 构建的

使用像这样的相同代码也会失败:

let serviceName;
    cy.get('tr').eq(1).find('td').eq(1).invoke('prop', 'innerText').then(val => { serviceName = val });
    })
javascript automated-tests cypress
1个回答
0
投票

您可以使用 aliases 为此。

cy.get('tr')
  .eq(1)
  .find('td', {timeout: 5000})
  .eq(1)
  .should('be.visible')
  .invoke('prop', 'innerText')
  .as('text')

cy.get('@text').then((text) => {
  //access text here
})
© www.soinside.com 2019 - 2024. All rights reserved.