为什么webdriver打开这么多的驱动程序?

时间:2014-07-01 13:46:02

标签: java selenium webdriver cucumber-jvm selenium-chromedriver

当我开始测试时,会产生3-4个驱动程序,但只有其中一个会实际运行测试。我不希望不止一个司机开动。我使用intellij和maven项目。我在硒上面使用了黄瓜-jvm。我觉得我错过了一些简单的东西,但是我无法确定问题区域。

版本:

Selenium 2.42.2

Cucumber-junit 1.1.5

Chromedriver 2.42.2

测试跑步者代码:

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 {
}

小黄瓜脚本:

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();
}
}

页面对象抽象层:

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;
}
}

2 个答案:

答案 0 :(得分:4)

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

@Before将在每个测试方法之前运行,因此每次都会创建一个新的驱动程序。

您可能想试用@BeforeClass

答案 1 :(得分:0)

我想出来了,也许你说的是Arran,但我发现我的问题与课程有关,而不一定是方法。我有几个步骤定义类(每个类中有很多方法),当你执行一个cucumber-jvm测试时,看起来cucumber-jvm将加载所有这些步骤定义类,并且如果在所述类中有注释之前和之后,他们会执行。在我的情况下,我将它设置为之前将启动WebDriver实例的位置。我将功能从之前的方法移动到我的步骤定义类

中的各个步骤