0

我正在使用带有 BDD 语法(https://docs.cypress.io/api/cypress-api/spec)的柏树和下面的测试用例。(这是进行中的工作)

我想将行的值从“然后”行传递到“和”行。

Scenario: Table contains correct data
    Given I am on page "status overview"
    Then the "1" row "ID" should display "1"
      And "items" column should display "1"
      And "cost" column should display "2"
    Then the "2" row "ID" should display "1"
      And "items" column should display "1"
      And "cost" column should display "2"

我的测试定义步骤

Then('the {string} row {string} should display {string}', (index, column, value) => {
  column = column.toLowerCase();

  cy.task('setRowId',index);
  getRow(index)
    .find(`[data-testid="${column}"]`)
    .should('have.text', value)
})


And('{string} column should display {string}',(column, value)=>{
  column = column.toLowerCase();
  var index = (cy.task('getRowId')).toString();
  // index should be loaded with correct id from previous test
  getRow(index)
    .find(`[data-testid="${column}"]`)
    .should('have.text', value)
})

然后我添加了自定义柏树步骤

Cypress.Commands.add('setRowId',(val) => { return (rowId = val); })

Cypress.Commands.add('getRowId',() => { return rowId; })
4

2 回答 2

1

我想你想要一个别名

Then('the {string} row {string} should display {string}', (index, column, value) => {
  column = column.toLowerCase();

  cy.wrap(index).as('rowId')
  getRow(index)
    .find(`[data-testid="${column}"]`)
    .should('have.text', value)
})


And('{string} column should display {string}',(column, value)=>{
  column = column.toLowerCase();

  cy.get('@rowId')).then(rowId => {
    // rowId should be loaded with correct id from previous test
    getRow(rowId)
      .find(`[data-testid="${column}"]`)
      .should('have.text', value)
  });
})
于 2021-09-28T12:01:16.817 回答
0

为您的值创建全局变量是否适用于您的场景?像这样的东西:

//create global variable
let rowId

Then('the {string} row {string} should display {string}', (index, column, value) => {
  column = column.toLowerCase();

  //assign value to global variable
  rowId = index
  getRow(index)
    .find(`[data-testid="${column}"]`)
    .should('have.text', value)
})


And('{string} column should display {string}',(column, value)=>{
  column = column.toLowerCase()
  // assign value of global variable to index or use the global vriable itself
  index = rowId
  getRow(index)
    .find(`[data-testid="${column}"]`)
    .should('have.text', value)
})
于 2021-09-28T11:38:37.147 回答