Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

javascript - Cypress having trouble importing page objects in BDD step definition file

This is probably a simple resolution but after a number of days I can't fix this problem or get my BDD cypress step definition file to run.

I created a feature for the following scenario:

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 

This is the 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 def

I keep getting the following message from the terminal

Error: Can't walk dependency graph: Cannot find module '../../../../support/pageOjbects/HomePage' from '/Users/MYNAME/Documents/CYPRESSProjects/cypress/integration/BDD/ecommerce'
    required by /Users/MYNAME/Documents/CYPRESSProjects/cypress/integration/BDD/ecommerce/ecommerceStepDef.js


This occurred while Cypress was compiling and bundling your test code. This is usually caused by:

- A missing file or dependency
- A syntax error in the file or one of its dependencies

Fix the error in your code and re-run your tests.

There seems to an issues in this area here and I have no idea what I did wrong or how to fix it:

enter image description here

I will also add where the page object files are located in the integration folder if this helps me to solve this problem.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...