Elegantly handling basic browser authentication with Watir

In programming, I find there’s little that compares to the feeling of deleting code; like finding a one or two-line solution that means you can remove a good-sized chunk of code no longer needed.

A fellow ThoughtWorker (who I shall call the Awesome-est Tom™) showed me a solution to handling the basic browser authentication in Internet Explorer, which effectively meant I could delete lines and lines of ruby code that called the AutoIt dll to handle the modal authentication dialog.

Internet Explorer Basic Authentication Dialog
Internet Explorer Basic Authentication Dialog

The code I had was loosely based upon the solutions available on the Watir wiki, all which roughly use the same technique of locating and authenticating the authentication dialog using AutoIt. Because the dialog is modal, the code I had used to run in a separate thread, and this caused me problems with debugging, and I find if I can avoid AutoIt, then my Watir tests run a lot more reliably.

Tom’s Occam’s Razor solution simply embeds the username and password in the URL that usually triggers an authentication dialog.

Instead of using:

Watir::Browser.start "http://yoururl.com"

you use:

Watir::Browser.start "http://username:password@yoururl.com"

This means you don’t see the authentication dialog at all! No more messy AutoIt code. The only tricky part is ensuring that Internet Explorer allows you to do this. Since Internet Explorer 6, this has been disabled by default, but it’s a simple matter of enabling the functionality by setting two registry keys.

This can easily be done running a .reg file you can create, that sets the required values:

Windows Registry Editor Version 5.00

You then run this file from the command line: regedit.exe /s IESecurityURL.reg and Robert is your father’s brother.

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

13 thoughts on “Elegantly handling basic browser authentication with Watir”

  1. I’m having trouble getting this to work in with ruby version 1.8.7. The error when I try to open a new browser window using this method is:

    C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.9.2/lib/watir/ie-class.rb:370:in `method_missing’: navigate (WIN32OLERuntimeError)
    OLE error code:800C000E in

    HRESULT error code:0x80020009
    Exception occurred.
    from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.9.2/lib/watir/ie-class.rb:370:in `goto’
    from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.9.2/lib/watir/ie-class.rb:104:in `start_window’
    from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.9.2/lib/watir/ie-class.rb:97:in `start’
    from C:/Ruby187/lib/ruby/gems/1.8/gems/commonwatir-1.9.2/lib/watir/browser.rb:71:in `start’
    from access_columbia_staging_homepage.rb:7
    >Exit code: 1

    I had version ruby version 1.8.6 with an old watir version 1.3.7 running on Windows XP machine and this worked perfectly. I’ve recently upgraded to a Windows7 and installed ruby 1.8.7 with watir 1.8.7 and now this super-simple authentication doesn’t work! I’m bummed. Any ideas?


  2. I tried modifying the Registry as suggested but it does not log in to the application in fact it just takes the Username and password and just launches the browser…. please let me know if anything else needs to be handled.


  3. To get this working on a 64-bit install of Windows, needed registry keys can be located in “Wow6432Node” as follows:


Comments are closed.