In Selenium Webdriver Windows post, we will learn how to work with windows/tabs while we are doing web automation with selenium webdriver.

Audience

This post is designed for SW test professionals who have some information about selenium web automation. At the end of this tutorial, you will know how to work with windows/tabs.

Prerequisites 

Nice to have: Before starting to read this post it is better to read previous selenium tutorials.

  1. http://www.swtestacademy.com/selenium-tutorial-java-junit/
  2. http://www.swtestacademy.com/selenium-webdriver-api/
  3. http://www.swtestacademy.com/selenium-webdriver-navigation/
  4. http://www.swtestacademy.com/selenium-webdriver-interrogation/
  5. http://www.swtestacademy.com/selenium-webdriver-manipulation/
  6. http://www.swtestacademy.com/selenium-webdriver-alerts/
  7. http://www.swtestacademy.com/selenium-webdriver-frames/

Working with Handles

Every window has a unique handle. Handle means a specific identifier that represents its window. We can manage the windows with their handles. I want to explain what Selenium Webdriver provides us for window handles.

– driver.getWindowHandle() – We can get current window’s handle

– driver.getWindowHandles() – We can get all windows handles

– driver.switchTo().window(String handle) – We can switch to the target window by using its handle.

Example-1: Window Handles

Test website: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_link_target

Test Scenario:

  1. Go to above URL.
  2. Get the current window’s handle and write to the console window. It must be first window handle.
  3. Locate the “Visit W3Schools.com!” link and click it.
  4. Get all window handles and hold them in a list.
  5. Write to total window handle number to the console. It must be 2.
  6. Switch t the second window.
  7. Get the current window’s handle and write to the console window. It must be second window handle.
  8. Check the upper left side text is “THE WORLD’S LARGEST WEB DEVELOPER SITE” in the second window.
  9. Go back (Switch) to the first window.
  10. Get the current window’s handle and write to the console window. It must be first window handle.
  11. Check the See Run Button Text. It must contain “Run >>” text.

Test Code:

Console Output:

windows

How to Manage Selenium Webdriver Windows

Webdriver provides us below methods to manage windows. We can control the size and position of the current window with the following methods.

Driver.manage.window()

– .maximize() – It maximizes the current window.

– .getSize() – It returns the size of the current window.

– .setSize() – It sets a new size to current window.

– .getPosition() – It returns current position in term of x,y coordinates.

– .setPosition() – It moves the current window around.

Example-2: Manage Window

Test website: http://www.swtestacademy.com

Test Scenario:

  1. Navigate to above URL.
  2. Maximize the current window.
  3. Get the size of the window and write the full-screen size to the console.
  4. Minimize the window by 1/4 and write the new screen size to the console.
  5. Get window position and write it to the console.
  6. Set window position x=100 and y=200 and write to the console.

Test Code:

Console Output:

windows

Summary

  • You learned how to handle and manage browser windows with selenium webdriver methods.

Selenium Webdriver Tutorial Series