猿问

在我的 selenium 中打开了两个 firefox 浏览器窗口

我正在使用 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");

    }


桃花长相依
浏览 199回答 1
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 {&nbsp; &nbsp; public static WebDriver driver;&nbsp; &nbsp; public static String driverPath = "C:\\Users\\pburgr\\Desktop\\selenium-tests\\FF_driver_0_23\\geckodriver.exe";&nbsp; &nbsp; public static WebDriver startFF() {&nbsp; &nbsp; &nbsp; &nbsp; FirefoxOptions options = new FirefoxOptions();&nbsp; &nbsp; &nbsp; &nbsp; ProfilesIni allProfiles = new ProfilesIni();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");&nbsp; &nbsp; &nbsp; &nbsp; options.setProfile(selenium_profile);&nbsp; &nbsp; &nbsp; &nbsp; options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("webdriver.gecko.driver", driverPath);&nbsp; &nbsp; &nbsp; &nbsp; driver = new FirefoxDriver(options);&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().window().maximize();&nbsp; &nbsp; &nbsp; &nbsp; return driver;&nbsp; &nbsp; }&nbsp; &nbsp; public static void shutdownFF() {&nbsp; &nbsp; &nbsp; &nbsp; driver.quit();&nbsp; &nbsp; }}包含驱动程序使用的方法的类: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 {&nbsp; &nbsp; @BeforeClass public static void setUpClass() {&nbsp; &nbsp; &nbsp; &nbsp; startFF();&nbsp; &nbsp; }&nbsp; &nbsp; @Before public void setUp() {}&nbsp; &nbsp; @After public void tearDown() {}&nbsp; &nbsp; @AfterClass public static void tearDownClass() {&nbsp; &nbsp; &nbsp; &nbsp; shutdownFF();&nbsp; &nbsp; }&nbsp; &nbsp; public WebDriverWait waitSec(WebDriver driver, int sec) {&nbsp; &nbsp; &nbsp; &nbsp; return new WebDriverWait(driver, sec);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; public WebElement byId(String id) {&nbsp; &nbsp; &nbsp; &nbsp; WebElement element = driver.findElement(By.id(id));&nbsp; &nbsp; &nbsp; &nbsp; return element;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public WebElement byXpath(String xpath) {&nbsp; &nbsp; &nbsp; &nbsp; WebElement element = driver.findElement(By.xpath(xpath));&nbsp; &nbsp; &nbsp; &nbsp; return element;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; public WebElement byText(String text) {&nbsp; &nbsp; &nbsp; &nbsp; WebElement element = driver.findElement(By.linkText(text));&nbsp; &nbsp; &nbsp; &nbsp; return element;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public WebElement clickableByXpath(String xpath, int sec) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WebElement element = waitSec(driver, sec).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return element;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; public WebElement clickableByName(String name, int sec) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WebElement element = waitSec(driver, sec).until(ExpectedConditions.elementToBeClickable(By.name(name)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return element;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public WebElement visibleByXpath(String xpath, int sec) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WebElement element = waitSec(driver, sec).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return element;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public WebElement visibleById(String id, int sec) {&nbsp; &nbsp; &nbsp; &nbsp;WebElement element = waitSec(driver, sec).until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));&nbsp; &nbsp; &nbsp; &nbsp;return element;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public List<WebElement> byXpaths(String xpath) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List<WebElement> elements = driver.findElements(By.xpath(xpath));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return elements;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public void atr2beByXpath(int sec, String xpath, String atr, String val) {&nbsp; &nbsp; &nbsp; &nbsp; waitSec(driver, sec).until(ExpectedConditions.attributeToBe(By.xpath(xpath), atr, val));&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public void atrNot2beByXpath(int sec, String xpath, String atr, String val) {&nbsp; &nbsp; &nbsp; &nbsp; waitSec(driver, sec).until(ExpectedConditions.not(ExpectedConditions.attributeToBe(By.xpath(xpath), atr, val)));&nbsp; &nbsp; }&nbsp; &nbsp; public void elements2beMoreByXpath(String xpath, int sec, int amount) {&nbsp; &nbsp; &nbsp; &nbsp; waitSec(driver, sec).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath(xpath), amount));&nbsp; &nbsp; }&nbsp; &nbsp; public void elements2beByXpath(String xpath, int sec, int amount) {&nbsp; &nbsp; &nbsp; &nbsp; waitSec(driver, sec).until(ExpectedConditions.numberOfElementsToBe(By.xpath(xpath), amount));&nbsp; &nbsp; }&nbsp; &nbsp; public void tryUrl2be(int sec, String url) {&nbsp; &nbsp; &nbsp; &nbsp; try {waitSec(driver, sec).until(ExpectedConditions.urlToBe(url));&nbsp; &nbsp; &nbsp; } catch (TimeoutException e) {}&nbsp; &nbsp; }&nbsp; &nbsp; public void tryUrl2contain(int sec, String string) {&nbsp; &nbsp; &nbsp; &nbsp; try {waitSec(driver, sec).until(ExpectedConditions.urlContains(string));&nbsp; &nbsp; &nbsp; } catch (TimeoutException e) {}&nbsp; &nbsp; }}测试类:package brucey;import org.junit.Test;import org.openqa.selenium.support.ui.ExpectedConditions;public class TestExample extends WebDriverBase {&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testExample() {&nbsp; &nbsp; &nbsp; &nbsp; driver.get("https://www.google.com");&nbsp; &nbsp; &nbsp; &nbsp; waitSec(driver, 10).until(ExpectedConditions.elementToBeClickable(byId("some WebElement's ID")));&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答