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 Open a New Tab in Incognito Mode in Selenium Automation

How to Open a New Tab in Incognito Mode in Selenium Automation

Benefits of using incognito mode in Selenium automation, such as testing without cookies, cache, or saved session data.

1. Using ChromeOptions:

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

public class IncognitoModeExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");

WebDriver driver = new ChromeDriver(options);
driver.get("https://example.com");
}
}

2. Opening a New Tab in Selenium:

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

public class NewTabInIncognito {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");

WebDriver driver = new ChromeDriver(options);
driver.get("https://example.com");

// Open a new tab
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open('https://google.com', '_blank');");
}
}

3.0pen a new Chrome browser instance from your Selenium automation and switch control to that new instance:


Step 1: Create a Utility Class for Chrome Instances
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class BrowserManager {
private WebDriver driver;

// Constructor to initialize a Chrome instance
public BrowserManager(boolean isIncognito) {
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");

ChromeOptions options = new ChromeOptions();
if (isIncognito) {
options.addArguments("--incognito"); // Enable incognito mode if required
}

driver = new ChromeDriver(options);
}

// Getter to retrieve the WebDriver instance
public WebDriver getDriver() {
return driver;
}

// Method to quit the browser
public void quitBrowser() {
if (driver != null) {
driver.quit();
}
}
}

Step 2: Use the Utility Class in Your Main Code

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class TestMultipleChromeInstances {
public static void main(String[] args) {
// Create the first Chrome instance
BrowserManager browser1 = new BrowserManager(false); // Normal mode
WebDriver driver1 = browser1.getDriver();
driver1.get("https://example.com");
System.out.println("First Chrome instance title: " + driver1.getTitle());

// Create the second Chrome instance
BrowserManager browser2 = new BrowserManager(true); // Incognito mode
WebDriver driver2 = browser2.getDriver();
driver2.get("https://google.com");
System.out.println("Second Chrome instance title: " + driver2.getTitle());

// Perform action in the second instance
WebElement searchBox = driver2.findElement(By.name("q")); // Locate Google search box
searchBox.sendKeys("Selenium WebDriver"); // Enter a search term
searchBox.submit(); // Submit the search

// Wait to observe results
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

// Print title of the second browser after action
System.out.println("Second Chrome instance title after search: " + driver2.getTitle());

// Quit both browsers
browser1.quitBrowser();
browser2.quitBrowser();
}
}

Utility Class (BrowserManager):

  • Encapsulates the logic for initializing Chrome browser instances.
  • Accepts a parameter (isIncognito) to customize browser options, e.g., enabling incognito mode.

Benefits:

  • Separation of Concerns: Initialization logic is isolated in the BrowserManager class.
  • Reusability: The BrowserManager class can be reused across your project.
  • Flexibility: Easily configure different browser options by extending the utility class.

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