0

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

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', () => {
    ......
    ......
)
})

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

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

4

1 回答 1

1

请参阅共享上下文中的示例,尽管我认为那里有错字

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)
    })
  })
})
于 2021-07-21T10:17:35.363 回答