Webdriver take screen shot when case failed use TestNG
2014-08-07 17:05
483 查看
Is there a good way to capture screenshots when running tests in parallel on the method level?
In order to run tests in parallel, each individual test needs a unique driver instance. So, at any given time you have x number of driver instances running. When it comes time to capture a screenshot, how do you determine which driver to use?
If you create a base test class with access to the driver, then that driver will always be the correct driver
The following will achieve this;
All test classes must extend a simple base test class;
In your listener, you need to access the base class;
In order to run tests in parallel, each individual test needs a unique driver instance. So, at any given time you have x number of driver instances running. When it comes time to capture a screenshot, how do you determine which driver to use?
If you create a base test class with access to the driver, then that driver will always be the correct driver
The following will achieve this;
All test classes must extend a simple base test class;
public asbtract baseTestCase() { private WebDriver driver; public WebDriver getDriver() { return driver; } @BeforeMethod public void createDriver() { Webdriver driver=XXXXDriver(); } @AfterMethod public void tearDownDriver() { if (driver != null) { try { driver.quit(); } catch (WebDriverException e) { System.out.println("***** CAUGHT EXCEPTION IN DRIVER TEARDOWN *****"); System.out.println(e); } } }
In your listener, you need to access the base class;
public class ScreenshotListener extends TestListenerAdapter { @Override public void onTestFailure(ITestResult result) { Object currentClass = result.getInstance(); WebDriver webDriver = ((BaseTest) currentClass).getDriver(); if (webDriver != null) { File f = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE); //etc. } }
相关文章推荐
- Webdriver take screen shot when case failed use TestNG
- How to take partial screenshot with Selenium WebDriver in python
- Webdriver MouseMove and Screen Shot
- How to take screenshot (thumbnail) of a web site with ASP.NET 2.0?
- The web application [ ] registered the JDBC driver [net.sourceforge.jtds.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver
- 基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure
- The web application registered the JDBC driver * but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
- registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
- registered the JBDC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. (转)
- 基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure
- registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.
- Take webpage screenshot from command line in Ubuntu Linux | Binary Tides
- Webdriver MouseMove and Screen Shot
- the web application registered the jbdc driver but failed unregistered it when application stoped
- application registered the JDBC driver [com.mysql] but failed to unregister it when the web app...
- html2canvas- Take Screenshot of Web Page and Save It to Server (Javascript and PHP)
- WebDriverException; Capture Page Screenshot
- [tomcat启动报错]registered the JDBC driver [com.alibaba.druid.proxy.DruidDriver] but failed to unregister it when the web application was stopped
- use case when on the outer join condition
- The DirectFB screen driver functionality test failed