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 Run Automation Scripts in Headless Mode Using Selenium

Introduction
Headless browsers enable selenium to execute tests without a GUI(graphical user interface), making them ideal for CI/CD pipelines and server environment.

Libraries such as HTMLUnitDriver, Headless Chrome, and Headless Firefox enable quicker and more efficient test execution while maintaining full browser compatibility. Utilizing headless mode in Selenium helps testers streamline their testing workflows.

Step-by-Step Guide to Run Chrome in Headless Mode
Chrome can run headless mode, providing full browser capabilities without a GUI

//Set Chrome options
ChromeOptions options=
new ChromeOptions();
//Run in headless mode
 options.addArguments("--headless")

Example code

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class HeadlessChromeExample {

public static void main(String[] args) {
// Set the path to the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

// Set Chrome options
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless"); // Run in headless mode
options.addArguments("--disable-gpu"); // Disable GPU acceleration
options.addArguments("--no-sandbox"); // Bypass OS security model
options.addArguments("--window-size=1920x1080"); // Set window size

// Initialize the WebDriver with Chrome options
WebDriver driver = new ChromeDriver(options);

// Navigate to the desired URL
driver.get("https://enter your application url.com"); // Change to your application's URL

// Perform your automation tasks
System.out.println("Title of the page: " + driver.getTitle());

// Close the driver
driver.quit();
}
}

Step-by-Step Guide to Run Firefox in Headless Mode
Firefox can also be run in headless mode, similar to Chrome 

// Set Firefox options for headless mode
 FirefoxOptions options = new FirefoxOptions();
// Run in headless mode
 options.setHeadless(true); 

Example code

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class HeadlessFirefoxExample {

public static void main(String[] args) {
// Set the path to the GeckoDriver executable
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");

// Set Firefox options for headless mode
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true); // Run in headless mode

// Initialize the WebDriver with Firefox options
WebDriver driver = new FirefoxDriver(options);

// Navigate to the target URL
driver.get("https://example.com"); // Replace with your application's URL

// Perform your testing actions
System.out.println("Page Title: " + driver.getTitle());

// Close the WebDriver
driver.quit();
}
}
Conclusions 
Headless browsers enhance automation testing by speeding up execution and integrating smoothly into CI/CD pipelines. Understanding headless mode in Selenium is vital for efficient testing.

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