0

我正在尝试使用 BDD 运行测试。它将打开测试页面,但是当我要求它进入包含一些测试数据的页面对象时,我从赛普拉斯控制台收到以下消息。

有趣的是,在将页面对象添加为普通 mocha 测试之前,我已经运行了这个测试。这段代码完美地端到端运行。

那么这个错误是从哪里来的呢?

在此处输入图像描述

BDD 测试场景

场景:用户可以购买商品并将其交付到送货地址

Given I am on the Ecommerce page
When I add the mobile handsets to the shopping cart
And I verify the total price of shopping cart
Then I select the checkout button
When I will select my shipping location
And I agree to the terms and conditions checkbox
When I select the Purchase button
Then I will see an alert stating my order was successful, plus an ETA delivery

步骤定义文件

/// <reference types="Cypress" />

import { Given,When,Then,And } from "cypress-cucumber-preprocessor/steps";
import Homepage from "../../../support/pageObjects/Homepage";
import orderSummaryPage from "../../../support/pageObjects/orderSummaryPage";
import completeOrderPage from "../../../support/pageObjects/completeOrderPage";
 
const homepage = new Homepage()
const StartCheckout = new orderSummaryPage()
const CompleteOrder = new completeOrderPage()
 
Given ( 'I am on the Ecommerce page', () => {
 
    cy.visit(Cypress.env('url')+"/angularpractice/")
    
    })
 
When('I add the mobile handsets to the shopping cart',function () {
 
        homepage.getShopTab().click() 
    
        this.data.mobileHandset.forEach(function(element) {// this custom commad will add items to your cart
    
              cy.AddToCart(element)  
            }); 
    
    
    StartCheckout.getBasketCheckoutButton().click()
    
    } )//end of step 
 
And('I verify the total price of shopping cart',() => {
 
    //calculate shopping cart here
    let sum=0
       
    CompleteOrder.getProductCost().each(($e1, index, $list) =>{
    
      const unitCost=$e1.text()  
      let res= unitCost.split(" ") 
      res= res[1].trim() 
      sum=Number(sum)+Number(res)
      
    }).then(function() 
    {
      cy.log(sum)
    
    })
    
    CompleteOrder.getBasketTotalCost().then(function(element)
    {
      const shopCartTotal=element.text()  
      var res= shopCartTotal.split(" ") 
      var total= res[1].trim() 
      expect(Number(total)).to.equal(sum)
    
    })
    
    
    } )//end of step
 
Then('I select the checkout button',() => {
 
    StartCheckout.getStartCheckoutButton().click()
 
} )//end of step
 
When('I will select my shipping location',() => {
 
    CompleteOrder.getShippingCountry().type('United Kingdom')
    CompleteOrder.getShippingCountryConfirm().click()
 
} )//end of step
 
And('I agree to the terms and conditions checkbox',()=> {
 
    CompleteOrder.getTermsConditionsCheckbox().click({force: true})
 
})//end of step 
 
When('I select the Purchase button',()=> {
 
    CompleteOrder.getPurchaseButton().click()
 
})
 
Then('I will see an alert stating my order was successful, plus an ETA delivery',()=> {
 
    CompleteOrder.getPurchaseAlert().then(function(element){
 
      
        const actualText= element.text()
       expect(actualText.includes('Success')).to.be.true
    
      }) 
 
})
4

2 回答 2

1

箭头函数创建了一个新this范围,这可能就是您无法访问的原因this.data——它位于when场景的全局范围内。尝试用普通函数替换它们。

有趣的是,在将页面对象添加为普通 mocha 测试之前,我已经运行了这个测试。这段代码完美地端到端运行。

这是我在柏树测试中看到的。如果我想在全局范围内添加一个变量 - 我需要在一个before块中进行。可能它是赛普拉斯工作的异步方式的一部分。

于 2021-07-16T08:55:57.647 回答
0

好的,我能够解决这个问题。尝试上一个答案后,我仍然遇到问题。因此,我添加了以下内容以访问来自固定装置的数据(请参见下划线的行)。

在此处输入图像描述

然后,我将包含数据的测试步骤定义代码修改为:

When('I add the mobile handsets to the shopping cart', () =>{
 
        homepage.getShopTab().click() 
    
        data.mobileHandset.forEach(function(element) {// this custom commad will add items to your cart
    
              cy.AddToCart(element)  
            }); 
    
    
    StartCheckout.getBasketCheckoutButton().click()
    
    } )//end of step 

一旦我这样做了,cypress 就能够访问我的代码并成功运行。

出于某种原因this,在 cypress mocha 中使用数据可以正常工作,但由于某些未知原因,如果我this在 Cypress BDD 中使用它,它会停止工作。

对于其他试图在 cypress BDD 中检索数据的人来说,这是一个很好的选择。

于 2021-07-16T12:09:11.043 回答