@kovid,嗨。
让我们尝试一步步调试它:
您也可以分享一下吗testng.XML
套件如何组织和共享测试代码您正在尝试执行吗?
从我看到的异常中 - 它试图访问浏览器实例,但由于某种原因它不可访问。
可能会被称为
driver.close();
or
driver.quit();
代码中的某个地方。
关于 testNG 配置 - 你可以看看框架规格 https://testng.org/doc/index.html和/或example https://docs.experitest.com/display/public/TA/TestNG代表测试套件的组织方式。
希望对您有帮助,
问候,
尤金
=============更新2019/03/02除了上面提供的显式版本控制 @debanjanB ^^ 之外,我建议按以下方式重新编写代码:
改进#1驱动程序初始化(我已将此方法与适当的配置分开;我个人使用的是 selenium GRID ,但您可能会继续使用您在代码中使用的 Chromedriver,我想说这是习惯问题):
public static WebDriver driverSetUp(WebDriver driver) throws MalformedURLException {
ChromeOptions options = new ChromeOptions();
options.addArguments("-incognito");
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(ChromeOptions.CAPABILITY, options);
//System.setProperty("webdriver.chrome.driver", System.getProperty("user.home")+"/Documents/:Proj_folder:/chromedriver");
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
capability.setBrowserName("chrome");
capability.setCapability("nativeEvents", true);
LoggingPreferences logs = new LoggingPreferences();
//Javascript console logs from the browser
logs.enable(LogType.BROWSER, Level.WARNING);
logs.enable(LogType.PERFORMANCE, Level.ALL);
capability.setCapability(CapabilityType.LOGGING_PREFS, logs);
String webDriverURL = "http://" + environmentData.getHubIP() + ":" + environmentData.getHubPort() + "/wd/hub";
log.info("creating driver instance on the URL :#### " + webDriverURL);
driver = new RemoteWebDriver(new URL(webDriverURL), capability);
driver.manage().window().maximize();
return driver;}
public static WebDriver driverInit(WebDriver driver, String startingUrl) throws MalformedURLException {
driver = DriverInit.driverSetUp(driver);
driver.get(startingUrl);
return driver;
}
如果您需要执行驱动程序重新启动操作 - 您可以使用驱动程序初始化driver.close()
包裹在 try-catch 块中
public static WebDriver driverRestart(WebDriver driver, String startingUrl) throws MalformedURLException {
try {
driver.close();
} catch (WebDriverException e) {
log.error("#### oops, seems driver instance have been already closed. Doing re-initialization right now!", e.getMessage(), e);
}
return driverInit(driver, startingUrl);
}
So 后法看起来像(请添加'alwaysRun=true' flag):
@AfterMethod(alwaysRun = true)
public void closeDriverInstance() {
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(
String.format("c:\\SeleniumScreenshots\\%s.jpg", count)));
driver.close();
} catch (Exception e) {
log.info(" oops, it seems that driver instance have been already closed.");
}
}
改进#2
请稍微修改 testng XML(请添加并行=“假”在套件标签中明确标记):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="CustomerPortalTestSuite-SIT2" parallel="false" thread-count="5" verbose="8">
<test name="CustomerPortalTestExecution">
<classes>
<class name="testclasses.CustomerPortalSupportTest"/>
<class name="testclasses.CustomerPortalAccountTest"/>
<class name="testclasses.CustomerPortalBillingTest"/>
<class name="testclasses.CustomerPortalHomeTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
改进#3(假设)
如果这没有帮助。根据经验,如果对于某些测试 chrome 实例启动并多次关闭并重新启动 - 您可能仍然有一堆chromedriver.exe进程(挂在您的系统中并阻止新的 chromedriver 处理启动)
因此,如果您发现您有很多进程,要在 WIN 中杀死它们,您可以调用以下命令:
Taskkill /T /F /IM chromedriver.exe
因此,考虑到您每次在每个测试方法之后都会“杀死”您的 chrome,您还可以在方法之后杀死任何 chromedriver 进程(已解释)here https://stackoverflow.com/a/15464449/1546574如何执行包含在java代码中的cmd)。
希望最终能有所帮助:)
问候,
尤金