capture screenshot using testing

Capture Screenshot Using Testng!

Every automation test is expected to capture screenshots when any failure happened. All in all, luckily, Selenium provides a method to capture screenshots, and Testng provides useful listeners who manage the test events.
Basically, in this tutorial, we will learn how to capture screenshots on test failure using Testng listeners. Additionally, each step is explained in detail explanation. As a matter of fact, this tutorial will give you the real experience.


Refer below for step by step explanation to capture screenshots using testng:

Pre-Requisite:

It will be great if you complete the following steps:

  1. Java should be installed ( If Java is not installed then click on the link (Install & Setup Java)
  2. Selenium TestNG Project should be set up (Refer link if Selenium TestNG project is not set up – Setup Selenium TestNG)
  3. Basic understanding of the Screenshot Capture method (Refer to our blog if you need to understand the Basics of Screenshot capture)

Write a few classes for the following purposes:

  1. Base Class: In the Base class, we need to write the most frequent methods and variables that we need to use across multiple classes. Although, in this tutorial, we declared a static WebDriver by extending the Base Class.
  2. TestngListeners Class: This class implemented ITestListener methods and write screenshot capture code on test failure.
  3. Test Class (captutreScreenShotonFailure): Create a test class and write assertion code.
test class

Step 1: Open Eclipse and Selenium Project and create one Base class. Declare static WebDriver instance which can use across different classes.
Refer below for Base class Code:

package com.thoughcoders.automation;
import org.openqa.selenium.WebDriver;

public class Base {
public static WebDriver driver;
}
webdriver instance

Step 2: Create test class captutreScreenShotonFailure, extend the Base class to utilize WebDriver driver, and write test method:
Refer below for captutreScreenShotonFailure class code:

package com.thoughtcoders.automation.test;

import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import com. thoughtcoders.automation.Base;
import io.github.bonigarcia.wdm.WebDriverManager;

public class captutreScreenShotonFailure extends Base {
	
@Test
public void HomePageTest() {
		WebDriverManager.chromedriver().browserVersion("83.0").setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://thoughtcoders.com/");
System.out.println("Title of WebPage: "+ driver.getTitle());
Assert.assertEquals("aThoughtCoders | Web Development, Automation,"
 + " QA Training", driver.getTitle());
driver.close();
}
		
}

create test class

Step 3: Create a class to implement TestNG Listeners (TestngListeners) and implement ITestListner methods.
ITestListner: ITestListeners is the most popular TestNG interface which provides a very useful set of methods and by implementing these methods we perform the action on the Test Events. Refer below for the list of ITestListners
Override method on test failure and write code to capture screenshot and save in root. (If you need a detailed explanation of the screenshot capture code then refer to the link – https://thoughtcoders.com/blog/capture-screenshots-using-selenium-webdriver/
)

@Override
public void onTestFailure(ITestResult arg0) {
try {
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(System.getProperty("user.dir")+File.separator + 
				arg0.getName()+"_"+arg0.getEndMillis()+".png"));
System.out.println("*******Screenshot captured********");
} 
catch(Exception e) {
e.printStackTrace();
}
}

testng listeners

Step 5: Open Testng.xml and add the below step to integrate TestNG listeners.

Complete testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="listeners.TestngListeners"/>
</listeners>
  <test thread-count="5" name="Test">
    <classes>
      <class name="com.qaedupoint.automation.test.captutreScreenShotonFailure"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
completing testing.xml

Step 6- Run Test class explicitly- To run the test use @Listeners(className.class) annotation and pass the class name as an argument.
@Listeners(listeners.TestngListeners.class)

Listeners annotation usage

Step 7: Run Testng.xml and perform the test.
Console output:
Title of WebPage: ThoughtCoders | Web Development, Automation, QA Training
Screenshot captured*
Suite
Total tests run: 1, Failures: 1, Skips: 0
Afterward, test execution, right-click on the project and refresh. Now captured screenshot is visible and saved into the project root directory.

Output: Screenshot file

Congratulations, you have successfully captured a screenshot of the test failure. In conclusion, feel free to write us at info@thoughtcoders.com if you face any issues or need more information, you may also stay connected to us through social media: Facebook, Twitter, or LinkedIn!

Although this may be true that practical knowledge is a must, one can do wonders with our self practiced tutorials. Additionally, you can also have a look at our another blogs whether related to technology or latest updations in coding.

Write A Comment