Browser Commands in Selenium Webdriver Java

When you manually browse the web, you don’t just interact with buttons or text fields on a page. Your interaction starts much earlier: you launch the browser application, type a URL into the address bar, resize the window to see content clearly, or use the “Back” and “Forward” arrows to navigate through your history. Finally, when you are finished, you close the specific tab or shut down the browser entirely.

In test automation, Browser Commands are the specific instructions that replicate these high-level browser actions.

They are the foundational layer of any Selenium script. Before your code can find a specific element inside a webpage (like a login button), it must first successfully control the browser environment itself. You need to instruct the WebDriver to open the application, manage the window state, and ensure the session is terminated correctly when testing is complete.

If you skip mastering these basics, you risk creating scripts that cannot navigate effectively, fail to load pages properly, or leave “zombie” browser processes running in the background, consuming your system’s memory.

This guide covers every essential browser command in Selenium, explaining not just how to use them, but why and when to use them.

Let’s go through the points above one by one.

The first action in 99% of automation scripts is navigating to the application under test (AUT).

This is the most fundamental command in Selenium.

Syntax:

driver.get("String URL");

What it does: It instructs the browser to open the specified URL.

How it works: It doesn’t just “hit enter” and move on. Selenium is smart; the get() command automatically waits until the page has fully loaded (specifically, until the onload event fires) before giving control back to your script.

Why use it: It is the safest way to start a test because it ensures the page is ready before you try to find any elements.

// Opening the TestKru practice page
driver.get("https://testkru.com/Elements/TextFields");

Syntax:

driver.navigate().to("String URL");

What it does: Functionally, it does the exact same thing as get(), it loads the URL.

The Difference: While get() is a direct command to load a page, navigate().to() is part of the Navigation interface. It is designed to mimic the action of a user typing a URL into the address bar while maintaining the browser’s history. This allows you to use back() and forward() commands later.

Expert Note: In modern Selenium, get() and navigate().to() are virtually interchangeable. However, convention suggests using get() for the initial page load and navigate().to() if you are changing URLs in the middle of a test case.

Real users don’t just stay on one page; they click “Back” to correct a mistake or “Forward” to return to where they were. Selenium simulates this behavior using the Navigation interface.

Note: To use the commands below, you must first navigate to a URL using the navigate.to() method; otherwise, these commands may not work as expected.

  • Action: Simulates clicking the Back arrow (<) in the browser.
  • Use Case: Imagine testing an e-commerce site. You click a product, go to the details page, and then want to verify that clicking “Back” returns you to the search results with your filters still applied.
back arrow in text field page
  • Action: Simulates clicking the Forward arrow (>) in the browser.
  • Constraint: This command only works if you have previously navigated back. If you are at the “top” of your history stack, this command does nothing.
  • Action: Simulates clicking the Reload button or pressing F5.
  • Why is this dangerous? Beginners often face errors here. When you refresh a page, the browser destroys the current webpage and redraws it from scratch.
  • The Risk: If you found an element (like a button) before the refresh, and try to click it after the refresh, your script will crash with a StaleElementReferenceException. You must find the element again after refreshing.
// 1. Start at TestKru
driver.get("https://testkru.com/Elements/TextFields");

// 2. Go to Google (History is now: TestKru -> Google)
driver.navigate().to("https://google.com");

// 3. Go Back (Current Page: TestKru)
driver.navigate().back();

// 4. Go Forward (Current Page: Google)
driver.navigate().forward();

// 5. Reload the page
driver.navigate().refresh();

Automation is useless without validation. You need to confirm the browser is actually where you think it is.

  • Purpose: Retrieves the text displayed on the browser tab (the <title> tag in HTML).
title of text field page
  • Why use it: It is the fastest way to verify you have landed on the correct page.
  • Example:
String title = driver.getTitle();
if(title.equals("Text Fields")) {
    System.out.println("Test Passed: Correct Page");
}
  • Purpose: Fetches the exact string currently in the browser’s address bar.
  • Why use it: Essential for security testing or redirection testing. For example, if you try to access a secure page without logging in, the app should redirect you to the Login page. You use getCurrentUrl() to confirm this redirection happened.
Current url of text field page
  • Purpose: Returns the entire HTML source code of the page as a String.
  • Why use it: This is used less frequently but is helpful if you need to verify that a specific snippet of text or a hidden script exists on the page, even if it isn’t visible to the user.

