0
0
Selenium Javatesting~10 mins

Screenshot attachment on failure in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage, tries to find a button, clicks it, and verifies the page title. If the test fails, it takes a screenshot and saves it for debugging.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class ScreenshotOnFailureTest {
    private WebDriver driver;

    @Before
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testButtonClickAndTitle() throws IOException {
        driver.get("https://example.com");
        WebElement button = driver.findElement(By.id("start-button"));
        button.click();
        String title = driver.getTitle();
        try {
            Assert.assertEquals("Expected Page Title", title);
        } catch (AssertionError e) {
            takeScreenshot("failure_screenshot.png");
            throw e;
        }
    }

    private void takeScreenshot(String fileName) throws IOException {
        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileHandler.copy(srcFile, new File(fileName));
    }

    @After
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.comBrowser displays the example.com homepage-PASS
3Finds element with id 'start-button'Button element is located on the page-PASS
4Clicks the 'start-button'Page reacts to button click, possibly navigates or updates-PASS
5Gets the page title after clickPage title is retrieved-PASS
6Checks if page title equals 'Expected Page Title'Title is compared to expected stringAssert.assertEquals("Expected Page Title", title)FAIL
7On assertion failure, takes screenshot and saves as 'failure_screenshot.png'Screenshot file is created in test folder-PASS
8Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Page title does not match 'Expected Page Title' after clicking the button
Execution Trace Quiz - 3 Questions
Test your understanding
What happens immediately after the test detects the page title is incorrect?
AA screenshot is taken and saved
BThe browser closes immediately
CThe test retries clicking the button
DThe test ignores the failure and continues
Key Result
Taking a screenshot only on test failure helps quickly understand what went wrong without cluttering storage with unnecessary images.