SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Using getwindowhandle() and getWindowHandles() methods in Selenium WebDriver

getwindowhandle() and getWindowHandles() are methods to get handle of window. getwindowhandle() is useful to get handle of current selected window or tab while getWindowHandles() will return handles of all open windows or tabs. Both these methods are useful when you are working with multiple windows and you need to switch from one window to another window.

How to use getwindowhandle() method?
package TestPack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class testgetWindowHandle {

 public static void main(String[] args) {
  String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
  System.setProperty("webdriver.chrome.driver", exePath);
  WebDriver driver = new ChromeDriver();
  
  driver.get("https://only-testing-blog.blogspot.com/2014/01/textbox.html");
    
  //Get current browser window handle.
  String tabonehandle = driver.getWindowHandle();
  System.out.println(tabonehandle +" is current browser window handle.");
 }
}

Output : CDwindow-8C98EDC035FFE4FD2C644AF7F6FBB0B4 is current browser window handle.

In above example you can see that getwindowhandle() method has retrieved current browser's window handle and print it in console.

How to use getWindowHandles() method?
package TestPack;

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class testgetWindowHandles {

 public static void main(String[] args) {
  String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
  System.setProperty("webdriver.chrome.driver", exePath);
  WebDriver driver = new ChromeDriver();
  
  driver.get("https://only-testing-blog.blogspot.com/2014/01/textbox.html");
  driver.findElement(By.xpath("//b[contains(text(),'Open New Page')]")).click();
  
  //Get main window handle.
  String tabonehandle = driver.getWindowHandle();
  System.out.println(tabonehandle +" is tab one window handle.");
  
  //Get all windows handles using getWindowHandles()
  Set<String> allhandles=driver.getWindowHandles();
  
  //Iterate through allhandles.
  Iterator<String> it = allhandles.iterator();
  
  String tabtwohandle = null;
  
      while(it.hasNext()){
       //Store iterator window handle value in temp
       String temp = it.next();
       
       //compare temp value with tabonehandle
      if(!tabonehandle.equalsIgnoreCase(temp)) {
       //If not match with tabonehandle then it is tab two handle and store it in tabtwohandle.
       tabtwohandle = temp;
       System.out.println(tabtwohandle  +" is tab two window handle.");
      }
      
      }
      
      //Switch to tab two of browser using tabtwohandle
      driver.switchTo().window(tabtwohandle);
      //type text in text box of tab two
      driver.findElement(By.id("fname")).sendKeys("some text");
      
     //Switch to tab one of browser using tabonehandle
      driver.switchTo().window(tabonehandle);
      //select radio button on tab one.
      driver.findElement(By.id("radio2")).click();      
 }
}

Result : It will switch between two tabs of browser and perform some actions.

In above example, Initially we stored tabonehandle using getWindowHandle() method. Next we clicked on link to open page in new tab. On this stage, We have two tabs open. Then we got all window handles using getWindowHandles() method and retrieved tabtwohandle. So we have both tabs handle and we can switch between two tabs using driver.switchTo().window(handlename).

When to use close() and quit() methods?

When you need getwindowhandle() and getWindowHandles()?
You need getwindowhandle() and getWindowHandles() methods when you are working on multiple windows or tabs and need to switch between them.

No comments:

Post a comment