Why Learn Switching Windows using Selenium?
You may come across certain automation scenarios that involve any of the following
- An action on website causes a new browser tab to be launched.
- Website shows some popups which may require some action.
- An action on website causes new browser window to be launched.
And sometimes we ourselves might be interested in testing an application by using it from multiple tabs (e.g. GMail when multiple-tabs are opened, they all connect to same user account with which we are logged in).
Switching Windows in selenium is about learning how to write tests that can work with multiple browser windows or tabs in the same test.
WebDriver interface provides us three new functions using which we can code tests that involve dealing with multiple browser windows or tabs. We just need to understand that each browser window or tab in selenium is identified by a unique string (which is automatically assigned) this unique string is called Window Handle. If there are two browser tabs opened and tab 1 is active, driver.findElement command will work for HTML code in Tab1 only. If we want to click any button in page of Tab2, we first need to ask driver object to switch its context to tab 2 by giving Handle of tab 2.
// Instantiate WebDriver object WebDriver driver = new FirefoxDriver(); // some code here to brows to the site, click on button/page element that generates alert String mainWindowHandle = driver.getWindowHandle(); // Do an action that opens a new browser window or tab in the same browser Set<String> windowHandles = driver.getWindowHandles(); // Switch to browser windowt/tab whose handle is stored in mainWindowHandle variable driver.switchTo().window(mainWindowHandle); // After switching to a new window or tab, all commands like findElement or getTitle will work according to page code in the new window/tab
Methods in WebDriver Interface for Switching Windows
Key methods that we would require to perform four common operations on any given alert are:
// Methods of WebDriver interface public String getWindowHandle(); // Returns a String (called handle) that represents currently opened TAB public Set<String> getWindowHandles(); // Returns a set that contains handles (Strings) of all opened browser windows public TargetLocator switchTo(); // Get the TargetLocator using which we can switch to any browser window (using handle).
Exercise – 1
- Launch Browser
- Open Popup Test Website.
- Click MultiPopup Test link on the homepage of site (this will launch few popups).
- Print count of total new popups opened by the browser.
- Switch to each popup one by one and do the following
- Print the URL
- Print Page Title
- Close each pop-up one by one (don’t close the main website page)
- Quit the browser.
Exercise – 2
- Using Selenium Launch Browser
- Open Selenium New Tab Page
- Click Terms and Conditions link on the page.
- Verify that a new browser window or tab is launched.
- Switch to newly opened window.
- Print the Current URL and Title after switching.
- Click Agree button.
- Switch back to main window.
- Print Current URL and Title.
- Quit browser.