【问题标题】:cypress-cucumber - Passing value from one step defination to another step definationcypress-cucumber - 将值从一个步骤定义传递到另一个步骤定义
【发布时间】:2021-07-21 10:01:52
【问题描述】:

我正在使用柏树黄瓜。我有以下情况

When I go to home page I extract the price
When I go to shopping cart page I extract the price
Then the price on the home page and shopping cart page is same

步骤定义:

When('I go to home page I extract the price', () => {
    ......
    ......
    const homePagePrice = 100
)
})

When('I go to shopping cart page I extract the price', () => {
    ......
    ......
    const shoppingCartPagePrice = 100
)
})

Then('the price on the home page and shopping cart page is same', () => {
    ......
    ......
)
})

我的问题是:如何在“那么主页和购物车页面上的价格相同”的步骤定义中传递主页和购物车页面中提取的价格

实现它的最佳方式/最佳实践是什么?

【问题讨论】:

    标签: cypress cypress-cucumber-preprocessor


    【解决方案1】:

    请参阅Sharing context 的示例,虽然我认为那里有错字

    Given('I go to the add new item page', () => {
      cy.visit('/addItem');
    });
    
    When('I add a new item', () => {
      cy.get('input[name="addNewItem"]').as('addNewItemInput');  // grab the value
      cy.get('@addNewItemInput').type('My item');
      cy.get('button[name="submitItem"]').click();
    })
    
    Then('I see new item added', () => {
      cy.get('td:contains("My item")');
    });
    
    Then('I can add another item', () => {
      cy.get('@addNewItemInput').should('be.empty');             // use the value
    });
    

    您的代码...

    When('I go to home page I extract the price', () => {
      const homePagePrice = 100
      cy.wrap(homePagePrice).as('homePagePrice');
    })
    
    When('I go to shopping cart page I extract the price', () => {
      const shoppingCartPagePrice = 100
      cy.wrap(shoppingCartPagePrice).as('shoppingCartPagePrice');
    })
    
    Then('the price on the home page and shopping cart page is same', () => {
      cy.get('@homePagePrice').then(homePagePrice => {
        cy.get('@shoppingCartPagePrice').then(shoppingCartPagePrice => {
          expect(homePagePrice).to.eq(shoppingCartPagePrice)
        })
      })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多