0
votes

I am trying to execute a Cucumber Feature file which Step Definition in two different files. All methods in first Step Defination is executed and when executing second one, It launches a new browser instance, instead of continuing with existing one.

Cucumber Feature file Scenario: Given I open Firefox and Navigate to Guru When I enter UserName and Password and login to guru And I click on New Customer Then New Customer Page is displayed And I click on HomePage Then HomePage is displayed

First Step Definition

package stepDefination;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import Pages.HomePage;
import Pages.NewCustomerPage;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class GuruStepDef {
    WebDriver Driver;
    NewCustomerPage customerPage;
    HomePage homePage = new HomePage(Driver);

    @When("^I enter UserName and Password and login to guru$")
        public void I_enter_and_and_login_to_guru()  {
            homePage=homePage.setup();
            homePage.navigateToWebApp();
        }

@Then("^HomePage is displayed$")
    public void Homepage_is_displayed()  {
        //assert
    }

@Then("^I click on New Customer$")
    public void I_click_on_New_Customer() {
       customerPage= homePage.NavigateToCustomerPage();
    }

@Then("^New Customer Page is displayed$")
    public void New_Customer_Page_is_displayed()  {
      //assert
    }

}

Second Step Definition

package stepDefination;

import org.openqa.selenium.WebDriver;
import Pages.HomePage;
import Pages.NewCustomerPage;
import cucumber.api.java.en.Then;

public class SmokeTest {

    WebDriver Driver;
    NewCustomerPage customerPage;
    HomePage homePage = new HomePage(Driver);

@Then("^I click on HomePage$")
    public void I_click_on_HomePage()  {
        homePage=customerPage.Manager();
}

}

1

1 Answers

1
votes

In both classes, you have:

HomePage homePage = new HomePage(Driver);

You are creating two instances of HomePage. If you want to utilize the same object, you'll need to share it between the two classes. For example, you could create HomePage in one of the classes and use a getter in the other, or you could use a Singleton pattern in the object itself to ensure only one instance is created at a time.