Multiple browser windows opening automatically in cucumber

Refresh

March 2019

Views

1.1k time

1

I don't know why I am getting 2 firefox browsers opened for the follwoing example. Can some one please tell me what is wrong in below code. I am new to cucumber and I am trying to develop cucumber poc with page object model.

Feature file:

    Scenario: Smoke test for application
      Given I am on home page

Step Defination file:

public class HomePageSteps {

CustomerDetails customerDetails;

HomePage homePage=new HomePage();

public HomePageSteps(CustomerDetails customerDetails){
    this.customerDetails=customerDetails;
}

@Before
public void environmentSteup(){
    homePage.envSetup();
}

@Given("^I am on home page$")
public void i_am_on_home_page() throws Throwable {
    homePage.openURL();
}
}

Actual implementation of Step definition file:(HomePage.java)

public class HomePage extends BasePage{

public void openURL() {
    driver.get("https://applicationURL.aspx");
    System.out.println("I am on home page executed");
}

public void envSetup() {
    driver=new FirefoxDriver();
    driver.manage().window().maximize();
}
}

BasePage.java

public abstract class BasePage {
protected WebDriver driver=new FirefoxDriver();
}

CustomerDetails.java

public class CustomerDetails {
private String mdn=null;

private String Fname=null;

private String Lname=null;

public String getMdn() {
    return mdn;
}

public void setMdn(String mdn) {
    this.mdn = mdn;
}
}

2 firefox browsers are opened: First it opens a blank browser. Later it opens another browser and in this browser it opens the application URL.

2 answers

3

You have two calls to open browser windows...

Once in the sub-class in envSetup() - driver=new FirefoxDriver();

And in the super class driver variable declaration with initialization - protected WebDriver driver=new FirefoxDriver();

You have to remove one of them, no need for the super class one... This is the one giving you the blank window

1

Refer to this page. Your maximize() call in envSetup() might be doing more than you think

In selenium webdriver what is manage() [driver.manage()]

edit:

You also do not need to instantiate a new FirefoxDriver() outside of BasePage as you have already instantiated a driver field with that object. Anything extending BasePage will have access to that driver field. It is not a problem that you're doing this, it is just extraneous code that doesn't need to be there