【问题标题】:Cypress & BDD how to pass value between two test cases?Cypress & BDD 如何在两个测试用例之间传递值?
【发布时间】:2021-06-30 10:56:18
【问题描述】:

我正在使用带有 BDD 语法 (https://docs.cypress.io/api/cypress-api/spec) 的 cypress,测试用例如下。 (正在进行中)

我想将“Then”行中的行值传递给“And”行。

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; })

【问题讨论】:

    标签: cypress cypress-cucumber-preprocessor


    【解决方案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)
      });
    })
    

    【讨论】:

      【解决方案2】:

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

      //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)
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多