为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

桃花长相依 2022-11-30 10:51:45
我正在使用 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");    }
查看完整描述

1 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

有一个例子,如何从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")));

        // ...

    }

}


查看完整回答
反对 回复 2022-11-30
  • 1 回答
  • 0 关注
  • 198 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信