Modern web applications are “Responsive,” meaning they change their layout based on the screen size (e.g., mobile view vs. desktop view). Selenium 4 gives you precise control over the browser window to test these behaviors.

  • Action: Expands the browser window to fit the maximum available screen space, including the OS taskbar and browser UI controls (address bar, tabs).
  • Why it’s critical: If a window is too small, elements might be hidden behind a specific mobile menu (hamburger icon) or simply overlap.
  • Best Practice: Always call this command immediately after initializing your driver to ensure a consistent “Desktop” view for your tests.

Action: Hides the browser window into the system taskbar.

Real Automation Use Case:

  • Testing “Page Visibility API”: Many modern apps behave differently when the user is not looking at them to save battery or data. For example, a video carousel might auto-pause, or a stock ticker might stop polling for updates when the tab is hidden. By using minimize(), you can verify that your application correctly pauses these background activities.
  • Local Debugging: If you are running a long regression suite on your local machine but need to continue working, minimizing the test window prevents it from constantly popping up and stealing focus from your keyboard.
  • Action: Switches the browser to full-screen mode.
  • Difference from Maximize: Unlike maximize(), fullscreen() completely hides the browser’s address bar, bookmarks bar, and tabs, as well as the operating system’s taskbar. The content takes up 100% of the pixels on the monitor.
  • Use Case: This is essential for visual regression testing where you need to take screenshots of the application without any browser UI “clutter” (like the URL bar) interfering with the image comparison.

In previous versions of Selenium, opening a new tab was a headache that involved simulating keyboard shortcuts (Ctrl+T) or injecting Javascript. Selenium 4 simplifies this with the newWindow command.

  • Action: Creates a new tab within the existing browser window.
  • Context Switch: Crucially, this command automatically switches the driver’s focus to the new tab. You do not need to manually switch handles immediately.
  • Use Case: Validating workflows where a user “Right-clicks > Open in new tab.” You can open a new tab, perform an action (like checking an email inbox), close it, and return to the main flow.
  • Action: Launches a completely separate browser window (a new instance).
  • Use Case: This is useful for simulating “Multi-window” scenarios, such as a chat application where one window is “User A” and the second window is “User B,” allowing you to test real-time communication between two sessions.

This is the most critical distinction for resource management. Misusing these can lead to “Memory Leaks,” where your computer slows down because dozens of invisible “Zombie Browsers” are running in the background.

  • Scope: It closes only the current window or tab that the WebDriver is focusing on.
  • Does the script stop? No. The WebDriver session is still active.
  • When to use: Use this when your test opens a secondary popup window (like a “Terms of Service” popup). You want to close that popup (driver.close()) and then return to the main parent window to continue testing.
  • Scope: It closes every single window and tab associated with the driver.
  • Does the script stop? Yes. It effectively destroys the WebDriver session.
  • When to use: You must use this at the very end of your test execution (usually in the @After or tearDown block). It performs the cleanup, killing the chromedriver.exe process in your system’s background.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WindowType;

public class BrowserCommandsMastery {
    public static void main(String[] args) throws InterruptedException {

        // 1. Setup the driver
        WebDriver driver = new ChromeDriver();

        // 2. Maximize immediately for consistent view
        driver.manage().window().maximize();

        // 3. Open the main application
        driver.get("https://testkru.com/Elements/TextFields");
        System.out.println("Opened: " + driver.getTitle());

        // 4. Validate URL
        String currentUrl = driver.getCurrentUrl();
        if(currentUrl.contains("testkru")) {
            System.out.println("URL Validation Successful.");
        }

        // 5. Open a new tab (Selenium 4 feature)
        // Scenario: We want to check a reference on Google without losing our place on TestKru
        driver.switchTo().newWindow(WindowType.TAB);
        driver.get("https://google.com");

        // 6. Close ONLY the Google tab
        driver.close();

        // IMPORTANT: After closing a tab, the driver focus is lost (in limbo).
        // You would normally switch back to the original window here.
        // (Topic for "Window Handles" article)

        // 7. Quit the entire session
        // This cleans up the memory and closes the TestKru window
        driver.quit();
        System.out.println("Test Completed Successfully.");
    }
}

Output after running the script

Opened: Text Fields
URL Validation Successful.
Test Completed Successfully.

Let us know in the comments if you run into any issues while following the steps in this article or while executing the script.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *