Creating new windows/tabs in Selenium Java - Automation Script Walkthrough
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import java.util.ArrayList; import java.util.List; public class NewTabTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); try { // Step 1: Navigate to example.com driver.get("https://example.com"); // Step 2: Click link/button that opens new tab // Assuming the link has id 'newTabLink' wait.until(ExpectedConditions.elementToBeClickable(By.id("newTabLink"))).click(); // Step 3: Switch to new tab List<String> tabs = new ArrayList<>(driver.getWindowHandles()); // The new tab should be the last in the list driver.switchTo().window(tabs.get(1)); // Step 4: Verify URL of new tab wait.until(ExpectedConditions.urlToBe("https://example.com/newpage")); assert driver.getCurrentUrl().equals("https://example.com/newpage") : "New tab URL mismatch"; // Step 5: Switch back to original tab driver.switchTo().window(tabs.get(0)); // Step 6: Verify URL of original tab wait.until(ExpectedConditions.urlToBe("https://example.com")); assert driver.getCurrentUrl().equals("https://example.com") : "Original tab URL mismatch"; } finally { // Close all tabs and quit driver.quit(); } } }
This code uses Selenium WebDriver with Java to automate the test case.
We start by setting up the ChromeDriver and opening the browser to 'https://example.com'.
We wait until the element with id 'newTabLink' is clickable and click it to open a new tab.
We then get all window handles and switch to the new tab, which is the second window handle.
We wait until the URL of the new tab is 'https://example.com/newpage' and assert it matches.
Next, we switch back to the original tab and verify its URL is still 'https://example.com'.
Finally, we close all browser windows with driver.quit() to clean up.
Explicit waits ensure the page loads before assertions, and using window handles properly switches between tabs.
Now add data-driven testing with 3 different URLs that open in new tabs and verify each.
Practice
Solution
Step 1: Understand Selenium's new window/tab creation
Selenium provides a methoddriver.switchTo().newWindow()to open new tabs or windows.Step 2: Identify the correct WindowType for a tab
UsingWindowType.TABopens a new tab, which matches driver.switchTo().newWindow(WindowType.TAB).Final Answer:
driver.switchTo().newWindow(WindowType.TAB) -> Option AQuick Check:
Open new tab = driver.switchTo().newWindow(WindowType.TAB) [OK]
- Using non-existent methods like driver.openNewTab()
- Confusing WindowType.WINDOW with TAB
- Trying to create tabs without switchTo()
Solution
Step 1: Recall the correct method to open new windows
The correct method isdriver.switchTo().newWindow()with a WindowType argument.Step 2: Match the exact syntax
driver.switchTo().newWindow(WindowType.WINDOW); matches the correct syntax exactly withWindowType.WINDOW.Final Answer:
driver.switchTo().newWindow(WindowType.WINDOW); -> Option BQuick Check:
Open new window = driver.switchTo().newWindow(WindowType.WINDOW) [OK]
- Omitting switchTo() before newWindow()
- Using incorrect method names like openWindow()
- Missing semicolon in Java syntax
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://selenium.dev");
System.out.println(driver.getCurrentUrl());
driver.switchTo().window(originalHandle);
System.out.println(driver.getCurrentUrl());Solution
Step 1: Understand window handles and tab switching
The code opens example.com, saves the original window handle, then opens a new tab and navigates to selenium.dev.Step 2: Analyze the printed URLs
First print shows the URL of the new tab (selenium.dev). Then it switches back to the original window and prints its URL (example.com).Final Answer:
https://selenium.dev https://example.com -> Option CQuick Check:
Switch tabs = different URLs printed [OK]
- Assuming URLs remain the same after switching tabs
- Not saving original window handle before opening new tab
- Confusing getWindowHandle() with getWindowHandles()
driver.get("https://example.com");
driver.newWindow(WindowType.TAB);
driver.get("https://selenium.dev");Solution
Step 1: Check method usage for opening new tabs
The methodnewWindow()must be called afterdriver.switchTo().Step 2: Identify the missing part in the code
The code callsdriver.newWindow()directly, which is incorrect syntax.Final Answer:
Missing driver.switchTo() before newWindow() -> Option DQuick Check:
Always use switchTo().newWindow() [OK]
- Calling newWindow() directly on driver
- Confusing WindowType.TAB as invalid
- Thinking driver.get() is restricted after newWindow()
Solution
Step 1: Save original window handle before opening new tab
We must save the original window handle before opening a new tab to switch back later.Step 2: Open new tab, navigate, then switch back and assert title
String original = driver.getWindowHandle(); driver.switchTo().newWindow(WindowType.TAB); driver.get("https://selenium.dev"); driver.switchTo().window(original); assert driver.getTitle().contains("Example"); correctly callsswitchTo().newWindow(WindowType.TAB), navigates, switches back using saved handle, and asserts title.Final Answer:
Option A code snippet correctly performs all steps -> Option AQuick Check:
Save handle, open tab, switch back, assert [OK]
- Saving window handle after opening new tab
- Calling newWindow() without switchTo()
- Not switching back before assertion
Ready to prove it?
Pass the test and this pattern moves unknown
