selenium - 在测试之间重新启动浏览器
问题描述
如何在测试之间重新启动浏览器?我有 DriveErFactory 并在构造函数的 BasePage 中调用
public BasePage() {
this.driver = DriverFactory.getDriver();
this.wait = new WebDriverWait(getDriver(), waitTime);
}
然后在我的测试中我有:
@BeforeMethod
public void beforeTest() {
loginPage.open();}
和
@AfterMethod
public void afterTest() {
loginPage.quitDriver();
}
第一次测试后,我得到: org.openqa.selenium.NoSuchSessionException:会话 ID 为空。调用 quit() 后使用 WebDriver?
如果我在之后使用:
loginPage.closeDriver();
我有: org.openqa.selenium.NoSuchSessionException:没有这样的会话
在BeforeTest WebDriver 中可以启动,为什么他在AfterTest之后不启动?
如何做得更好?每次测试前如何重启浏览器?
驱动工厂
public class DriverFactory {
static PropertyBase propertyBase = new PropertyBase();
Properties prop = new Properties();
private static WebDriver driver;
private static void setChromeDriverPath() {
String chromeDriverPath = ".\\resources\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
}
private static void setFireFoxDriverPath() {
String chromeDriverPath = ".\\resources\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver", chromeDriverPath);
}
private static void configureDriver() {
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
}
private static FirefoxProfile setFireFoxProfile() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "./downloads");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;");
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference( "pdfjs.disabled", true );
return profile;
}
private static ChromeOptions setChromeProfile() {
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.prompt_for_download", true);
prefs.put("download.default_directory", "./download");
options.setExperimentalOption("prefs", prefs);
return options;
}
private static WebDriver createChromeDriver(){
setChromeDriverPath();
return driver = new ChromeDriver(setChromeProfile());
}
private static WebDriver createFireFoxDriver(){
setFireFoxDriverPath();
return driver = new FirefoxDriver(setFireFoxProfile());
}
public static WebDriver getDriver() {
String browserType = propertyBase.getProperty("browser");
switch (browserType) {
case "chrome":
if (driver==null) {
driver = createChromeDriver();
}
break;
case "firefox":
if (driver==null) {
createFireFoxDriver();
}
break;
}
configureDriver();
return driver;
}
解决方案
您有一种从工厂获取驱动程序对象的方法。也许你也应该在那里制定一个方法来关闭它。您现在在登录页面上调用它。这似乎不太合乎逻辑。然而,重要的可能是使用 driver.quit() 或 driver.close() 关闭驱动程序,然后确保设置 driver=null;
这样,当您运行新测试时,将调用 getDriver 并且 if (driver == null) 确保实例化一个新对象。
如果我的假设是正确的,那么这应该可以解决问题,否则请在 loginPage.quitDriver() 中发布您拥有的任何内容。
像这样的东西,在 DriverFactory 类中
public static void quitDriver() {
driver.quit();
driver = null;
}
编辑:错别字
推荐阅读
- php - 多页调查问卷的控制器数量 Laravel / PHP
- php - 从数组 2 中获取与数组 1 相比的唯一值并在数组 3 中输出
- ios - SwiftUI:仅当 ScrollView 超过屏幕高度时才可滚动
- reactjs - 为什么 Jest 无法运行引用 npm 包的测试?
- python - 零测试用例的代码覆盖率很高
- python - 为什么这个 scipy 傅立叶积分函数不起作用?
- python - Python 错误:TypeError:不支持的操作数类型 -:'int' 和 'function'
- kubernetes - 如何访问同一节点中的 Pod?(舵)
- python - 在使用条件聚合进行分组时选择多个第 n 个值 - 熊猫
- mongodb - 不匹配 $ 作为正则表达式中的字符串