请注意:请要求人们不要将其标记为重复或类似内容。请所有人阅读问题。
我遇到了需要在自动化框架中集成硒网格的任务: 我有以下课程:
DriverFactory
DriverManager
除此之外,我在drivers文件夹中定义了以下内容。
selenium standalone server jar
hub.json
node1.json
chromedriver.exe
DriverManager类:
public class DriverManager {
public static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
public static WebDriver getDriver() {
return driver.get();
}
public static void setDriver(WebDriver drive) {
driver.set(drive);
}
public static void removeDriver() {
if (driver.get() != null) {
driver.get().quit();
driver.remove();
}
}
}
在这里,我需要将WebDriver替换为RemoteWebDriver,但是当我这样做时,我会遇到错误。
DriverFactory类:
public class DriverFactory {
public static WebDriver createInstance(String browser) {
if (browser.equals("chrome")) {
ChromeDriver driver = new ChromeDriver(getChromeOptions());
try {
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
}
catch (Exception e) {
}
return driver;
}
return null;
}
public static void killBrowserInstance(String browser) throws InterruptedException {
try {
if (browser.equals("chrome")) {
if (OsUtils.isWindows()) {
Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
}
else {
Runtime.getRuntime().exec("killall \"chromedriver\"");
Runtime.getRuntime().exec("killall \"Google Chrome\"");
}
}
Thread.sleep(1000);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static ChromeOptions getChromeOptions() {
if (OsUtils.isWindows()) {
System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe");
}
else {
System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver");
}
ChromeOptions options = new ChromeOptions();
return options;
}
}
请忽略import语句。 如果您需要更多信息,请告诉我,即使能够远程解决该问题也可以用于屏幕共享。
示例测试案例:
@Test
public void LoginValid() throws Throwable {
try {
loginpage.login(prop.getProperty("username"), prop.getProperty("password"));
// Logout
} catch (Exception e) {
Logz.step(LogStatus.FAIL, "Unable to Login");
e.printStackTrace();
throw e;
}
}
当我触发testng时,内部会在创建驱动程序初始化和实例的地方进行调用。
public synchronized void onTestStart(ITestResult iTestResult)
DriverManager.setDriver(DriverFactory.createInstance(iTestResult.getTestContext().getCurrentXmlTest().getLocalParameters().get("browser")));
}