having a terrible time trying to run my BDD test case in cypress. For the sake of speed I've done a quick video so you can see what I am experiencing.
I keep getting the following error message from my terminal zsh: command not found: cypress
I've used the following syntax:
cypress run --spec /Users/myName/Documents/CYPRESSProjects/BDD/ecommerce.feature --headed --browser chrome
also the following alternatives:
cypress run --spec /Users/myName/Documents/CYPRESSProjects/BDD/ecommerce.feature --headed --browser chrome
cypress run --spec="CYPRESSProjects/BDD/ecommerce.feature" --headed --browser chrome
Now I'm wondering if I've done something wrong with my package json. or my code for feature or spec definition file. So I will add it just to thorough.
Thanks a million in advance
Package 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 file
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
Step definition file:
/// <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
In package.json update scripts section with below run configuration
For execution