How to Handle Window based Pop up using Selenium and AutoIT?

Some times while doing testing with selenium, we get stuck by some interruptions like a window based pop up.  I have faced similar situation while doing my analysis. Since we were using a proxy to connect to internet, any page we open will ask for proxy authentication in a window based pop up. But selenium fails to handle this as it has support for only web based application. To overcome this problem we need to use AutoIT along with selenium script. AutoIT is a third party tool to handle window based applications. The scripting language used is VBScript.

So let’s take the example of the problem I was facing. Every time you open a page you get a pop up asking for authentication. To overcome the problem let create an AutoIT script first. To do so let’s create one file with any name and .au3 extension in any location. Let’s say the file name is Authentication.au3 and it’s created in the example folder of the AutoIT installation folder. So we need to know the window title of the window that is disturbing our test. Taking an example of Firefox let’s consider the window title as “Authentication Required”. The script to handle this pop up is as below which is written inside Authentication.au3. You need to write click on the file and click edit and write the following.

WinWaitActive("Authentication Required","","10")
 If WinExists("Authentication Required") Then
 Send("userid{TAB}")
 Send("password{Enter}")
 EndIf

Now save the file and click on tools and compile it. One more file with same name but with .exe extension will be created in the same location. This file needs to be used inside selenium script. How to use it. It’s pretty simple. We need to add one statement in the setUp method before calling the recursive setUp method with the URL. Below is the statement that needs to be added.

Runtime.getRuntime().exec("C:\\Program Files\\AutoIt3\\Examples\\authenticationFF.exe");

The whole path along with the file name has to be passed to exec method. Now when you fire your script your autoIT script will be fired first and the initial pop up will be handled.

About these ads

