繁体   English   中英

赛普拉斯 V10。 不在 BDD 测试中读取数据:TypeError:无法读取未定义的属性(读取“mobileHandset”)

[英]Cypress V10. Not Reading data in BDD test: TypeError: Cannot read properties of undefined (reading 'mobileHandset')

提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文

我希望有人能提供帮助,我刚刚将 Cypress Mocha 框架转换为 BDD。 在转换之前它运行完美并且测试运行顺利。 现在我已经转换了它,我似乎收到一条错误消息Cannot read properties of undefined (reading 'mobileHandset') 我以前从未遇到过这个问题,所以我很困惑。 这是代码并观看此视频

特征:

Feature: End to End Shopping Purchase Validation

 Registered user will be able to purchase an item and have it shipped to their country

    Scenario: Customer Purchase and delivery
    
    Given  I am on the eCommerce page
    When I add items to cart 
    And I confirm shopping cart total
    Then I select my delivery country and see a thank for your order notification

步骤定义

import { Given, And, Then, When } from "@badeball/cypress-cucumber-preprocessor";
import Homepage from '../../../support/pageObjects/Homepage'
import orderSummaryPage from '../../../support/pageObjects/orderSummaryPage'
import completeOrderPage from '../../../support/pageObjects/completeOrderPage'
const data = require ('../../../fixtures/example.json');

const homepage = new Homepage()
const StartCheckout = new orderSummaryPage()
const CompleteOrder = new completeOrderPage()



Given(/^I am on the eCommerce page$/, () => {
    cy.visit(``+"/angularpractice/")

});

When(/^I add items to cart$/, function() {
    
  homepage.getShopTab().click({force:true}) 

    this.data.mobileHandset.forEach(function(element) {// this custom commad will add items to your cart

          cy.AddToCart(element)  
        }); 

        StartCheckout.getBasketCheckoutButton().click()

});

When(/^I confirm shopping cart total$/, () => {

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

Then(/^I select my delivery country and see a thank for your order notification$/, () => {
    
    StartCheckout.getStartCheckoutButton().click()
    CompleteOrder.getShippingCountry().type('United Kingdom')
    CompleteOrder.getShippingCountryConfirm().click()
    CompleteOrder.getTermsConditionsCheckbox().click({force: true})
    CompleteOrder.getPurchaseButton().click()
    
    CompleteOrder.getPurchaseAlert().then(function(element){

      
       const actualText= element.text()
      expect(actualText.includes('Success')).to.be.true
   
     }) 
    
});

这是数据

{
  "name": "MY_NAME",
  "gender": "Female",
  "mobileHandset": ["Blackberry", "Nokia Edge"]
  
  
}

每个之前

beforeEach(function()
{

    cy.fixture('example').then(function(data){ 
 
        this.data=data 

       })

})

经过讨论,我将 BeforeEach 文件移至 Support。 仍然得到原来的错误

在此处输入图像描述

如果cypress/fixtures 文件夹中已有数据夹具,则无需导入它。

您可以在测试之前将夹具加载到 Before 挂钩中。

import { 
    Given,
    And,
    Then,
    When,
    Before 
} from "@badeball/cypress-cucumber-preprocessor";
//...

Before(function() {
    cy.fixture('example').then((data) => {
        this.data = data;
    });
});
//...

您的beforeEach()应该可以正常工作,但是您不必只引用data而不是this.data

const data = require ('../../../fixtures/example.json');  // data available anywhere in this step
...
When(/^I add items to cart$/, () => {
  ...
  data.mobileHandset.forEach(element => {
    cy.AddToCart(element)  
  })
  ...
})

惯例是使用cy.fixture()

When(/^I add items to cart$/, () => {
  ...
  cy.fixture('example.json').then(data => {    // no ../.. needed
    data.mobileHandset.forEach(element => {
      cy.AddToCart(element)  
    })
  }) 
  ...
});
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM