0

尝试在 cypress 中运行我的 BDD 测试用例时遇到了可怕的事情。为了速度,我做了一个快速视频,这样你就可以看到我正在经历的事情。

我不断从我的终端收到以下错误消息zsh: command not found: cypress

我使用了以下语法:

cypress run --spec /Users/myName/Documents/CYPRESSProjects/BDD/ecommerce.feature --headed --browser chrome

还有以下替代方案:

cypress run --spec /Users/myName/Documents/CYPRESSProjects/BDD/ecommerce.feature --headed --browser chrome

cypress run --spec="CYPRESSProjects/BDD/ecommerce.feature" --headed --browser chrome

现在我想知道我的包 json 是否做错了什么。或我的功能或规范定义文件的代码。所以我会添加它只是为了彻底。

提前一百万谢谢

包Json

{
  "name": "automation",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "./node_modules/.bin/cypress run",
    "headTest": "npm run test -- --headed ",
    "choromeTest": "npm run test -- --browser chrome ",
    "recordDashBoardTest": " npm run test --  --record --key 61a5893c-7e1b-43b9-a43f-3f1c4055e530 --reporter mochawesome",
    "GreenKartTest": " npm run test -- --spec \"cypress/integration/GreenKart/*\"    "
  },

  "cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": true
  },

  "author": "myName",
  "license": "ISC",
  "devDependencies": {
    "@4tw/cypress-drag-drop": "^1.4.0",
    "@cypress/skip-test": "^2.5.1",
    "cypress": "^6.0.0",
    "cypress-cucumber-preprocessor": "^4.0.0",
    "cypress-file-upload": "^4.1.1",
    "cypress-mailosaur": "^2.0.1",
    "mocha": "^5.2.0",
    "mochawesome": "^4.1.0"
  }
}

BDD 功能文件

Feature: Add items to shopping cart and add delivery address

    using cypress and BDD we are going to add a number of items to shopping basket and checkout 

    Scenario: Ecommerce Product delivery

    Given I am on the Ecommerce page 
    When I add mobile phones to the shopping cart 
    And I validate the total price in cart 
    Then I add my chosen delivery country and verify a thank you message 

步骤定义文件:

/// <reference types="Cypress" />
import HomePage from '../../../../support/pageOjbects/HomePage'
import ProductPage from '../../../../support/pageOjbects/ProductPage'
import CheckoutOrderPage from '../../../../support/pageOjbects/CheckOrderPage'
import { Given,When,Then,And } from "cypress-cucumber-preprocessor/steps";
const homePage=  new HomePage()
const productPage = new ProductPage()


Given ( 'I am on the Ecommerce page', () => {

//add your page objects selector here
cy.visit(Cypress.env('url')+"/angularpractice/")

})


When('I add mobile phones to the shopping cart', function() {
//add code 

homePage.getShopTab().click()//find the SHOP selector and click on it 


  this.data.productName.forEach(function(element) {//place the data from the data.json file and place in the forEach this loop

    cy.selectProduct(element)  //using the customised command add the shopping items to the cart.
  });
 
 
  productPage.getCheckoutButton().click()


})//end

And( 'I validate the total price in cart', () =>{

    //add code here
    var sum=0//start calculation of shopping cart total 
  
  //Add total cost in shopping cart  of item in shopping cart
  cy.get('tr td:nth-child(4) strong') .each(($e1, index, $list) =>{//to calculate items in an array with javascript
   
    const unitCost=$e1.text()  //find text
    var res= unitCost.split(" ") //split text from the currency sign
    res= res[1].trim() //remove any white spaces 
    sum=Number(sum)+Number(res)//convert into a Integer number
    
}).then(function()//stop us giving the result BEFORE calculating we will add a promise 
{
    cy.log(sum)//End calculation of shopping cart total

})

cy.get('h3 strong').then(function(element)
{
    const shopCartTotal=element.text()  //find text
    var res= shopCartTotal.split(" ") //split from the currency sign
    var total= res[1].trim()//remove any white spaces 
    expect(Number(total)).to.equal(sum)//assertion to state total in cart and calculation is correct.

})
        
    })//end of step 

Then('I add my chosen delivery country and verify a thank you message',() =>{

// Add code for step
orderPage.getOrderButton().click()
  cy.get('#country').type('United Kingdom')
  cy.get('  .suggestions > ul > li > a').click()
  cy.get('#checkbox2').click({force: true})
  cy.get('input[type="submit"]').click() 
  //cy.get('.alert').should('have.text','Success! Thank you! Your order will be delivered in next few weeks :-).')
  cy.get('.alert').then(function(element){

   //How confirm a text element exists
    const actualText= element.text()
   expect(actualText.includes('Success')).to.be.true
  })//end of promise 


})//end of step
4

2 回答 2

0

在 package.json 更新脚本部分,运行配置如下

"scripts": {
    "test": "./node_modules/.bin/cypress run",
    "cuketest": "npm run test -- --spec **/*.feature --browser chrome"
 }

执行

>npm run cuketest
于 2020-12-15T11:44:14.240 回答
0

您好可以通过使用以下命令解决此问题:

npx cypress run --spec /Users/MYName/Documents/CYPRESSProjects/BDD/ecommerce.feature --headed --browser chrome

这允许 cypress 开始运行 BDD 文件。

资源:https://docs.cypress.io/guides/guides/command-line.html#cypress-run

于 2020-12-19T12:28:20.987 回答