30 comments

  1. This didn’t really work for me… so I used the following:

    WinWaitActive(“Windows Security”)
    ControlSetText(“Windows Security”,””,”Edit1″,”user”)
    ControlSetText(“Windows Security”,””,”Edit2″,”pass”)
    Send(“{ENTER}”);

  2. WinWaitActive(“Windows Security”)
    ControlSetText(“Windows Security”,””,”Edit1″,”user”)
    ControlSetText(“Windows Security”,””,”Edit2″,”pass”)
    ;Click Ok/Login button
    Send(“{ENTER}”);

    • Generate the exe using autoIT. Then use the following command where you want the exe to be executed.

      Runtime.getRuntime().exec(“C:\\Program Files\\AutoIt3\\Examples\\.exe”);

  3. Hi Thanks a lot for making us handling the pop-ups easier.but I am facing problem in handling the
    new window generated by the pop-up window.

    Here is the scenario When we click “OK” on the pop-up window a new window that is the excel file needs to get opened And we need to focus on that window ,after that we need to change the focus back to the main window

    • Hi Chethana,

      You can WinWait for the same. Below is an example for a notepad file.

      Run(“notepad.exe”)
      WinWait(“[CLASS:Notepad]“)

      You need to find out what the classname should be for the xls file. This will solve your problem.

  4. Hi Sudeep,

    Here is the code that i have used to handle a window pop-up.It works fine only after I click on the window

    WinWaitActive(“[CLASS:MozillaDialogClass]“,””,10)
    If WinExists(“[CLASS:MozillaDialogClass]“) Then
    Send(“OK{ENTER}”)
    EndIf

  5. Hi can we handle SSL certificates in google chrome using selenium ? if not what else can be done for the same. Your help will be highly appretiated.

  6. Hello blogger,

    My webdriver code is not running the AutoIT exe file. However if I open the website and run the AutoIT exe file separately, it works.

    Also, I could find in some site that says, AutoIT is not supported in Webdriver.

    Is there any workaround?

    Here is my code:

    SeleniumTest.java

    package tests;

    import java.io.IOException;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class Handling_Windows_Based_Prompt {

    public static void main(String[] args) throws IOException{
    WebDriver c1 = new FirefoxDriver();
    c1.get(“http://my.test..in”);
    Runtime.getRuntime().exec(“E:\\authenticationFF.exe”);

    }

    }

    authenticationFF.au3

    WinWaitActive(“Authentication Required”,””,”10″)
    If WinExists(“Authentication Required”) Then
    Send(“CHENNAI/sumand{TAB}”)
    Send(“smartek@1212{ENTER}”)
    EndIf

    Kindly reply

  7. Hi,
    My problem is after entering wrong username and password,one pop up is coming which is giving message “invalid Username And Password” and one ok button is there .If i click on that ok button then again it will go to login page.but i am unable 2 handle that pop up box.That pop up box has no title.Please help me how will i solve my problem.

  8. Hi sudeep,

    I need some help in here.
    My task is : I have to click a link on Firefox so download dialog box(window popup not browser) will appear and than i have to open file(file is txt so will open in notepad). Now i have to assert that file has some particular line in it. While doing this My Selenium script should wait for the result, or may take input from autoit about that line. If that line is present script should move forward else should fail.

    I am have automated upto opening my file in notepad but problem is how to get some return value from autoit and how to wait in selenium till that value comes.

    Can you please help me with this.

    • There is a suggestion for this problem. You can do just till the download part using AutoIT. Once the file is added just write a file reading code to do the read instead of using AutoIT to do so. It will implicitly wait till the execution of read code is over. If you use java you can use IO under utils.

  9. Thanks a lot Sudeep,
    Yeah that really helped. But there is 1 more thing than, how can we know if autoscript run is successful or failed, I mean when we call autoscript to save/open that file auto it will just run and open/save it. But how do we find that if file even get downloaded or not. May file didnt even got download and autoit gets failed.

    i use Process p = Runtime.getRuntime().exec( processName ); to execute the script but how will i know if it gets downloaded or not.

      • Actually, we can’t use sendKeys here because save dailouge box is window popup and not browser popup. So we can’t find the element using driver.findElements. I even checked it by Selenium IDE.
        So we needs to use some window automation tool like autoit there. But than problem is, when i calls my autoit script, it start running parallel to my selenium script. Selenium script doesn’t wait for it to complete and don’t even know if Autoit script gets passed or not.

  10. Hi Sudeep,

    I am using AutoIt script for a pop up in Watir-Webdriver. But when I run the watir script I am getting “uninitialized constant Runtime (NameError)”.

    Need help in this.

    Below is the code:

    I am executing the scripts from a linux server. When the user clicks on the “Calc” link in the page, the calculator opens in the remote windows desktop during which it asks for a user name and password for authentication.

    require ‘watir-webdriver’
    require ‘d:\\Ruby\\page_object_sgd_3′
    require ‘d:\\Ruby\\sgd_linux_3_Log_In’
    require ‘au3′

    begin

    puts ” ”
    puts ” ”
    puts “=======TestCase 3.1: Clicking on the ‘Calc’ option=======”

    click_calc
    @frame_calc.click
    sleep 10

    Runtime.getRuntime().exec(“D:\\Ruby\\authentication.exe”)

    AutoIT code:

    WinWaitActive(“Authentication for host21.uk.xxxxxx.com”, “”, “20”)
    If WinExists(“Authentication for host21.uk.xxxxxx.com”)Then
    Send(“xxxx{TAB}”)
    Send(“xxx{Enter}”)
    EndIf

  11. Hi Sudeep,

    Sorry, I got the answer.. Instead of doing this “Runtime.getRuntime().exec(“D:\\Ruby\\authentication.exe”)”, we need to run it as a system command or using back tick (“).

  12. Hello all,
    I stumbled upon this post while searching for Autoit script to be used for IE proxy authentication as IE behaves entirely different than ff and chrome.

    After doing a short study on the Autoit Functions I was able to write a script of my own.
    Sharing the same for reference.

    ;*****************************************************************************************************************
    AutoItSetOption(“WinTitleMatchMode”,”1″)
    if WinWaitActive(“Connect to {name of your proxy/ip}”)Then
    Beep(500, 1000)
    $UN=(“Username”)
    $PWD=(“Password”)
    Sleep(500)
    ControlSetText(“Title” , “Identifier_text”,”Edit2″,$UN)
    Sleep(500)
    ControlSetText(“Title” , “Identifier_text”,”Edit3″,$PWD)
    Sleep (500)
    Send(“{TAB}”)
    Sleep(100)
    Send(“{Enter}”)
    Sleep (10000)
    Run(“Authenticate-IE.exe”)
    EndIf
    ;*****************************************************************************************************************

    Regards,

    -Raj Sarodaya.

  13. Using Auto it not able to handle browser pop up window in firefox.Here is the code

    package mypackage;

    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import org.testng.annotations.*;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import com.thoughtworks.selenium.DefaultSelenium;
    import com.thoughtworks.selenium.Selenium;
    import Data.Function;

    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    //import org.openqa.selenium.support.ui.Select;

    public class Report extends Function{

    Object Open_a_popup_window ;
    String Positioned_Popup ;
    Object JavaScript_Popup_Windows ;
    private Selenium selenium;

    @Before
    public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = ” “;
    }

    @Test
    public void inventory() throws Exception {
    Function object = new Function();
    object.Login();
    Runtime.getRuntime().exec(“C:\\Program Files (x86)\\AutoIt3\\Examples\\Authentication.exe”);
    /*Enter Report Details */

    driver.get(baseUrl + “/dashboard/dashboard”);
    driver.get(baseUrl + “/report/”);

    /*Run*/

    driver.findElement(By.xpath(“.//*[@id='productFiltersRight']/div[1]/input”)).click();
    Thread.sleep(80000);
    WebElement Action = driver.findElement(By.xpath(“.//*[@id='tableListingData']/div/div”));

    String linktxt = Action.getText();

    if( linktxt.equalsIgnoreCase(“Record not found.”))
    {
    {

    System.out.println(“Report_US contains No Data:”);

    }

    if(linktxt.equalsIgnoreCase(“FAILED”))
    {

    System.out.println(“Report_US failed”);
    }
    }
    else {

    driver.findElement(By.xpath(“.//*[@id='filters_advance_top']/div[4]/a/img”)

    //After this line if CSV file present browser window pop up opens. So guide me how to handle it.
    }
    @After
    public void teardown() {
    driver.close();
    //System.exit(0);
    }
    }
    Authentication.au3
    WinWait(“[CLASS:MozillaDialogClass]“,””,10)
    If WinExists(“[CLASS:MozillaDialogClass]“) Then
    Send(“OK{ENTER}”)
    EndIf

  14. Hi, I am a Selenium and Ruby newbie. Can anyone help? What would the code look like in AutoIT (to run from Selenium with Ruby if I wanted to act on the IE brower Save Dialog. Specifically, to click on save, then save the file to a particular location?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s