13

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?

11

Use
http://username:password@site.com/page
instead of
http://site.com/page.

  • Misha Akovantsev did the trick. I love simple solutions like this. If you have different environments (with/without password) you would have to use an "if" to handle this. Since the URL is already externalized, perfect solution. – Felipe Butcher Dec 29 '12 at 20:06
  • Hi @FelipeButcher - welcome to SQA! As you don't yet have the privs to add comments directly, I've taken the liberty of moving your answer to a comment on the post you were talking about, so that people can see it in the right place. – testerab Dec 30 '12 at 12:59
  • Awesome solution. The only disadvantage is that credentials will be logged on a server side as part of URL. But I believe this is minor security concern, as you probably already have those credentials harcoded in your test data and they are fake. – dzieciou May 17 '14 at 17:03
  • 1
    I tried this approach manually in IE and Chrome. And IE does not accept this URL syntax for security reasons: support.microsoft.com/kb/834489 – dzieciou May 17 '14 at 17:07
  • I wasn't able to get passed the HTTP auth with the ChromeDriver. This doesn't always work with Selenium. It's driver dependent. – zabumba Apr 18 '16 at 15:30
3

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.

  • That may work with Firefox, does that work for IE or Chrome? Failed for me using the ChromeDriver. – zabumba Apr 18 '16 at 15:32
1

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.

1

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.

  • Thanks for the response, iam using java (TestNG) with selenium and platform is windows machine firefox browser – saikrishna Mar 15 '12 at 8:36
1

If you´re using robotframework with or without RIDE IDE, this suggestion actually works for me In conjunction with Saved Password Editor

  1. Generate a new firefox profile by starting the ff profile manager (dos prompt: ff.exe -p)

  2. Start ff with this profile

  3. 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.)

  4. Goto your website, fill in the credentials, and save them in the password manager

  5. 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'

  • Yes, this is solution i use as well (its not limited to robotframework..). Set up browsers with AutoAuth or similar, save profile, specify to use that that profile when starting up browser/tests. Other option to pass username/pass in URL (top answer) can fail because of security, and any other way will not work with selenium grid. – George Oct 12 '16 at 16:13
0

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.

0

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.

0

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()
0

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

  1. Desired Capabilities need to be set in the Open Browser command. (${preferences} = Create Dictionary webdriver_accept_untrusted_certs true webdriver_assume_untrusted_issuer false)

  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.