0
0
Selenium Javatesting~15 mins

Creating new windows/tabs in Selenium Java - Automation Script Walkthrough

Choose your learning style9 modes available
Verify opening a new tab and switching between tabs
Preconditions (3)
Step 1: Open the browser and navigate to 'https://example.com'
Step 2: Click on the link or button that opens a new tab
Step 3: Switch the WebDriver context to the new tab
Step 4: Verify the URL of the new tab is 'https://example.com/newpage'
Step 5: Switch back to the original tab
Step 6: Verify the URL of the original tab is 'https://example.com'
✅ Expected Result: The new tab opens with the correct URL, and switching between tabs works as expected.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert the URL of the new tab is 'https://example.com/newpage'
Assert the URL of the original tab is 'https://example.com'
Best Practices:
Use explicit waits to handle page load
Use WebDriver's window handles to switch tabs
Close tabs properly after test
Use Page Object Model if applicable
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Hardcoding window handles instead of retrieving them dynamically
Not switching back to the original tab after working on the new tab
Not closing the browser or tabs after test completion
Bonus Challenge

Now add data-driven testing with 3 different URLs that open in new tabs and verify each.

Show Hint