How to capture Screenshots for failed test cases in Selenium using TestNG Listeners

Published: 05 December 2022
on channel: SDET Adda For QA Automation
6,740
106

If a script fails, we need to know where was the error in script. Solution for this is to capture a screenshot of webpage when the test case fails. We could easily identify where exactly the script got failed by seeing the screenshot.

Here are the steps to capture screenshot in Selenium using TestNG Listener:
1)Create a class. Implement TestNG #ITestListener
2)Call the method ‘onTestFailure’
3)Add the code to take a #screenshot with this method.

In this video, I have answered the following questions -
How do I take a screenshot of a failed scenario?
What is the code for screenshot using Selenium?
How do you handle failed test cases in TestNG?
How do I take a screenshot using listeners?
How do you rerun failed test cases 3 times?
How do I run a failed test case?

Moreover here is BaseClass.java which will have the code for capturing screenshot-

package screenshots;
import java.io.File;
import java.time.Duration;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class BaseClass {
public static WebDriver driver;

public static void initialize()
{ System.setProperty("webdriver.chrome.driver","C:\\SDETADDA\\chromedriver.exe");
driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("   / @sdetadda  ");
}
public void captureScreenshot(String methodname)
{
Date d = new Date();
String timestamp = d.toString().replace(":", "_").replace(" ", "");
try {
File file= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("C:\\SDETADDA\\"+methodname+timestamp+".jpg"));
}
catch (Exception e) {
e.getMessage();// TODO: handle exception
}
}
}

and ListenerClass.java which will be listening to failedcase -
package screenshots;

import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.Test;

public class ListenerClass extends BaseClass implements ITestListener {
@Test
public void onTestFailure(ITestResult result) {
System.out.println("Test is failed");
try {
captureScreenshot(result.getName());
}
catch (Exception e) {
e.getMessage();// TODO: handle exception
}
}
}
and Below is the Testcase.java

package screenshots;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

//@Listeners(screenshots.ListenerClass.class)
public class Testcase1 extends BaseClass{

@BeforeTest
public void setup()
{
initialize();
}
@Test
public void testMethod1() {

driver.findElement(null);
}
@AfterTest
public void tearDown()
{
driver.quit();
}
}

Here is the free Selenium and TestNG complete tutorial -    • TestNG with selenium4 - Develop and T...  


Watch video How to capture Screenshots for failed test cases in Selenium using TestNG Listeners online, duration hours minute second in high quality that is uploaded to the channel SDET Adda For QA Automation 05 December 2022. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 6,740 times and liked it 106 visitors.