Tuesday 31 December 2013

Handling Windows Dialogs Using Selenium Part 2

In order to identify the Window Object properties, we use a tool provided by Microsoft called UISpy.
Download UISpy from here

When you run this tool, you can view all the windows objects along with their properties.

The following image shows the Properties for "RUN" dialog box.





From the above image, we can see the list of identification properties (Highlighted in red) for the Edit Box. We use some of this Properties to Identify an Object.


Note:


Add the jacob-(version-bit-type).dll file in your project Root Directory.
or
Place it in any directory and use the below code to load the DLL file.

for 32 Bit : jacob-version-x86.dll
for 64 Bit : jacob-version-x64.dll

Ex:
System.setProperty("jacob.dll.path""D:\\jacob-1.17-M2-x86.dll");

LibraryLoader.loadJacobLibrary(); 

After the above code, you can test the following snippets.

Sample Code To Test Run Window:



public static void testRUN() throws AWTException, WindowsHandlerException {

              WindowHandler handler = new WindowHandler();

              WindowElement runWindowElement = handler.getWindowElement("Run");

              WindowElement textBoxElement = handler.findElementByClassName(

                           runWindowElement, "Edit");

              handler.clearText(textBoxElement);

              handler.typeKeys(textBoxElement, "notepad");

              WindowElement okButton = handler.findElementByName(runWindowElement,

                           "OK");

              handler.click(okButton);

       }

The below are the various methods provided by the API to identify an Object.
  1. findElementByName
  2. findElementByClassName
  3. findElementByNameAndClassName
  4. findElementByAutomationID 
  5. findElementByLocalizedControlType
  6. findElementByNameAndLocalizedControlType 

Sample Code for Downloading a file in Firefox:



public static void testDownloadFirefox() throws WindowsHandlerException, InterruptedException {
     driver.get("http://www.zkoss.org/zkdemo/file_handling/file_download");
     driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/table/tbody/tr/td[2]   /div[2]/div/div[2]/div/div/div/div/div[2]/span/table/tbody/tr[2]/td[2]"))
     .click();
     Thread.sleep(3000);
     WindowHandler handler = new WindowHandler();
     WindowElement firefoxElement = handler
                     .getWindowElement("ZK Live Demo - File Download - Mozilla Firefox");
     WindowElement downloadElement = handler.findElementByClassName(firefoxElement,     "MozillaDialogClass");
     WindowElement saveRadioButton = handler.findElementByName(downloadElement, "Save File"); 
     handler.click(saveRadioButton);
     WindowElement okButton = handler.findElementByName(downloadElement,"OK");
     handler.click(okButton);
}


Sample Code for Uploading a file in Firefox:


public static void testUploadFileFirefox() throws AWTException,
                     WindowsHandlerException, InterruptedException {

      driver.get("http://cgi-lib.berkeley.edu/ex/fup.html");
      driver.findElement(By.xpath("/html/body/form/input")).click();
      Thread.sleep(3000);
      WindowHandler handler = new WindowHandler();
      WindowElement firefoxElement = handler
                   .getWindowElement("Sample File Upload Form - Mozilla Firefox");
      WindowElement uploadElement = handler.findElementByName(firefoxElement,
                   "File Upload");
      WindowElement fileNamePath = handler.findElementByNameAndClassName(uploadElement,  "File name:""Edit");
      handler.typeKeys(fileNamePath, System.getProperty("user.dir")"\\sample.txt");
      WindowElement openButton = handler.findElementByName(uploadElement,"Open");
          handler.click(openButton);
  }


Sample Code for Handling Authentication in Firefox - Tomcat Authentication:


public static void testTomcatAuthenticationInFirefox()
               throws WindowsHandlerException, AWTException, InterruptedException {
        driver.get("http://localhost:8080/");
        driver.findElement(
                      By.xpath("/html/body/div/div[3]/div[3]/div[2]/a/span")).click();
        Thread.sleep(3000);
        WindowHandler handler = new WindowHandler();
        WindowElement firefoxWindowElement = handler
                     .getWindowElement("Apache Tomcat/7.0.42 - Mozilla Firefox");
        WindowElement authenticationElement = handler.findElementByName(
                      firefoxWindowElement, "Authentication Required");
        WindowElement userNameElement = handler
                      .findElementByNameAndLocalizedControlType(
                                      authenticationElement, "User Name:""edit");
        handler.typeKeys(userNameElement, "admin");
        WindowElement passwordElement = handler
                      .findElementByNameAndLocalizedControlType(
                                      authenticationElement, "Password:""edit");
        handler.typeKeys(passwordElement, "admin");
        WindowElement okButton = handler.findElementByName(
                      authenticationElement, "OK");
        handler.click(okButton);
 }


