Search code examples
javascriptcucumbercypress

Can one feature file be called in another feature file in cucumber


Below is my cypress-cucumber project structure.

If I want to use steps from the Login.feature, Logout.feature inside the Home.feature how to do that?

Basically I want to reuse login & log out in Home istead of rewrite them in again in Home Here is the configuration:

{
  "stepDefinitions": [
    "cypress/e2e/step_definitions/*.js"    // use any step in any feature
  ]
}

Login.feature
Feature: Successful Login into the application  
   Background: 
       Given Launch he application
       Then  I enter Username "xxxx"
       Then  I enter Password "xxxx"   
       Then  I click on Login Button

Home.feature      
    Feature:  application - Home Page with Page Object Model with cypress cucumber BDD
    
      Background: 
            Given I am on  Login page
            Then  I enter Username "XXXX"
            Then  I enter Password "XXXXX"   
            Then  I click on Login Button 
    
            Scenario: Verify Home Page after Login
                Then  I am on Home Page
                Then  I log out

Logout.feature
Feature: Logout from the application
      Background: 
            Then  I Log Out

LogoutSteps.js

/// <reference types="Cypress" />

import { Given, When, Then } from "@badeball/cypress-cucumber-preprocessor";
import { Logout } from "../../Pages/LogoutPage"
const logout = new Logout()


Then("I log out", () => {
    logout.clickLogout()

}) 

HomeSteps.js

// <reference types="Cypress" />
    
    import { Given, When, Then } from "@badeball/cypress-cucumber-preprocessor";
    import { Login } from "../../Pages/LoginPage"
    import { Home } from "../../Pages/HomePage"
    import { Logout } from "../../Pages/LogoutPage"
    const login = new Login()
    const home = new Home()
    const logout = new Logout()
    
    Then("I am on Home Page", () => {
        home.verifyHomePage() 
        home.searchfield() 
    
    })  

enter image description here


Solution

  • You haven't given details of configuration, but this sounds like a case for common step definitions.

    For example

    {
      "stepDefinitions": [
        "cypress/e2e/[filepath].js",                  // 1) steps by feature
        "cypress/e2e/common-step-definitions/*.js"    // 2) common to all
      ]
    }
    
    /home/john.doe/my-project
    └── cypress
        └── e2e
            ├── foo
            │   ├── a.feature
            │   └── a.js
            ├── bar
            │   ├── b.feature
            │   └── b.js
            └── common-step-definitions
                ├── 1.js
                └── 2.js
    

    But since you have a single folder for steps, it depends on how you configured things.

    If you used pattern #2, you should be able to reference any steps in any feature:

    {
      "stepDefinitions": [
        "cypress/e2e/step-definitions/*.js"    // use any step in any feature
      ]
    }