我正在使用firefox驱动程序,并且我注意到由于初始化了firefox驱动程序的新实例,因此在运行测试时会打开两个fireFox窗口。在初始化驱动程序方面是否有正确的方法,因为我可能错了,但是我想我不应该在两个位置写入WebDriver webDriver = new FirefoxDriver();
并以某种方式只在一个位置写入并调用它吗?
第1页:
public class waitMethods extends PageObject {
WebDriver webDriver = new FirefoxDriver();
public void waitForElementToBeDisplayed(By element){
try {
WebDriverWait webDriverWait = new WebDriverWait(webDriver, 30);
webDriverWait.until(ExpectedConditions.presenceOfElementLocated(element));
System.out.println(element + " is displayed correctly");
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
System.out.println(element + " is not displayed");
}
}
第2页:
public class WebPageMethods extends PageObject {
WebDriver webDriver = new FirefoxDriver();
public void navigateToAuth0WebPage(){
webDriver.get("https://www.test.com");
}
答案 0 :(得分:1)
有一个示例,说明如何从WebDriver继承:
WebDriver设置类:
package brucey;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class WebDriverSetup {
public static WebDriver driver;
public static String driverPath = "C:\\Users\\pburgr\\Desktop\\selenium-tests\\FF_driver_0_23\\geckodriver.exe";
public static WebDriver startFF() {
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
return driver;
}
public static void shutdownFF() {
driver.quit();
}
}
包含驱动程序使用的方法的类:
package brucey;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WebDriverBase extends WebDriverSetup {
@BeforeClass public static void setUpClass() {
startFF();
}
@Before public void setUp() {}
@After public void tearDown() {}
@AfterClass public static void tearDownClass() {
shutdownFF();
}
public WebDriverWait waitSec(WebDriver driver, int sec) {
return new WebDriverWait(driver, sec);
}
public WebElement byId(String id) {
WebElement element = driver.findElement(By.id(id));
return element;
}
public WebElement byXpath(String xpath) {
WebElement element = driver.findElement(By.xpath(xpath));
return element;
}
public WebElement byText(String text) {
WebElement element = driver.findElement(By.linkText(text));
return element;
}
public WebElement clickableByXpath(String xpath, int sec) {
WebElement element = waitSec(driver, sec).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
return element;
}
public WebElement clickableByName(String name, int sec) {
WebElement element = waitSec(driver, sec).until(ExpectedConditions.elementToBeClickable(By.name(name)));
return element;
}
public WebElement visibleByXpath(String xpath, int sec) {
WebElement element = waitSec(driver, sec).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
return element;
}
public WebElement visibleById(String id, int sec) {
WebElement element = waitSec(driver, sec).until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
return element;
}
public List<WebElement> byXpaths(String xpath) {
List<WebElement> elements = driver.findElements(By.xpath(xpath));
return elements;
}
public void atr2beByXpath(int sec, String xpath, String atr, String val) {
waitSec(driver, sec).until(ExpectedConditions.attributeToBe(By.xpath(xpath), atr, val));
}
public void atrNot2beByXpath(int sec, String xpath, String atr, String val) {
waitSec(driver, sec).until(ExpectedConditions.not(ExpectedConditions.attributeToBe(By.xpath(xpath), atr, val)));
}
public void elements2beMoreByXpath(String xpath, int sec, int amount) {
waitSec(driver, sec).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath(xpath), amount));
}
public void elements2beByXpath(String xpath, int sec, int amount) {
waitSec(driver, sec).until(ExpectedConditions.numberOfElementsToBe(By.xpath(xpath), amount));
}
public void tryUrl2be(int sec, String url) {
try {waitSec(driver, sec).until(ExpectedConditions.urlToBe(url));
} catch (TimeoutException e) {}
}
public void tryUrl2contain(int sec, String string) {
try {waitSec(driver, sec).until(ExpectedConditions.urlContains(string));
} catch (TimeoutException e) {}
}
}
测试课程:
package brucey;
import org.junit.Test;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class TestExample extends WebDriverBase {
@Test
public void testExample() {
driver.get("https://www.google.com");
waitSec(driver, 10).until(ExpectedConditions.elementToBeClickable(byId("some WebElement's ID")));
// ...
}
}