Latest post from this blog

Test Scenarios vs. Test Cases: Understanding the Basics

What Are Test Scenarios Test scenarios represent high-level ideas or conditions that need to be validated to ensure the application works as expected. They provide a broader perspective and are typically used during the test planning phase. Purpose: To capture the "what to test" without going into granular details. Example of a Test Scenario: Verify that a user can successfully log in to the application using valid credentials. Verify the behavior of the login page when invalid credentials are entered. Verify the application behavior when the login button is clicked without entering any credentials. What Are Test Cases Test cases are detailed documents that define the specific steps to execute a test. They cover inputs, execution steps, expected results, and actual outcomes. Purpose: To guide the tester step-by-step on "how to test." Example of a Test Case (for the first scenario): Test Case ID TC_01_Login_Valid_Credentials Test Scenario Verify user login with val...

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

Common Exception in Selenium

Test Scenarios vs. Test Cases: Understanding the Basics

Handling Shadow DOM in Selenium WebDriver

Handling HTTPS Websites and SSL Certificate Errors in Selenium

Normalize-space Function In Xpath