Sunday, 28 September 2014

Handling Windows Browser Authentication PopUps using AutoIt Tool

Handling Browsers Authenttication PopUp can be easily done by one of the open sources available known as "AutoIt"

You can download AutoIt from here. AutoIt has a basic and simple Scripting language and can automate the windows desktop features.

Here we will talk about how to automatically handle the windows browser authentication popups as follows :


To handle this, we need to send the username and password in the popup and click Ok button.

Step 1:
So, to do this, we need to create a small '.au3' file which has the AutoIt Scripting Program in it as follows:


While 1
WinWait("Authentication Required")
If WinExists("Authentication Required") Then
 WinWaitActive("Authentication Required","",20)
 Send("username")
 Send("{TAB}")
 Send("password")
 Send("{ENTER}")
EndIf
Wend


Step 2:
Save the file above, right click on it click on the option "Compile Script(x86)". This will compile the script and create a ".exe" of the ".au3" file that you made.

This convert your AutoIt script to an executable.

Step 3:
Now to use it with WebDriver. Following is the Java Code :


/**
 * Code for Handling Windows Authentication PopUps
 */
 public static void authenticationHandler(){

  String execFilePath = null;

  // Handled only in case of Windows
  if(Platform.WINDOWS.is(currentOS)) {
   execFilePath = "../resources/drivers/autoItDriver/autoItExecutable/AuthenticationHandler.exe";
  }
  try {
   logger.info("AutoIt executable Path"+execFilePath);
   if(execFilePath != null) {
    authenticationProcess = Runtime.getRuntime().exec(execFilePath);
    logger.info("Windows Browser Authentication PopUp Handled");
   }
  } catch (Exception e) {
   e.printStackTrace();
   throw new RuntimeException("Authentication Failed");
  }

 }

Also , in @AfterSuite you can write the following code to destroy the .exe :

private static Process authenticationProcess;
 @AfterSuite(alwaysRun=true)
 public void quitWebDriver()
 {
  logger.info("Inside After Suite");

  if(authenticationProcess != null) {
   authenticationProcess.destroy();
  }
  driver.quit();
 }


That's it !

You are done with handling windows Authentication Browser PopUps.

Note: This only works on Windows Machine, For Linux & IOS there are other tools available in the market.