Using Java Threads:


The above examples works fine for downloading, uploading and windows authentication.

This section describes windows authentication when you encounter a Authentication Dialog Box as soon as you open the Browser using selenium. In this kind of scenarios, selenium does not execute further steps unless the Authentication Dialog box is closed. which means if you write the Windows handler Code after the browser has opened, the Windows Handler Login code will not execute.



WebDriver driver = new FirefoxDriver();
            driver.get("url");

            // Following code Wont Execute if Windows Authentication Dialog is 
            // Opened right after driver.get("url") is called
            WindowHandler handler = new WindowHandler();
            // Develop Code For Handling the Dialog Box

            // further test code follows
            //driver.findElement ...



To Handle this kind of scenarios. We use Java Threads to perform Windows Authentication using a separate Thread.



// Define a Separate Thread class that will wait for Certain amount of time for // the Login Dialog Box to appear and then Window Hanlder Code will execute

public class Authenticator extends Thread {

      @Override
      public void run() {
            try {
                  Thread.sleep(5000);
                  WindowHandler handler = new WindowHandler();
                  // Develop Code For Handling the Dialog Box
            } catch (WindowsHandlerException e) {
                  e.printStackTrace();
            } catch (InterruptedException e) {
                  e.printStackTrace();
            }

      }
}


In our Selenium Test Class we call the Thread as follows



WebDriver driver = new FirefoxDriver();

            // We Start the Thread before driver.get() method
            // so that the Thread can execute in parallel, once the Window
            // Authentication Dialog is opened
            // The Window Handler Code Executes
            Authenticator authenticator = new Authenticator();
            authenticator.start();

            driver.get("url");

            // further test code follows
            // driver.findElement ...


Go To: Handling Windows Dialogs Using Selenium Part 3  - Contains Details regarding Upcoming Releases and Features

