Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

How to Capture Screenshots for Failed Test Cases in Selenium

Before you start capturing screenshots of failed test cases in automated testing, Understanding Most Common Reasons for Test Case Failures

1. The locator used to find a web element is incorrect or outdated, causing Selenium to be unable to locate the element on the page.

2. The application may not be fully loaded when the test tries to interact with elements, leading toStaleElementReferenceException or NoSuchElementException

3. Updates or changes to the application’s UI or functionality can break existing tests, making them fail unexpectedly.

Capturing screenshots for failed test cases in Selenium WebDriver is essential for debugging and understanding why test cases fail. 

ExampleCode:
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.apache.commons.io.FileUtils; // You need Apache Commons IO dependency for this

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

public class TakeScreenshotForFailedTestCase {

private WebDriver driver;

public void setUp() {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
driver = new ChromeDriver();
}

public void tearDown() {
driver.quit();
}

public void captureScreenshot(String testName) {
// Capture screenshot
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
// Save the screenshot to a specific location
FileUtils.copyFile(screenshot, new File("screenshots/" + testName + ".png"));
} catch (IOException e) {
System.out.println("Failed to save screenshot: " + e.getMessage());
}
}

public void testExample() {
setUp();
try {
driver.get("https://example.com");
// Simulating a test failure
if (driver.getTitle().equals("Expected Title")) {
System.out.println("Test Passed");
} else {
throw new Exception("Title did not match");
}
} catch (Exception e) {
System.out.println("Test Failed: " + e.getMessage());
captureScreenshot("testExample"); // Capture screenshot on failure
} finally {
tearDown();
}
}

public static void main(String[] args) {
TakeScreenshotForFailedTestCase example = new TakeScreenshotForFailedTestCase();
example.testExample();
}
}

TakesScreenshot Interface: This interface allows you to capture screenshots in Selenium WebDriver.

OutputType.FILE: Specifies the output type as a file.

Apache Commons IO: The FileUtils.copyFile() method from Apache Commons IO is used to save the screenshot. 

Add dependency 
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version> <!-- Use the latest version -->
</dependency>
I believe the content of this blog article will be beneficial for your interview success and for enhancing your automation test framework.

Comments

Popular posts from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

Purpose of the StepData Class in Selenium + Java Cucumber Automation Framework

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java