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

标签 java selenium webdriver cucumber-jvm selenium-chromedriver

当我开始测试时,会生成 3-4 个驱动程序,但只有其中一个会实际运行测试。我不希望有多个驱动程序旋转。我正在使用 intellij 和一个 Maven 项目。我在selenium 之上使用cucumber-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 {
}

小 cucumber 脚本:

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

步骤定义:

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

最佳答案

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

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

您可能想尝试@BeforeClass

关于java - 为什么 webdriver 打开这么多驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24511876/

相关文章:

java - WebDriver:executeScript() 导致 BindException

python - 如何让 Selenium Web 驱动程序等待元素可访问,而不仅仅是存在?

Java 扫描器定界符用法

perl - 在 Selenium 和 Perl 中使用现有的 Firefox session

c# - Selenium `wait.until` 不等到提供的路径在 DOM 上呈现

java - 如何使用 Selenium WebDriver 查找在每个页面加载时动态 ID/名称发生变化的元素

java - Infinispan CacheEntryExpired 监听器

java - 如何将数组存储在单个数组中

java - 损坏的 CORBA 对象引用

selenium - 导出 WebDriver 代码 - Selenium IDE 与 Selenium Builder