2016-09-23 196 views
1

我有一個通過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然而,這是不太理想的,因爲我想使用一個偵聽器。

+0

你確定硒是專注於正確的窗口嗎?如果您打開一個新選項卡(例如),硒將繼續使用舊選項卡,除非您明確切換到新選項卡。當您拉動驅動程序運行時,您可能會拉動第一個或最後一個啓動的驅動程序。 – Brydenr

+0

@Brydenr是的,我相信它可能會拉動最後一名駕駛員。我不知道如何讓它拉到正確的驅動程序。有任何想法嗎? –

+0

切換標籤,你使用窗口句柄http://stackoverflow.com/questions/19112209/how-to-handle-the-new-window-in-selenium-webdriver-using-java – Brydenr

回答

3

從您的代碼WebDriver driver = SeleniumSetup.driverrunning看來,driverrunning是SeleniumSetup類中的靜態驅動程序實例。所以,在並行執行中它可能會引用一個錯誤的驅動程序對象。

ThreadLocal可能會幫助您創建一個線程安全驅動程序對象,下面是一個示例。

public class DriverFactory 
{ 

    private DriverFactory() 
    { 
     //Do-nothing..Do not allow to initialize this class from outside 
    } 
    private static DriverFactory instance = new DriverFactory(); 

    public static DriverFactory getInstance() 
    { 
     return instance; 
    } 

    ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>() // thread local driver object for webdriver 
    { 
     @Override 
     protected WebDriver initialValue() 
     { 
     return new FirefoxDriver(); // can be replaced with other browser drivers 
     } 
    }; 

    public WebDriver getDriver() // call this method to get the driver object and launch the browser 
    { 
     return driver.get(); 
    } 

    public void removeDriver() // Quits the driver and closes the browser 
    { 
     driver.get().quit(); 
     driver.remove(); 
    } 
} 

使用DriverFactory來獲取驅動程序實例。

WebDriver driver = DriverFactory.getInstance().getDriver(); 
+0

你必須傳遞驅動程序名稱爲testng配置文件中的一個參數,這可能對你有所幫助。 http://stackoverflow.com/questions/26604745/parameterized-selenium-tests-in-parallel-with-testng –

+0

感謝您的意見。我實施了這個課程,它很有魅力。我還添加了一個setter方法,以便可以基於瀏覽器類型等構建驅動程序。 –

相關問題