I am automating an application which contains browser level authentication.
When I open the application, the browser performs basic authentication, i.e. it prompts for a username and password. How can I handle this scenario using Selenium?
Use
http://username:[email protected]/page
instead of
http://site.com/page
.
Here one method of handling, Create browser profile and check remind password. By calling the profile before the execution, It won't ask you the authentication next time onwards. Let me know if it doesn't work.
Basic authentication isn't handled very well in Selenium, and to be honest, I'm not certain that it should be either (although it would be nice). The solution that I've been using is to create a script in autoIT or something similar that is able to access the authentication box. Essentially, create the script that looks for the authentication box, enter credentials, click OK or press Enter and then either end or kill this sub script.
It's dirty, not ideal, but it's the best solution that I've found so far that can work with multiple browsers.
It would be better if you provide some more info on what language/platform are you using, but, anyway…
Selenium is not really good on handling such windows, so
If you are using Java, then please look at Robot class
If you are using Ruby, C#, Java, then look at WatiR, WatiN, WatiJ. All this libraries can attach to the existing browser by it’s title and handle the pop-up dialogs. I am not saying about replacing Selenium with WatiN, but you can use this library of this specific purpose.
Also for .NET you can use Windows UI automation
And, of course, you can use AutoIt or AutoHotkey. The most simplest way is to compile the dialog handling code in to .exe file and run it with specific parameters when it is necessary.
If you´re using robotframework with or without RIDE IDE, this suggestion actually works for me In conjunction with Saved Password Editor
Generate a new firefox profile by starting the ff profile manager (dos prompt: ff.exe -p)
Start ff with this profile
Install the add-on Auto-auth, (which allows you to log on automatically on sites with basic auth when you have stored the credentials in the password manager of ff; without this add-on, for every new session you will get prompted for the credentials, even if you stored them in the password manager.)
Goto your website, fill in the credentials, and save them in the password manager
Use this profile to run your robotframework tests: start the selenium server standalone with the option -firefoxProfileTemplate 'C:\Program Files (x86)\Mozilla Firefox...\path_to_this_new_profile'
Adding to Dmitry's and Lyndon's answer, you can also using Apple scripts if you are using a Mac machine. It is very efficient to handle such situations. Just need to run the scripts using java runtime class if you are using java.
I just got done working on a prototype project that is supposed to handle exactly this kind of situation. It is written in C#, so it currently only works for the .NET release of Selenium.
It utilizes BrowserMob, a popular open source proxy, to perform the authentication.
SeleniumBasicAuthWrapper Hope it helps! It is still a work in progress, but hopefully we'll get any kinks or defects ironed out in the near future.
Since this is one of the top hits on google ill provide the ruby solution if you cant use the http://username:password@url.
you can use system input but doing it on windows and on mac are different. Here is the solution on a mac:
You would have to detect if the test is running on windows or mac first and then do the corresponding.
t1=Thread.new{
@browser.get 'url';
}
t2=Thread.new{
sleep (10)
#replace these lines with the windows corresponding if on windows.
%x(osascript -e 'tell application "System Events" to keystroke "username"')
%x(osascript -e 'tell application "System Events" to keystroke tab')
%x(osascript -e 'tell application "System Events" to keystroke "password"')
%x(osascript -e 'tell application "System Events" to keystroke return')
}
t1.join()
t2.join()
Here is the windows solution:
t1=Thread.new{
@browser.get 'url'
}
t2=Thread.new{
sleep (5)
wsh = WIN32OLE.new('Wscript.Shell')
wsh.AppActivate('browser') #fx. firefox
wsh.SendKeys('username{TAB}password{ENTER}')
}
t1.join()
t2.join()
After opening the browser (Selenium2Library.Open Browser), I used the AutoItLibrary keywords as follows:
Win Activate Windows Security
Send <username>{TAB}
Send <password>{Enter}
This works for gc,ff and ie... for ff
Desired Capabilities need to be set in the Open Browser command. (${preferences} = Create Dictionary webdriver_accept_untrusted_certs true webdriver_assume_untrusted_issuer false)
profile needs to be set as follows:
profile setting as follows:
a) Close down any running Firefox instances.
b) Start Firefox (the one you're going to run your tests with) with the profile manager: firefox -ProfileManager
c) Create a new profile. You'll be prompted to choose a directory for the profile. Put it somewhere inside the project where you're writing the tests. Select the profile and run Firefox using it.
Browse to the HTTPS URL (with self-signed certificate) you're going to be testing against.
Accept the self-signed certificate when prompted. This creates an exception for it in the profile.
Close the browser.
Go to the Firefox profile directory.
Delete everything in the directory except for the cert_override.txt and cert8.db files.
When you run your Selenium server pass a -firefoxProfileTemplate /path/to/profile/dir
argument to it. This tells Selenium to use your partial profile (with certificate exceptions) as a basis for minting its new profile. So you get the certificate exceptions, but without any of the other clutter you would get if you used a whole profile.