我有一个通过testng运行并行测试的硒项目。当测试失败时,我有一个侦听器类捕获屏幕截图。类是如下Selenium截图侦听器捕获错误的浏览器
public class ScreenshotOnFailure extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult tr) {
WebDriver driver = SeleniumSetup.driverrunning;
boolean hasQuit = driver.toString().contains("(null)");
if(!hasQuit){
System.out.println(driver);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
DateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy__hh_mm_ssaa");
Date date = new Date();
String NewFileNamePath = null;
File directory = new File(".");
String methodName = tr.getMethod().getMethodName();
try {
NewFileNamePath =directory.getCanonicalPath() + "\\target\\surefire-reports\\html\\Screenshots\\"+methodName+"_"+ dateFormat.format(date) +"Listener.png";
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
FileUtils.copyFile(scrFile, new File(NewFileNamePath));
} catch (IOException e) {
e.printStackTrace();
}
String reportFilePath = ".\\Screenshots\\"+methodName+"_"+ dateFormat.format(date) +".png";
System.setProperty("org.uncommons.reportng.escape-output", "false");
Reporter.log("<a href=" + reportFilePath + ">Click to open screenshot</a><img src=" + reportFilePath + " height='350' width='700'>");
}
}}
在我的测试中,我已如果测试的运行清理测试
@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception
{
driver.quit();
}
一个AfterMethod逐一那么正确的浏览器截图但是如果我跑parrallel抓获testsit捕获错误的测试浏览器。 我认为这个问题可能是以下
- 的之一后,方法已经退出浏览器(这是一个情况下 有时因此为什么我添加的hasQuit布尔)
- 的listene被引用错误的驱动对象。我相信这是问题,但我不确定如何确保它引用正确的驱动程序。
我有一个解决方法,工作几乎invlaves创建一个静态屏幕捕获对象,然后将其添加到AfterMethod然而,这是不太理想的,因为我想使用一个侦听器。
你确定硒是专注于正确的窗口吗?如果您打开一个新选项卡(例如),硒将继续使用旧选项卡,除非您明确切换到新选项卡。当您拉动驱动程序运行时,您可能会拉动第一个或最后一个启动的驱动程序。 – Brydenr
@Brydenr是的,我相信它可能会拉动最后一名驾驶员。我不知道如何让它拉到正确的驱动程序。有任何想法吗? –
切换标签,你使用窗口句柄http://stackoverflow.com/questions/19112209/how-to-handle-the-new-window-in-selenium-webdriver-using-java – Brydenr