38 comments:

  1. I have added ATU_selenium_utilies and Jacob jar files but when i gave the above code window handler cannot be resolved to type is displaying. can anyone suggest me your ideas for this defect.

    ReplyDelete
  2. Please can any one tell me whether the above code will work in selenium webdriver

    ReplyDelete
    Replies
    1. Hi,

      Use ATU_Selenium_Utilities version 2. If you are using version 1, you will face this error. Please note that the Windows Handling API is introduced from version 2 onward.

      Regards
      AT

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. The upload dialog is invoked by browser, so first search for your browser window in UISpy, then expand it, under this you can find the upload dialog window information.

      Regards
      AT

      Delete
  4. Hi,
    I am unable to recognize window element for the first time. If I open the window element again after closing the first for upload, it works fine. This code does not work with framework. It works fine otherwise.

    ReplyDelete
    Replies
    1. As explained in the above post, Create a thread that will handle uploading.

      refer to "Using Java Threads:" section in the above post. This should help you out.

      #AT

      Delete
    2. public int Windows_Handler_Upload(Object[] enter) throws WindowsHandlerException, InterruptedException{
      int flag=0;
      ffdriver.findElement(By.xpath("/html/body/div[12]/div/div/div/div[3]/ul/li[2]/a/img")).click();
      ffdriver.findElement(By.xpath("/html/body/div[12]/div/div/div/div[3]/ul/li[2]/ul/li/a")).click();
      Thread.sleep(5000);
      try{
      WindowHandler handler = new WindowHandler();
      Thread.sleep(5000);
      System.setProperty("To_Upload", "C:\\Users\\janani.r\\Desktop\\Test_Text_4.txt");
      WindowElement firefoxElement = handler.getWindowElement("Synchronoss - Mozilla Firefox");
      WindowElement uploadElement = handler.findElementByName(firefoxElement,"File Upload");
      WindowElement fileNamePath = handler.findElementByNameAndClassName(uploadElement, "File name:", "Edit");
      handler.typeKeys(fileNamePath, System.getProperty("To_Upload"));
      WindowElement openButton = handler.findElementByName(uploadElement,"Open");
      handler.click(openButton);
      System.out.println("Executed");
      flag=1;

      }
      catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      flag=0;
      }
      return flag;
      }

      The above Code works good without the Framework, When it integrated with Framwork, it is not recognize the Windows in the First instance.

      Delete
    3. Hi Sridhar,

      The scenario which you are facing is explained below.

      When a native dialog say upload dialog appears, the webdriver does not proceed to next step unless the dialog is closed, which means the next step for entering upload action does not execute.

      However you can still overcome this challenge by implementing threads as explained in the above post - using threads, the upload action can be performed by the thread.

      #AT

      Delete
  5. Hi....Thanks for the Reply....This is Pradeep, a Collegue of Sridhar...
    Can you provide the sample code which got thread in it, to handle window elements...?or you mean to say to use Thread.sleep() in the code, before encountering the Window Element Code.....?

    Thanks...
    Pradeep

    ReplyDelete
    Replies
    1. Hi Pradeep,

      In the above post, please refer to "Using Java Threads:" section. an example for creating thread is explained. Call this thread just before when you click on a element which will invoke the native dialog.

      #AT

      Delete
    2. Hi Pradeep,

      Try the below code
      {{



      //Create the below class and call it in your function Windows_Handler_Upload

      public class ThreadToClickOnBrowse implements Runnable{

      static WebDriver browser;
      static String locator;
      AUTThread(WebDriver ffdriver, String loc){
      browser = ffdriver;
      locator=loc;

      }
      @Override
      public void run() {
      browser.findElement(By.xpath(locator)).click();
      }

      }

      //modified function

      public int Windows_Handler_Upload(Object[] enter) throws WindowsHandlerException, InterruptedException{
      int flag=0;
      ffdriver.findElement(By.xpath("/html/body/div[12]/div/div/div/div[3]/ul/li[2]/a/img")).click();

      Thread t1 = new Thread(new ThreadToClickOnBrowse(ffdriver, "/html/body/div[12]/div/div/div/div[3]/ul/li[2]/a/img");
      t1.start();

      Thread.sleep(5000);
      try{
      WindowHandler handler = new WindowHandler();
      Thread.sleep(5000);
      System.setProperty("To_Upload", "C:\\Users\\janani.r\\Desktop\\Test_Text_4.txt");
      WindowElement firefoxElement = handler.getWindowElement("Synchronoss - Mozilla Firefox");
      WindowElement uploadElement = handler.findElementByName(firefoxElement,"File Upload");
      WindowElement fileNamePath = handler.findElementByNameAndClassName(uploadElement, "File name:", "Edit");
      handler.typeKeys(fileNamePath, System.getProperty("To_Upload"));
      WindowElement openButton = handler.findElementByName(uploadElement,"Open");
      handler.click(openButton);
      System.out.println("Executed");
      flag=1;

      }
      catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      flag=0;
      }
      return flag;
      }



      }}

      Regards,
      Vinay

      Delete
    3. small correction in the above code, replace AUTThread constructor with ThreadToClickOnBrowse

      {{

      public class ThreadToClickOnBrowse implements Runnable{

      static WebDriver browser;
      static String locator;
      ThreadToClickOnBrowse (WebDriver ffdriver, String loc){
      browser = ffdriver;
      locator=loc;

      }
      @Override
      public void run() {
      browser.findElement(By.xpath(locator)).click();
      }

      }



      }}

      Delete
  6. Hi,

    I implemented handling download pop-up in IE11 using a separate thread as explained in the above post, but my code does not execute anything below the line that generates the pop-up even when the pop-up has been handled by the separate thread. Any suggestion on how to make selenium execute further steps once the pop-up has been taken care of?

    ReplyDelete
    Replies
    1. Hi Binita,

      If the popup has been handled successfully by separate thread, then the rest of the selenium code is purely dependent on how you write it.

      (Check if a thread is really required in your case. give a look at join() method in Thread class, this might help you).

      #AT

      Delete
  7. Hi,
    I am getting the exception "Can't load IA 32-bit .dll on a AMD 64-bit platform", what could be the problem ?

    ReplyDelete
  8. Hi,
    How to add dll in netbeans? I never added DLLs so new to this. Tried following code:
    System.setProperty("jacob.dll.path", "D:\\jacob-1.17-M2-x64.dll");

    LibraryLoader.loadJacobLibrary();

    LibraryLoader class does not exits. Which package to be imported?

    ReplyDelete
    Replies
    1. Please download Jacob jar file, this library contains the required classes. This part is described in main page. The download link is provided in there.

      #AT

      Delete
  9. Hi AT,

    Is there any limitation in Windows 2008 Server.
    When i launch UISpy it throws the following error :
    {{

    Description:
    Stopped working

    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: uispy.exe
    Problem Signature 02: 6.0.5808.16384
    Problem Signature 03: 45305459
    Problem Signature 04: UISpy
    Problem Signature 05: 3.0.0.0
    Problem Signature 06: 45305459
    Problem Signature 07: f3
    Problem Signature 08: 7f
    Problem Signature 09: System.IO.FileNotFoundException
    OS Version: 6.1.7601.2.1.0.272.7
    Locale ID: 1033

    Read our privacy statement online:
    http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

    If the online privacy statement is not available, please read our privacy statement offline:
    C:\Windows\system32\en-US\erofflps.txt


    }}

    Also when i run handler.getWindowElement("**** - Windows Internet Explorer"); it returns false though the window is present, it works fine in Windows7.

    Please suggest.

    Regards,
    Vinay

    ReplyDelete
    Replies
    1. I was able to run "C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm ATU_WindowsHandler.dll /codebase " command with out error.

      Delete
    2. Hello,

      have .NET 3.0 installed in your machine which is required for UISpy.

      #AT

      Delete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Hi AT,

    Can you please point me the link to jar which has the following classes
    WindowHandler, WindowElement and LibraryLoader

    Thanks,
    Sai Arun J

    ReplyDelete
    Replies
    1. You can find the download links in the main page of "Handling Windows Dialogs Using Selenium" section.

      #AT

      Delete
  12. Hi,
    I did the following:
    I registered ATU_WindowsHandler.dll (64 bit). I imported the ATU_Selenium_Utilities_2.0.jar file into my project. I also added the following lines of code.
    System.setProperty("jacob.dll.path", "C:\\workspace\\MyProject\\lib\\jacob-1.18-M2-x64.dll");
    LibraryLoader.loadJacobLibrary();

    I see the following:
    WindowsHandlerException is not resolved. It does recognize the WindowElement and the WindowHandler. It also asks me to add this exception if I remove it but is unable to resolve it. Can you provide any suggestion to fix this.

    ReplyDelete
    Replies
    1. Fix the Project Setup ! Adding the jar to your build path should fix the import error.

      #AT

      Delete
  13. Hi,
    I registered the ATU_WindowsHandler.DLL
    I unable to add jacob-version-x86.dll reference
    Can you provide file upload in firefox in c# lang?

    ReplyDelete
  14. can you please provide the link to download SOB

    ReplyDelete
  15. Firefox authentication flow works fine only on a Desktop in Unlocked state. Is there a way to get it working on Locked screen as well? I get the following error message when I run the tests on machine where desktop is in locked state: OS windows 7 64bit
    Exception in thread "main" [ATU Windows Handler Framework Exception] Unable
    to Type Text for the Given Element
    at atu.utils.windows.handler.WindowHandler.typeKeys(WindowHandler.java:348)

    ReplyDelete
  16. Hi facing a problem in coding while dialog box popup and select a item form drop-down and click on select button.
    Can anyone help regarding this!!

    ReplyDelete
  17. Hi facing a problem in coding while dialog box popup and select a item form drop-down and click on select button.
    Can anyone help regarding this!!

    ReplyDelete
  18. I'm trying to use this with Selenium and Java. I cannot handle "WindowHandler handler = new WindowHandler();", it doesn't know what to do with WindowHandler. What am I missing?

    Jacob is defined as follows:

    System.setProperty("jacob.dll.path", "C:\\Users\\CookP\\workspace\\jacob-1.18\\jacob-1.17-M2-x86.dll");

    System.loadLibrary("C:\\Users\\CookP\\workspace\\jacob-1.18\\jacob-1.17-M2-x86.dll");

    ReplyDelete
  19. please tell me how to use function findelementbyaccesskeys and how to send combination of access keys.

    ReplyDelete
  20. Uploading file is working fine for me on Firefox but on Chrome its unable to identify the window.

    WindowElement firefoxElement = handler.getWindowElement("Sample File Upload Form");

    ReplyDelete
  21. Hello,

    I am facing issues regarding the handling of windows dialog on windows 10 machines. On windows 7 machines I do not have the issues.

    On of the issue is that the dialog cannot be found. I believe it is because the dll is outdated, but can somebody help me?

    Kind regards

    ReplyDelete
  22. This blog is really helpful. But I am facing issues with .dll file. Where can I find it?Please help me

    ReplyDelete