[英]Selenium webdriver - why same code behave differently in different browser drivers
[英]Why is webdriver opening so many drivers?
當我開始測試時,將產生3-4個驅動程序,但實際上只有一個驅動程序可以運行測試。 我不要一個以上的驅動程序。 我正在使用intellij和一個maven項目。 我在硒上使用黃瓜-jvm。 我覺得我缺少一些簡單的東西,但是我無法指出問題所在。
版本:
硒2.42.2
黃瓜瓜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;
}
}
@Before
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
loginMap = PageFactory.initElements(driver, LoginMap.class);
}
@Before
將在每種測試方法之前運行,因此每次都會創建一個新的驅動程序。
您可能想嘗試@BeforeClass
我知道了,也許你在說Arran,但是我發現問題出在類而不是方法上。 我有幾個步驟定義類(每個類中有許多方法),當您執行一個cucumber-jvm測試時,cucumber-jvm似乎將加載所有這些步驟定義類,並且在所述類中是否存在注釋之前和之后,他們將執行。 就我而言,我已經將它設置為以前可以啟動WebDriver實例的位置。 我將功能從之前的方法移到了步驟定義類中的各個步驟中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.