Thursday, 8 August 2013

Simple solution to automate Selenium tests of Uploading/Downloading file from/To local Disk for Firefox.

I have observed that many automation engineer gets stuck while automating Upload/download File  from/to local disk ,related tests with selenium, I would like to take this opportunity to throw some light on it

Autoit is one of the option to download or upload the file but many automation framework developer avoids the use of third party tools with their framework, Even though it’s an open source, it could get licensed in future.
Apart from AutoIt, there is couple of other solutions to achieve I tried to list it down along with the open issues for the provided solution.
Here is what we already tried and the problems we faced along with the actual solution :)

Solution 1 : Uploading a file using Autoit & WebDriver
Refer a blog : blog1 and blog2

Solution 2: Download programmatically using href attribute of button/link/image
Problem: Works only for button/link/image having href pointing to actual file location. IPP uses javascript as value for href. So cant be used.

 Solution 3: Use user32 (JNA)
            Fine for tasks like checking window existence, setting focus on specific window etc.
Problem: But can’t identify objects on window (buttons, checkboxes etc. on window).

 Solution 4: Use SendKeys on window under focus
            Problem:  Firefox popups doesn’t respond to keys send programmatically.

 Solution 5: Use Send keys using selenium
             Problem:  Selenium sends keys only on objects which Selenium identifies. Firefox’s ‘File Download’ popup is not recognised by Selenium.

 Solution 6: Use AutoHotKey tool
             Problem:  Firefox’s ‘File Download’ popup doesn’t respond to keys send by AutoHotKey.

Solution with which I moved ahead and found very simple.
Simple Solution: Use firefox profile to handle file download
Firefox settings to manage File Download dialog in Selenium:

1.       Disable “Show the Downloads window when downloading a file”:
2.       Add default download path to profile being used for Selenium run.

3.       Download each file type (which you will be downloading during test runs e.g. .xls, csv, txt etc.) once and check the “Do this automatically for files like this from now on” option.
1.       Above options (1,2 and 3) once set, will automatically download selected file types to folder specified in step 2.
2.       Above options are stored in the current firefox profile.
3.       All these options can be changed using Applications tab in Firefox’s options.

Even internet explorer and chrome are having those options so that we can download the file at one fix location so that  you will not have to work on download window.
Feel free to revert back.

Friday, 2 August 2013

How to deal with multiple frames with dynamic Ids using webdriver.

Here I would like to address an issues that I faced while upgrading the existing automation framework of selenium webdriver  to support HTML5 UI.
Earlier Application Under Test (AUT) was developed in Icefaces means all tables, popups,controltypes were developed in icefaces. later on management decided to revamped the IceFaces UI with HTML 5.
To render the old UI of Icefaces on HTML5 , developers has deployed an additional Iframe for each view. And it has created a big challenge for me to keep existing tests running on the new UI without modifying the tests. I followed following approach in updating the framework/keywords to make sure test won’t need an update.

Main challenge :
In this assignment the main challenge was to attach an iframes with dynamic IDs .Switching between different iframes, once user has changed the Views.
In this I was not aware of the frame ID to whom I have to attaché webdirver , because each frame was having a dynamic ID starting with String ‘Frame’ for e.g Frame1256893
I could use this as an xpath :
String frameXpath = "//iframe[starts-with(@id,'Frame')]";

But it will not solve all my problems because there could be multiple views opened at a time with only one view in a focus.if I would have been used only above xpath then it would have always attach the webdriver control to first frame

How can we achiev it ?

protected void attachToVisibleViewFrame() {
  String frameXpath = "//iframe[starts-with(@id,'Frame')]";
  WebElement[] framesElement = wedriverInstance.findElements(
    By.xpath(frameXpath), 3);
  if (framesElement != null) {
   for (WebElement frameElement : framesElement) {
    if (frameElement.isDisplayed() && frameElement.isEnabled()) {
       .println("PortalView : WebDriver control switched to a frame with label : "
         + frameElement.getAttribute("view-label"));

Above code additionally checks whether iframe for which webdriver is trying to get attach isDisplayed and it will always attach to a frame which is in focus and not to a frame which is hidden.
Hope this will help you to avoid an overhead of multiple frames within your application.