我一直在使用 Cypress/Cucumber 时收到步骤定义错误

I keep getting a Step definiton error using Cypress/Cucumber

当我尝试 运行 我的 Cypress/Cucumber 时,我收到 “缺少步骤实施:我打开电子商务页面” test.feature 文件.

我指的是另一个 post 来帮助我,但没能帮助解决我自己的问题。我用的post的link如下

这就是我为此准备的一切。

这里首先是我调整过的当前文件结构

-cypress
  -integration
  -test
    -test.feature
    -test.js

这是我的test.feature文件

Feature: End to end GFM validation



Scenario: Ecommerce products delivery
Given I open ecommerce page
When I add items to cart
And Validate the total prices
Then select the country submit and verify thank you

这是包含步骤定义的test.js文件

/// <reference types="Cypress" />
import { Given,When,Then,And } from 'cypress-cucumber-preprocessor/steps'
import HomePage from "../../support/classes/HomePage"
import ProductPage from "../../support/classes/ProductPage"
import CheckOutPg from "../../support/classes/CheckOutPg"



const homePage = new HomePage()
const productPage = new ProductPage()
const checkOut = new CheckOutPg()




Given("I open ecommerce page",()=>{
    cy.visit(Cypress.env('url')+"/angularpractice/")
})


//When using hook to load data, use non-es6 syntax for a function.....might need to check if this was updated in later releases
When('I add items to cart',function(){
    
    homePage.getShopTab().click()
    
    
    testData.productName.forEach((product)=>{
        cy.selectProduct(product)
    })


    
    productPage.getCheckoutBtn().click()


})

And('Validate the total prices',()=>{
    let sum = 0
    let total;
                                              
    checkOut.getItemPrices().each(($el,index,$list)=>{
        let price = $el.text().split(' ')[1].trim()
        sum = Number(sum) + Number(price);
    }).then(()=>{
        cy.log(sum)
        
    })
    checkOut.getTotal().then((el)=> {
        total =   el.text().split(' ')[1].trim()
        expect(Number(total)).to.eq(sum)
       })

})

Then('select the country submit and verify thank you',()=>{
    cy.get(':nth-child(6) > :nth-child(5) > .btn').click()
    cy.get('#country').type('United States')
    cy.wait(6000)
    cy.get('.suggestions > ul > li > a').click()
    cy.get('input[type="checkbox"]').check({force:true})
    cy.get('.ng-untouched > .btn').click()
    cy.get('.alert').then((el)=>{
        const text = el.text()
       expect(text.includes('Success')).to.be.true
    })
})

我在 package.json 中添加了 stepDefinitions,并根据黄瓜文档将 nonGlobalStepDifitions 属性设置为 true stepDefinition 的路径设置为 "cypress/integration".

"cypress-cucumber-preprocessor": {
    "stepDefinitions": "cypress/integration",
    "nonGlobalStepDefinitions": true,
    "cucumberJson": {
      "generate": true,
      "outputFolder": "cypress/cucumber-json",
      "filePrefix": "",
      "fileSuffix": ".cucumber"
    }
  }

我不确定我是否很好地介绍了这方面的背景,所以我愿意接受任何建议。除此之外,还有谁能帮我看看哪里出了问题?

当您在 package.json 中设置 "nonGlobalStepDefinitions": true"stepDefinitions": "cypress/integration" 时,Cypress-cucumber-preprocessor 推荐此文件夹结构。

请参阅文档 here

- cypress/
  - integration/
    - feature1.feature
    - feature1/
      - feature1.js
    - feature2.feature
    - feature2/
      - feature2.js

所以:

  1. cypress/integration
  2. 中创建您的 .feature 文件
  3. 创建一个与您的 .feature 文件同名的文件夹,也在 cypress/integration
  4. 在新创建的文件夹中使用您的步骤定义创建一个 .js 文件