Tuesday, October 25, 2011

Selenium WebDriver tips

Since the beginning of my current automation project we've been encountering issues that seemed to be real showstoppers. These issues were not only time-consuming but could also stop the whole idea of automated checks. Fortunately thanks to the Selenium WebDriver community we were able to handle every problem challenge. Here are some tips that were useful in our context:

1. HTTP Basic Authentication with Selenium WebDriver
Our automated checks needed to be run against web server with HTTP Basic Authentication.

Firefox Authentication Window


IE Authentication Window


Basically the authentication prompt is not available from the Selenium WebDriver. One of the way to handle this limitation is to pass user and password in the url like like below:
http://user:password@example.com

Before we can pass username and password in the url we need to make sure that given web browser let us use this feature.

Firefox
You need to change the following flag:
browser.safebrowsing.malware.enabled

You can change the state of this flag in the default Firefox profile using about:config service page (double click on the flag to change it's state) From now on the Firefox should let you go through http authentication using name and password in the url. (Note that if you are using Selenium WebDriver 2.6 or higher this flag should be disabled by default)

Internet Explorer
Because of the security reason IE by default has this feature disabled. To overcome this limitation you need to update a windows registry as described in this article : http://support.microsoft.com/kb/834489
You need to go to regedit and look for the key

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE

Then you need to add a DWORD 'iexplorer.exe' with '0' value in this key:


This functionality should be available immediately after restarting IE browser

In case your authentication server requires username with domain like "domain\user" you need to remember about adding double slash “\\” (or "%5C" symbol in case of IE) to the url you use in Navigate().GoToUrl() function

http://localdomain\\user:password@example.com
http://domain%5Cuser:password@example.com



2. Same Protected Mode across all zones on IE 8
To be able to run WebDriver on IE you need to equalize the Protected Mode settings for all zones in IE8. It's easy when you are in charge of your computer security policy, however it might be difficult when your machine works under global company policy. In that case you may be not able to change this settings even with local administrator rights.



One of the way we found useful is to update these settings through the windows register in following register keys:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones

and equalize values for 0x2500 position in all zones.


Value: 3 (Protected Mode off)
Value: 0 (Protected Mode on)


[UPDATE 1]
It seems there is another way to overcome this IE limitation, however we haven't tried it yet - http://www.qaautomation.net/?p=373

[UPDATE 2]
Above tips were tested on : WebDriver 2.6, Windows 7, Firefox 7, IE8

[UPDATE 3]
Thanks to Damian Galek:

Another way of bypassing Selenium limitation, regarding not being able to reach Windows Authentication prompt, that actually worked for us, could be using SendKeys class (one of the class in System.Windows.Forms.dll library – link). It can be used to type whatever we need into any Windows-based application. So in our solution something similar to thefollowing code was used to enter any desired users credentials and accept authentication prompt:

Driver.SwitchTo().Alert(); #Web driver function
System.Windows.Forms.SendKeys.SendWait("Down}");System.Windows.Forms.SendKeys.SendWait("Down}");System.Windows.Forms.SendKeys.SendWait(username);System.Windows.Forms.SendKeys.SendWait("TAB}");System.Windows.Forms.SendKeys.SendWait(password);System.Windows.Forms.SendKeys.SendWait("{Enter}");Driver.SwitchTo().DefaultContent(); #Web driver function

This may not be the cleanest solution out there but it works.


Alek

29 comments:

  1. Aleksander, I really appreciate your post, but here (under Firefox Ubuntu) your suggestione does not work for me.

    ReplyDelete
  2. Yes this is possible. I should have probably mentioned that I have checked this on Windows 7 only.

    Good luck with this on Ubuntu OS !

    ReplyDelete
  3. I am trying the firefox solution you have given. Have set the flag browser.safebrowsing.malware.enabled to false > Restarted firefox > Entered the username-password like you have given http://username:password@stg.mycompany.com but I still get the auth prompt. Is it because of the subdomain in the URL? The URL of the site is stg.mycompany.com.
    Please help.

    ReplyDelete
    Replies
    1. I am facing the same problem ..did u gt a solution for this.

      Delete
    2. I am facing a similar problem??Can u plz help me out

      Delete
    3. Thanks Aleksander I have been searching for this long time. Keep posting :)

      Delete
    4. Tried a lot of handlers but seems to fail. Is there a work around?
      I have been facing same issue here.

      Delete
    5. I am having the same problem, please update if you guys found any solution.

      Delete
  4. @Monali
    Maybe your site calls a different site in the background (retrieves some data) and you are simply not logged in to that site. If this is the case, you need to log in using above method to all sites you retrieve data from. This often happens if images on your site are stored on another site (e.g. profile photos).

    ReplyDelete
  5. Very Informative article on Selenium WebDriver ...Thanks for sharing

    http://whiteboxqa.com/#selenium.php

    ReplyDelete
  6. @Aleksander I have a question like i have three machines where i need to run webdriver in machine 1 and invoke selenium RC in another two machines (2 & 3) and run selenium scripts on those machines individually. After that i need to take the results log and dump it into results folder in machine 1. Can you suggest me any solution for this ?

    ReplyDelete
  7. @Sushant I am not sure if I understand your question correctly but it seems that the thing is in the copying result from one machine to another only. If so then without more details (eg. environment) I am not able to help you.

    regards,
    Alek

    ReplyDelete
  8. @Aleksander thanx for the authentication problem solution, i was also facing the issue and ur trick is worked :)

    by the way i wanna ask that is there any option for Xpath, or we must have to collect Xpath of all required elements...

    ReplyDelete
  9. @Aleksander I have been using Selenium Web driver using c#. Solution does not work for me. I have done the required setting for controlling the authentication pop up. But still I see the pop up
    projecting when I run the test.

    I run the test from Visual studio.

    ReplyDelete
  10. After typing in http://user:password@example.com and hitting enter in firefox (9.0.1), I get another Confirmation box
    You are about to log in to the site "xxxxx" with the user name "yyyyy".
    This has a Ok and Cancel. How to deal with this?

    ReplyDelete
  11. After typing in http://user:password@example.com in firefox (9.0.1) I still get another confirmation pop up with "You are about to log in to the site "XXXXXX" with the user name "yyyyyy". with OK and Cancel button. How to deal with this?

    ReplyDelete
  12. i face some problem when i use slenium 2.0 webdriver to handle ajax application then i can not recognize the element present on the page..
    so how can i tackle this problem ...



    thanx in advance
    Anurag Sharma

    ReplyDelete
    Replies
    1. Without the source code for this element noone is able to help you

      Alek

      Delete
  13. In firefox 10 it seems not working.It asks for username & password again.Please help.


    Thanks,
    Ketan

    ReplyDelete
  14. Hi Alek,

    I get HTTP basic authentication when I click a button How do I handle it ?

    I am using firefox.

    ReplyDelete
  15. Thanks very much for this.
    BTW The registry entry should read iexplore.exe not iexplorer.exe
    Worked fine once I worked that out

    ReplyDelete
  16. FirefoxProfile profile = new FirefoxProfile(new File("D:\\FP Profile"));
    WebDriver base= new FirefoxDriver(profile);

    ReplyDelete
  17. Try this:
    copy your firefox profile to a specific location first.

    FirefoxProfile profile = new FirefoxProfile(new File("D:\\FP Profile"));
    WebDriver base= new FirefoxDriver(profile);

    This will take the current user profile and will not prompt for credentials. It works for me.

    ReplyDelete
  18. Hi Alek, thanks for the useful post. I'm running into an issue with Firefox and wonder if you have any ideas. I'm working with a site that requires Basic Authentication. With no changes to the Firefox profile, I can request the start page, and, when prompted, enter my Basic Auth credentials, and everything is fine. Next I set browser.safebrowsing.malware.enabled to 'false', and request the start page, using the https://user-name:password@mydomain/startpage format. As usual, the request for the start page triggers dozens of other requests for various images, style sheets, and javascript files (all with the same domain). The start page is returned, but I get presented with Basic Auth dialogs for some of these subrequests. I can watch the traffic using HTTPFox, and I can see which requests trigger the Basic Auth dialog (usually about a third of the total requests). Each time I do this, the requests which trigger the Basic Auth dialog are different. For instance, a CSS file which was returned with no issues one time, might require Basic Auth credentials the next time (a few minutes later). I can see no pattern.
    If I just hit Cancel on all the Basic Auth dialogs, I get a page with missing images, styling, etc. If I then hit refresh, the page renders completely, and those missing files are shown as having been retrieved from cache.
    It's like a timing thing...like too many requests going out at once, and the credentials-in-the-url thing doesn't work for all of them. But, by the time I refresh the page, all the files have been retrieved.
    I just don't know enough about the inner workings of Firefox to understand what's going on here? Do you have any ideas? Thanks!

    ReplyDelete
  19. I still have an issue
    Using IE9-Windows 7
    Authentication failed shows up!
    What to do?

    ReplyDelete
  20. Not resolved my issue!
    Still getting authentication failed msg!
    Windows7-IE9

    ReplyDelete
  21. i am unable to make it run in IE.
    I still get the pop-up box which asks for my authentication.
    Any suggestions?

    ReplyDelete
  22. This is so helpful...
    Thanks a lot

    ReplyDelete
  23. Hi,

    I am trying to run some selenium scripts in Jenkins using Xvfb(Healess mode), There are around 20 scenario's there. Sometimes in between scripts are holding may be due to "Browser Not Responded" cause.

    In Selenium WebDriver, other than Implicit and Explicit waits, How can we deal the issue when browser stops responding ?

    Suresh

    ReplyDelete