Selenium Capture Screenshot on test failure or pass

Hi,

Please find the Screenshot listener example for Selenium RC Java and Testng.

public class Screenshot extends TestListenerAdapter {

@Override
public void onTestFailure(ITestResult result) {
log(LogLevel.INFO, “Test starting for “+getClass().getName());
File file = new File(“”);

Reporter.setCurrentTestResult(result);
System.out.println(file.getAbsolutePath());
Reporter.log(file.getAbsolutePath());

Reporter.log(“screenshot saved at ” + file.getAbsolutePath()+ “/selenium-reports/html/” + result.getName() + “.jpg”);
Reporter.log(“<a href='”+ file.getAbsolutePath()+”/selenium-reports/html/” + result.getName() + “.jpg’> <img src='”+ file.getAbsolutePath()+”/selenium-reports/html/”+ result.getName() + “.jpg’ height=’100′ width=’100’/> </a>”);
ResourceManager.selenium.captureScreenshot(file.getAbsolutePath()+ “/selenium-reports/html/” + result.getName() + “.jpg”);
Reporter.setCurrentTestResult(null);

//Reporter.log(“<a href=’http://www.w3schools.com/’>Visit W3Schools</a>”);
//org.testng.Reporter.log(“&lta href=’http://google.com’&lt/a&gt&#8221;);
//Reporter.log(“<a href=’../../” + result.getName() + “.jpg’> <img src=’../../”+ result.getName() + “.jpg’ height=’100′ width=’100’/> </a>”);
}

@Override
public void onTestSkipped(ITestResult result) {
// will be called after test will be skipped
}

@Override
public void onTestSuccess(ITestResult result) {
// will be called after test will pass
File file = new File(“”);
Reporter.setCurrentTestResult(result);
System.out.println(file.getAbsolutePath());

System.out.println(“Printing the Name of result getname”+result.getName());
Reporter.log(“<a href='”+ file.getAbsolutePath()+”/selenium-reports/html/” + result.getName() + “.jpg’> <img src='”+ file.getAbsolutePath()+”/selenium-reports/html/”+ result.getName() + “.jpg’ height=’100′ width=’100’/> </a>”);
ResourceManager.selenium.captureScreenshot(file.getAbsolutePath()+ “/selenium-reports/html/” + result.getName() + “.jpg”);
Reporter.setCurrentTestResult(null);
}

Let me know if you need any help

3 comments on “Selenium Capture Screenshot on test failure or pass

  1. Hi

    ResourceManager.selenium <– how to use this one.

    when i tried to follow your copy. look like we dont have any class for this one. I tried to import import org.apache.velocity.runtime.resource.ResourceManager; it doesnt not help, still error.

    Thanks for helping

    • Andrew ResourceManager is my Selenium Initialization class in which selenium is a static variable. So just either modify the the Resourcemanager and add your implementation to get selenium instance.

    • Andrew the Resource Manager is my custom class for setting up the instance of Selenium. In which as you can selenium is a Static variable. Hence the implementation follows this style. However you can use your existing instance and change resourcemanager to any name. Hope this helps

      public class ResourceManager{
      public static Selenium selenium;
      public static Selenium globalSeleniumInstance;

      /**
      * These parameter must be set inside testng.xml
      *
      * @param host
      * @param port
      * @param browser
      * Browser on which the tests will be executed
      * @param appPath
      */
      @BeforeSuite(alwaysRun = true)
      @Parameters({ “host”, “port”, “browser”, “app-path” })
      public void init(String host, String port, String browser, String appPath){
      org.testng.Reporter.log(“Initializing Selenium Instance”);
      log(LogLevel.INFO, “Test starting for “+getClass().getName());
      selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, appPath);
      globalSeleniumInstance = selenium;
      selenium.start();
      selenium.open(appPath);
      selenium.windowMaximize();
      selenium.windowFocus();
      }

Leave a reply to satishjohn Cancel reply