Thursday 1 March 2018

How to Handle Windows in Selenium

Many times we need to handle more than one windows in testing. It is due to many application has multiple windows. Handling more than one windows in selenium is very easy. We can handle each window with unique id of that window.

Selenium webdriver assign one unique alphanumeric id to each window as soon as web driver object is initiated. When we need to switch from base window to other window than we can switch to that wind using ID of that window.

GetWindowHandle

String vWindowHandle = driver.getWindowHandle();

Use : To get Window handle (Unique ID) of current window


GetWindowHandles

Set<String> vWindowHandles = driver.getWindowHandles();

Use : To get window handles of all windows.


We can use above to commands for switch to different window.

How to Switch to different windows?

Lets assume that current you have total three windows open and you want to switch to last window. 


String vBaseWindowHandle = driver.getWindowHandle();
Set<String> vWindowHandles = driver.getWindowHandles();
  
for(String temp : vWindowHandles)
{
 driver.switchTo().window(temp);
}
  
// Code for second window
  
driver.close();
driver.switchTo().window(vBaseWindowHandle);


Switch To Frame


Many application has multiple frame and when we do some operation on application then other frames will visible. For this scenario we need to switch that frame if we want to do some operations on that frame.

We can switch to frame with below command.


driver.switchTo().frame("name of frame");



Other type windows like pop up and alert also exists in many applications.

Please visit below post for how to handle pop up in selenium.

How to Handle PopUp in Selenium

Please add comment if you have any question.

No comments:

Post a Comment

Popular