How to capture a screenshot in tests?

    IHUB Talent: The Best Playwright Testing Training in Hyderabad with Live Internship

IHUB Talent stands out as the best institute for Playwright Testing Training in Hyderabad, offering a comprehensive, hands-on learning experience that prepares you for a successful career in test automation. Playwright, a cutting-edge testing framework for web applications, has gained immense popularity for its ability to handle modern web apps with speed and reliability. Our training program ensures that students gain expertise in Playwright for automated testing using JavaScript, TypeScript, and Python.

The course is meticulously designed to cover all aspects of Playwright Testing, starting from the fundamentals to advanced concepts. Students will learn how to set up and use Playwright for end-to-end testing, explore browser automation, and work with advanced tools like Playwright Inspector. Practical training on integrating Playwright with CI/CD pipelines and various testing frameworks such as Mocha, Jest, and others ensures that students are well-prepared for real-world automation challenges.

In Playwright, you can locate elements using a variety of methods, similar to Selenium. Playwright supports several ways to find elements on a web page, including CSS selectorsXPath, and text-based selectors. Here’s an overview of the main techniques to locate elements in Playwright, with examples.

In Playwright, the method page .wait For Selector() plays an important role in waiting for an element to appear (or become visible) on the page before interacting with it. 

Capturing a screenshot during automated tests is a useful way to debug and document the state of a web page when a test runs — especially when failures occur.


✅ Capturing a Screenshot in Selenium (Python)

You can use the get_screenshot_as_file() or save_screenshot() methods provided by the WebDriver.


๐Ÿ“Œ Basic Example:

python

Copy

Edit

from selenium import webdriver


driver = webdriver.Chrome()

driver.get("https://example.com")


# Capture screenshot and save it as a PNG file

driver.save_screenshot("screenshot.png")

or


python

Copy

Edit

driver.get_screenshot_as_file("screenshot.png")

๐Ÿ“ Saving with a Timestamp (Useful in Tests):

python

Copy

Edit

import time


timestamp = time.strftime("%Y%m%d-%H%M%S")

driver.save_screenshot(f"screenshot_{timestamp}.png")

๐Ÿ”„ Use in Test Framework (e.g., pytest)

In a testing scenario, like inside a pytest test function, you can capture a screenshot when a test fails:


python

Copy

Edit

def test_example(driver):  # 'driver' is a Selenium WebDriver fixture

    try:

        driver.get("https://example.com")

        assert "Example Domain" in driver.title

    except AssertionError:

        driver.save_screenshot("test_example_failed.png")

        raise

๐Ÿ”น Tips

Screenshots are saved in the current working directory unless a path is specified.

Use screenshots to capture visual bugs, layout issues, or unexpected behaviors.

Integrate screenshots with test reports (e.g., Allure or HTML reports) for better traceability.

If you’re using a different language (Java, C#, etc.) or framework (Cypress, Playwright), I can give you tailored guidance.

Read More

Comments

Popular posts from this blog

How to locate elements in Playwright tests?

How do you perform assertions in Playwright tests?

How do you handle pop-ups or dialogs in Playwright?