Friday, May 22, 2020

Window Handling – Working With Multiple Windows

Selenium WebDriver has  switch from one frame to another or to switch from one browser window to another in any web application.
The following example shows that how to switch over multiple windows. Each browser window has a unique window handle assigned to it. This handle is used by selenium to uniquely identify a window.
Many a times we open a page in a new window (or tab), or sometimes by clicking on a link or on a button a new window (or new tab) opens up. The control of Selenium doesn’t automatically switches to this new window (or new tab), but we have to switch to perform any operation on this new window.
Principle:
  • Initialize the webdriver and create an object of the same. 
  • Instantiate the browser driver to the new ChromeDriver (in this case, we are working on ChromeDriver, you can choose Firefox or IE) and specify the path in which the ChromeDriver exists followed by the extension of an executable file. 
  • Get the URL of the particular web page which you want to test
  • Find the element using one of the element locators in Selenium

Syntax

  • get.windowhandle(): helps in getting the window handle of the current window
  • get.windowhandles(): helps in getting the handles of all the windows opened
  • set: helps to set the window handles which is in the form of a string.  set<string> set= driver.get.windowhandles()
  • switch to: helps in switching between the windows
  • action: helps to perform certain actions on the windows.
Scenario:


Feature: Launch FireFox browser and handling multiple windows
Scenario: Launch ebay browser
Given User navigates to URL "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open"
And User print the title of this old window
When User clicks Click Try it button.
Then User print the title of this new window
And User close the window
 How to handle multiple windows in Selenium gives you an idea about how you can handle multiple windows while testing an application.
package com.automation.eng.AutomationProject;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class MultipleWindowsHandling {
public WebDriver driver;
@Test
public void mouseHover() throws InterruptedException{
//setup the firefoxdriver using WebdriverManager
WebDriverManager.firefoxdriver().setup();
//Create driver object for Chrome
driver = new FirefoxDriver();
String url = "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open";
//To maximise the browser
driver.manage().window().maximize();
//To delete all cookies
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//To invoke URL
driver.get(url);
String parentWindow = driver.getWindowHandle();
System.out.println("Parent window Id is : "+ parentWindow);
driver.switchTo().frame("iframeResult");
driver.findElement(By.tagName("button")).click();
String childWindow;
//Get Window Handle returns a set with session Id's of all the windows
childWindow = driver.getWindowHandles().toArray()[1].toString();
driver.switchTo().window(childWindow);
System.out.println("Title of child window is : "+ driver.getTitle());
driver.close();
driver.switchTo().window(parentWindow);
System.out.println("Title of Parent wind is : "+ driver.getTitle());
driver.quit();
}
}
/*
* Parent window Id is : 10
Title of child window is : W3Schools Online Web Tutorials
Title of Parent wind is : Tryit Editor v3.6
PASSED: mouseHover/
*/

CODE EXPLANATION- Few lines of code remains same i.e invoking browser, maximising window, deleting all cookies before actually launching your web URL. So, here we will start with the window handling part. Let’s go through each and every line of code-

Note: For simplicity, we are calling old window as Parent window and new one as child window.

  1. String parentWindow = Driver.getWindowHandle(); – This line of code will return a String which is handle of parent window.
  2. System.out.println(“Parent window Id is : “+ parentWindow);- We are simply printing the String i.e. handle obtained from the parent window in our console.
  3. Driver.switchTo().frame(“iframeResult”); Refer the Screenshot below , we are switching from main window to a frame (as Try it button is in a frame.)
  4. Driver.findElement(By.tagName(“button”)).click();- We have located the “Try it” button by tagName locator.
  5. String childWindow;- Declaring a String variable called ‘childWindow’.
  6. childWindow = Driver.getWindowHandles().toArray()[1].toString();- This line of code is important, getWindowHandles() method returns a SET of unique handles of all the windows which are currently opened. To get the handle of child window, we converted that set into an array. Now as there are only two windows opened and child being the latest one, so it will be at first index. Convert the object into string which will return the unique handle of the new window.
  7. Driver.switchTo().window(childWindow);- Here we are switching to the child window.window() method takes String . Hence, we passed “childWindow” that we obtained in above code.
  8. System.out.println(“Title of child window is : “+ Driver.getTitle());- Printing the title of child window  using getTitle() method.
  9. Driver.close();- close() method closes the active browser window.Hence, child window would be closed.
  10. Driver.switchTo().window(parentWindow);-  Now, switching back the handle  to the parent window.
  11. System.out.println(“Title of Parent wind is : “+ Driver.getTitle());- Printing the title of parent window.
  12. Driver.quit();- quit() method closes all the browser windows  completely.
IMPORTANT : PRACTICE MORE

No comments:

Post a Comment