Selenium Browser Commands

Controlling The Browser Window

Selenium gives you commands to resize, navigate, and manage browser windows. These commands mimic actions a real user performs, like clicking the back button or maximizing a window.

Diagram: Browser Command Groups

Browser Commands
   |
   |--- Navigation (back, forward, refresh, to)
   |
   |--- Window (maximize, minimize, resize, fullscreen)
   |
   |--- Manage (cookies, timeouts)

Navigation Commands

The navigate() method group lets your script move through browser history like a person clicking arrow buttons.

navigate().to()

Loads a new URL, similar to get().

driver.navigate().to("https://www.estudy247.com/courses");

navigate().back()

Moves one step backward in browser history.

driver.navigate().back();

navigate().forward()

Moves one step forward in browser history.

driver.navigate().forward();

navigate().refresh()

Reloads the current page, similar to pressing F5.

driver.navigate().refresh();

Window Management Commands

manage().window().maximize()

Expands the browser window to full screen size. Many scripts call this first to avoid layout issues on small windows.

driver.manage().window().maximize();

manage().window().minimize()

Shrinks the browser window to the taskbar.

manage().window().setSize()

Sets a custom window size using pixel dimensions. This helps test how a website looks on smaller screens.

driver.manage().window().setSize(new Dimension(1024, 768));

manage().window().fullscreen()

Puts the browser into true fullscreen mode, hiding the address bar.

Timeout Commands

Timeouts control how long Selenium waits before giving up on an action. Later topics cover waits in detail, but the basic timeout setting lives in the manage() group.

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

This line tells Selenium to wait up to ten seconds for elements to appear before throwing an error.

Example: Testing Back And Forward Navigation

driver.get("https://www.estudy247.com");
driver.get("https://www.estudy247.com/courses");
driver.navigate().back();
System.out.println(driver.getCurrentUrl());
driver.navigate().forward();
System.out.println(driver.getCurrentUrl());

This script visits two pages, goes back to the first, prints the URL, then moves forward again and prints the new URL. It checks that browser history behaves correctly.

Leave a Comment

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