Basic Authentication
(Looking for a Linux solution? See Solution #3 below.)
If you try and visit a site that uses basic authentication, this is the box you will appear. This wont appear in any unit tests, and its difficult to show working code, as we need a site that is secure, and we would need to supply passwords etc.

Simplest Internet Explorer Solution that avoids AutoIt entirely
Instead of using
Watir::Browser.start "http://yoururl.com"
you use:
Watir::Browser.start "http://username:password@yoururl.com"
All you need to do is set two registry values to make IE6+ allow this. You can find the full details on how to do this on Alister Scott's blog post.
Solution #1
One way to handle this is using WindowHelper.rb. This script is basically just a wrapper around AutoIt to help you deal with pop ups. The following code to implements this in an example using Test::Unit. Also, since you frequently see the Security Alert dialog along with Basic Authentication, this example deals with this pop up as well. You will need to have two other files, handle_logon.rb and handle_security_alert.rb in the same directory as your main script. These scripts are launched in additional threads to deal with the Basic Authentication pop up and the Security Alert pop up.
One problem with this script is that if you leave the Security Alert pop up thread in-tact and the script does not encounter this dialog box, the script will hang waiting for a Security Alert pop up that will never happen. To correct this, simply remove variable b and the b.join statement from main.rb.
require 'unittests/setup'
require 'watir'
require 'watir/WindowHelper'
class TC_Logon_Test < Test::Unit::TestCase
def setup
@url = 'yoursite.com'
@login_title = "Connect to #{@url}"
@username = 'uname'
@password = 'pass'
end
def test_logon
a = Thread.new {
system("ruby handle_logon.rb \"#{@login_title}\" \"#{@username}\" \"#{@password}\"")
}
b = Thread.new {
system('ruby handle_security_alert.rb')
}
$ie.goto(@url)
a.join
b.join
# continue code here...
end
end
require 'watir' require 'watir/WindowHelper' helper = WindowHelper.new helper.push_security_alert_yes puts "done with security alert"
require 'watir'
require 'watir/WindowHelper'
# these parameters are passed to the logon method in WindowHelper.rb
login_title = ARGV[0]
username = ARGV[1]
password = ARGV[2]
helper = WindowHelper.new
helper.logon(login_title, username, password)
Solution #2
Added 07/15/2008
Another solution that doesn't require launching new threads.
#if prompted about the security certificate use 'click_no_wait' to be able to deal with the popup ie.link(:text, 'Continue to this website (not recommended).').click_no_wait #use autoit to work with the authentication popup Watir.autoit.WinWait('Connect to ') Watir.autoit.Send('username') Watir.autoit.Send('{TAB}') Watir.autoit.Send('password') Watir.autoit.Send('{ENTER}')
Solution #3
Added 05/27/2010
How to do Basic Authentication using FireWatir on Ubuntu Linux?
(This is basically a copy of the post I made to the stack overflow link above. Credits to Aedorn Varanis for the basic auth code)
This solution lets you create tests that work in both ruby+firewatir and jruby+celerity on Linux in Firefox. Instead of using firewatir or celerity directly, I created a simple "browser" class which takes care of handling basic authentication for both frameworks (via jssh in firewatir, and by just yielding to the built in support for basic auth in celerity).
Now you can visit basic auth sites in by doing something like:
require 'browser' browser = Browser.new browser.credentials = 'user:pass' browser.goto('http://some.basic.auth.url')
Here's my browser.rb:
ENGINE = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby' if ENGINE == 'ruby' require 'firewatir' class Browser < FireWatir::Firefox def initialize(options={}) super(options) @username = nil @password = nil end def credentials=(string) username, password = string.split(":") if username.nil? or password.nil? raise "Invalid credentials: #{string})" else @username = username @password = password end end def goto(url, wait=3) if @username.nil? and @password.nil? return super(url) else t = Thread.new { logon(@username, @password, wait) } result = super(url) t.join return result end end private def logon(username, password, wait) jssh_command = " var length = getWindows().length; var win; var found = false; for (var i = 0; i < length; i++) { win = getWindows()[i]; if(win.document.title == \"Authentication Required\") { found = true; break; } } if (found) { var jsdocument = win.document; var dialog = jsdocument.getElementsByTagName(\"dialog\")[0]; jsdocument.getElementsByTagName(\"textbox\")[0].value = \"#{username}\"; jsdocument.getElementsByTagName(\"textbox\")[1].value = \"#{password}\"; dialog.getButton(\"accept\").click(); } \n" sleep(wait) $jssh_socket.send(jssh_command,0) read_socket() end end elsif ENGINE == 'jruby' require 'celerity' class Browser < Celerity::Browser; end else raise "Ruby ENGINE '#{ENGINE}' not supported." end
Comments (6)
Feb 18, 2009
Robert Papesch says:
Here is a single method that works without having to make a kludgy system call.....Here is a single method that works without having to make a kludgy system call..
//
Note, if the following error crops up, there are a couple of things to try...
Solution 1: Add this to the top of your script
Solution 2: Register AutoIt via MS-DOS
Now it oughta work...
Jun 11, 2009
Aaron OBrien says:
I got the above script to work for internet explorer, that is: require "wati...I got the above script to work for internet explorer, that is:
However if I use firewatir, with:
It doesn't work, I get the following error:
Any help will be really appreciated, I am a newby here and am really keen to use firefox instead of ie.
Jun 11, 2009
Aaron OBrien says:
With the help of some other forums, I got the following to work as an alternativ...With the help of some other forums, I got the following to work as an alternative, but again, it is only for IE, any ideas to get this working with firefox would be greatly appreciated.
Oct 15, 2009
Rick Herrick says:
I can get both of these solutions to work on their own. The issue I ran into was...I can get both of these solutions to work on their own. The issue I ran into was detecting the presence of the basic auth pop-up dialog. Everything works fine with these solutions (and the variations suggested in the comments) as long as the pop-up dialog appears!
But sometimes the pop-up doesn't appear (usually this is due to an instance of IE that has already authenticated being open on the desktop). In that case, the solution using the handle_logon.rb script just hangs, waiting, I guess, for something to happen. The solution using the browser.send_keys calls just pushes ahead, sending keys willy nilly regardless of their appropriateness.
The solution I came up with was using the Watir.autoit.WinWait call to get the window, as shown in Solution #2, but adding a timeout to the call. Once that returns, you can check the return value. The WinWait function returns 0 if it can't locate the indicated window within the specified timeout period.
If the window was found, I call Watir.autoit.WinActivate to bring the window to the front and Watir.autoit.Send as shown. I added one extra line to make sure the username text box is selected before sending the keystrokes (the ! represents the Alt key to the Send function, meaning that !u is Alt-U).
Of course, if the window wasn't found, we just continue on our way.
Hope this helps someone!
Mar 31, 2011
sun zhongying says:
Hello All, I follow the above ways to do that,But not sucess.Example,I want to ...Hello All,
I follow the above ways to do that,But not sucess.Example,I want to do the automation test for the wenku.baidu.com
please the steps:
1.goto wenku.baidu.com
2.click the "logon" text
3.pop-up the logon windows.
4. input the username and password,and submit.
I want to know,how to use watir to do.Please give me a more detailed code.I hope your help.
Thanks very much.please ASAP.
Tom sun.
Apr 01, 2011
Zeljko says:
We do not provide support via comments here. For support please see http://watir...We do not provide support via comments here. For support please see http://watir.com/support