Why does the webdriver open so many drivers?

advertisements

When I start a test, 3-4 drivers will spawn, but only one of them will actually run the test. I do not want more than one driver spinning up. I'm using intellij, and a maven project. I'm using cucumber-jvm on top of selenium. I feel like I'm missing something simple, but I'm not able to pin point the problem area.

Versions:

Selenium 2.42.2

Cucumber-junit 1.1.5

Chromedriver 2.42.2

Test runner code:

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Cucumber.Options(
    features = "automation/src/main/resources/applicationLogin.feature",
    format = {"pretty", "html:target/cucumber", "json:target/cucmber.json"})

public class ApplicationLoginTest {
}

Gherkin script:

Feature: Application login
As a user
I want to login to the application
So I can see the dashboard

Scenario: Login to the application
  Given I am on the page "product URL"
  And I enter "username" into the username field
  And I enter the "password" into the password field
  And I click the "submit" button
  And I accept the "User Agreement"
  Then I should be on the "dashboard" page

Stepdefs:

package stepdefs;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import objectmaps.LoginMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import static com.thoughtworks.selenium.SeleneseTestBase.assertTrue;

public class ApplicationLoginStepDefs {
protected WebDriver driver;
protected LoginMap loginMap;

@Given("^I am on the page \"([^\"]*)\"$")
public void I_am_on_the_page(String page) throws Throwable {
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    loginMap = PageFactory.initElements(driver, LoginMap.class);
    driver.get(page);
}

@And("^I enter \"([^\"]*)\" into the username field$")
public void I_enter_into_the_username_field(String arg1) throws Throwable {
    loginMap.getUsernameField().sendKeys("automation");
}

@And("^I enter the \"([^\"]*)\" into the password field$")
public void I_enter_the_into_the_password_field(String arg1) throws Throwable {
    loginMap.getPasswordField().sendKeys("a");
}

@And("^I click the \"([^\"]*)\" button$")
public void I_click_the_button(String arg1) throws Throwable {
    loginMap.getLoginButton().submit();
}

@And("^I accept the \"([^\"]*)\"$")
public void I_accept_the(String arg1) throws Throwable {
    Thread.sleep(2000);
    loginMap.getBetaUserTermsAgree().click();
}

@Then("^I should be on the \"([^\"]*)\" page$")
public void I_should_be_on_the_page(String text) throws Throwable {
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ng-binding")));
    assertTrue(driver.getCurrentUrl().contains(text));
    driver.quit();
}
}

Page object abstraction layer:

package objectmaps;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class LoginMap {
protected String hawkeyeLoginPage = "product URL";
@FindBy(id = "hk-login-username")
private WebElement usernameField;
@FindBy(id = "hk-login-password")
private WebElement passwordField;
@FindBy(xpath = "//button[contains(text(),'Agree')]")
private WebElement betaUserTermsAgree;
@FindBy(xpath = "//button[contains(text(),'Cancel')]")
private WebElement betaUserTermsCancel;

public WebElement getUsernameField() {
    return usernameField;
}

public WebElement getPasswordField() {
    return passwordField;
}

public WebElement getBetaUserTermsAgree() {
    return betaUserTermsAgree;
}

public WebElement getBetaUserTermsCancel() {
    return betaUserTermsCancel;
}

public WebElement getLoginButton() {
    WebElement element = getUsernameField();
    return element;
}

public void loginToHawkeye() throws Exception{
    usernameField.sendKeys("automation");
    passwordField.sendKeys("a");
    getLoginButton().submit();
    Thread.sleep(2000);
}

public void acceptUserAgreement() throws Exception{
    Thread.sleep(2000);
    getBetaUserTermsAgree().click();
}

public String getHawkeyeLoginPage() {
    return hawkeyeLoginPage;
}
}


@Before
public void setUp() {
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    loginMap = PageFactory.initElements(driver, LoginMap.class);
}

@Before will run before each test method, so a new driver will be created each time.

You may want to try out @BeforeClass