Java 为什么webdriver要打开这么多驱动程序?
当我开始一个测试时,会产生3-4个驱动程序,但实际上只有一个驱动程序会运行测试。我不想让一个以上的司机旋转起来。我正在使用intellij和maven项目。我在硒的基础上使用黄瓜jvm。我觉得我遗漏了一些简单的东西,但我无法指出问题所在 版本: 硒2.42.2 黄瓜junit 1.1.5 Chromedriver 2.42.2 测试运行程序代码:Java 为什么webdriver要打开这么多驱动程序?,java,selenium,webdriver,cucumber-jvm,selenium-chromedriver,Java,Selenium,Webdriver,Cucumber Jvm,Selenium Chromedriver,当我开始一个测试时,会产生3-4个驱动程序,但实际上只有一个驱动程序会运行测试。我不想让一个以上的司机旋转起来。我正在使用intellij和maven项目。我在硒的基础上使用黄瓜jvm。我觉得我遗漏了一些简单的东西,但我无法指出问题所在 版本: 硒2.42.2 黄瓜junit 1.1.5 Chromedriver 2.42.2 测试运行程序代码: import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunW
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
步骤定义:
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
将在每个测试方法之前运行,因此每次都将创建一个新的驱动程序
您可能需要在上课前试用@BeforeClass
@Before
将在每个测试方法之前运行,因此每次都将创建一个新的驱动程序
你可能想在上课前试试
@BeforeClass
我想出来了,这可能是你说的,Arran,但我发现我的问题在于类,而不一定是方法。我有几个步骤定义类(每个类中有许多方法),当您执行一个cucumber jvm测试时,cucumber jvm似乎将加载所有这些步骤定义类,如果在所述类中有前后注释,它们将执行。在我的例子中,我将它设置为之前将启动WebDriver实例的位置。我将功能从以前的方法转移到我的步骤定义类中的各个步骤中我弄明白了,这可能是你说的Arran,但我发现我的问题在于类,而不一定是方法。我有几个步骤定义类(每个类中有许多方法),当您执行一个cucumber jvm测试时,cucumber jvm似乎将加载所有这些步骤定义类,如果在所述类中有前后注释,它们将执行。在我的例子中,我将它设置为之前将启动WebDriver实例的位置。我将功能从before方法转移到步骤定义类中的各个步骤中我不太确定这是这种情况下的问题所在。我修改了代码,删除了前后注释,并在步骤中烘焙了功能,但仍然得到相同的结果。因此,您已经编辑了代码。现在,让我们用这个更新版来编辑你的原始问题,这样我们都知道Arran,我更新了代码,并添加了一个编辑摘要。我不确定在这种情况下这是问题所在。我修改了代码,删除了前后注释,并在步骤中烘焙了功能,但仍然得到相同的结果。因此,您已经编辑了代码。现在,让我们用更新后的内容编辑您的原始问题,这样我们都知道Arran,我更新了代码,并添加了编辑摘要。
@Before
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
loginMap = PageFactory.initElements(driver, LoginMap.class);
}