我有一個通過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