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

  Рет қаралды 6,342

SDET Adda For QA Automation

SDET Adda For QA Automation

Күн бұрын

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 ...

Пікірлер
@testing24x7
@testing24x7 2 жыл бұрын
Understood now why it is important to use listener for taking taking screenshots. I liked this playlist.Keep uploading. Thanks.
@sdetadda
@sdetadda 2 жыл бұрын
Welcome
@Bug_Hunt
@Bug_Hunt Жыл бұрын
Nice tutorial!! Thank you so much ☺
@akeemolalere7915
@akeemolalere7915 9 ай бұрын
Nice tutorial SIR
@sdetadda
@sdetadda 8 ай бұрын
Keep watching
Extent Report 5 with screenshots and Gherkins scenario using TestNG
27:11
SDET Adda For QA Automation
Рет қаралды 2,9 М.
P26 - Capture screenshots for only failed tests in TestNG | TestNG |
47:47
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
P25 - TestNG Listeners | TestNG | Testing Framework | ITestListener |
55:35
TestNG Tutorial #16 - How to Capture Screenshot for Failed Tests in TestNG
18:22
Software Testing Mentor
Рет қаралды 32 М.
Selenium Cucumber BDD Framework with Java and TestNG - Page Object Model | POM
24:59
SDET Adda For QA Automation
Рет қаралды 10 М.
Taking ScreenShot ONLY for Failed Tests in Selenium using TestNG Listener
32:12
Naveen AutomationLabs
Рет қаралды 118 М.
Postman API Testing Tutorial for Beginners | Setup and Validate HTTP responses
23:23
SDET Adda For QA Automation
Рет қаралды 978
Take screenshot only for failed test cases in selenium WebDriver
16:45
Tutorials by Prachujya Saikia
Рет қаралды 7 М.
How to take screenshot of failed test cases in Selenium Webdriver
12:04
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.