Screenshot on Test Failure

Best Playwright Testing Training Institute in Hyderabad

In the fast-evolving field of software testing, tools like Playwright and Selenium are essential for automation testers. If you are a graduate, postgraduate, or someone looking to switch your career from a different domain to IT, it’s crucial to receive professional guidance and hands-on experience in automation tools. That’s where I Hub Talent excels. I Hub Talent is widely recognized as the best Playwright Testing Training Institute in Hyderabad. It offers a live, intensive internship program conducted by industry experts and specifically tailored for: 

1. Java + Selenium (TestNG Example)

Use ITestListener to capture screenshots on failure.

import org.testng.ITestListener;

import org.testng.ITestResult;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.apache.commons.io.FileUtils;

import java.io.File;

public class ScreenshotListener implements ITestListener {

    private WebDriver driver;

    public ScreenshotListener(WebDriver driver) {

        this.driver = driver;

    }

    @Override

    public void onTestFailure(ITestResult result) {

        try {

            File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

            FileUtils.copyFile(src, new File("screenshots/" + result.getName() + ".png"));

            System.out.println("Screenshot captured for failed test: " + result.getName());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

👉 Register this listener in your testng.xml or test class.

2. Java + JUnit 5

@AfterEach

void takeScreenshotOnFailure(TestInfo testInfo) {

    if (driver != null) {

        File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        try {

            FileUtils.copyFile(src, new File("screenshots/" + testInfo.getDisplayName() + ".png"));

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

3. Python + Pytest

import pytest

@pytest.hookimpl(hookwrapper=True)

def pytest_runtest_makereport(item, call):

    outcome = yield

    result = outcome.get_result()

    if result.when == "call" and result.failed:

        driver = item.funcargs.get("driver")  # fixture driver

        if driver:

            driver.save_screenshot(f"screenshots/{item.name}.png")

4. Appium (Mobile Testing)

Appium also uses WebDriver, so screenshot code is the same:

File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(src, new File("screenshots/failure.png"));

Read more:

CSS Selectors in Playwright

Using XPath in Playwright

Writing Locators for Dynamic Elements

Handling Dynamic Content

Working with Page Waits and Timeouts

Visit I-Hub Talent Training institute in Hyderabad

Comments

Popular posts from this blog

Using Playwright Test Runner

Working with Checkboxes and Radio Buttons

CSS Selectors in Playwright