gpt4 book ai didi

java - 我的 selenium 中打开了两个 Firefox 浏览器窗口

转载 作者:行者123 更新时间:2023-12-02 09:53:21 34 4
gpt4 key购买 nike

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

最佳答案

有一个例子,如何继承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")));
// ...
}
}

关于java - 我的 selenium 中打开了两个 Firefox 浏览器窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56155485/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com