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.

How to Handle PopUp in Selenium

Many application has some pop up for display some success or failure message to user. We need to handle that types of pop up in script.

There are three types of pop Up.
  1. Web based pop up
  2. Alert message
  3. Window based pop up
How to handle web based Pop Up

Web based pop up can be handle using WindowHandle or SwitchToFrame command. First we need to identify type of pop up. 
If pop up is other window than we need to use WindowHandle command.
If pop up is just frame than we need to use SwitchToFrame command.

Using WindowHandle

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

Using SwitchToFrame

  String vBaseWindowHande = driver.getWindowHandle();
  driver.switchTo().frame("Name of Frame");
  
  // Code for popup
  
  driver.switchTo().window(vBaseWindowHande);


How to handle Alert Message

Po pup type is alert then we have to use Alert class for handle alert.

you can use object of alert class for different operations on alert like accept, dismiss, get alert text etc.



  Alert alert = driver.switchTo().alert();
  alert.accept();
  alert.dismiss();
  String vAlertText = alert.getText();


How to handle Windows based pop up



Selenium can not handle window bases pop up. Selenium only handle things which are based on web. There are many ways to handle window based pop up.
We can use AutoIT third party tool or Robot Framework for handle window based pop up.

Please visit below post for how to handle windows based pop up (dialog box) using AutoIT.

How to Automate Non Browser Based Functionality with Selenium + AutoIT









How to Automate Non Browser Based Functionality with Selenium + AutoIT

Selenium can automate only browser based functionality if there is scenario that any non browser based functionality like windows dialog box comes than selenium can not handle. We need to use other tool for handle such scenario.

AutoIT is best open source tool for automate non browser based functionality with selenium. We have to first configure AutoIT with selenium before use.

For configuration part please go to below link and Configure AutoIT.

Configure AutoIT with Selenium

Don't forget to register 'AutoItX3_x64.dll' because it is very important. You can not use that .dll without register.

Once you are done with configuration then create one project for automate calculator.


 public static void main(String[] args) throws InterruptedException
 {  
  String jacobDllVersion;
  if (jvmBitVersion().contains("32"))
  {
   jacobDllVersion = "jacob-1.18-M2-x86.dll";
  }
  else
  {
   jacobDllVersion = "jacob-1.18-M2-x64.dll";
  }
 
  File file = new File("lib",jacobDllVersionToUse);
  System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
 
  AutoItX autoIT = new AutoItX();
  autoIT.run("calc.exe");
  autoIT.winActivate("Calculator");
  autoIT.winWaitActive("Calculator");
  // Do 5 * 5 = 25
  //Enter 5
  autoIT.controlClick("Calculator", "", "135") ;
  Thread.sleep(1000);
  //Enter *
  autoIT.controlClick("Calculator", "", "92") ;
  Thread.sleep(1000);
  //Enter 5
  autoIT.controlClick("Calculator", "", "135") ;
  Thread.sleep(1000);
  //Enter =
  autoIT.controlClick("Calculator", "", "121") ;
  Thread.sleep(1000);
  // Get total and verify it should be 25
  String vTotal = autoIT.controlGetText("Calculator", "", "#32770");
  
  if(vTotal.equals("25"))
  {
   System.out.println("Test case Pass.");
  }
  else
  {
   System.out.println("Test case Fail.");
  }
  
 }

To get the Calculator button ids for the number 5 and = I used the Au3info application that is in the install directory of autoit-v3 that you downloaded and extracted.




Please add comment if you have any question.



Popular