19

I know that normally you can login to sites that require HTTP basic authentication with Selenium by passing the username and password in the URL, e.g.:

selenium.open("http://myusername:myuserpassword@mydomain.com/mypath");

I've been running a Selenium test with Firefox 2 or 3 and there I still get the "Authentication Required" dialog window?

Update: It seems not to be a Selenium problem but rather a Firefox issue. If I enter the URL manually within FF I'll get the authentication dialog, but if I enter the URL in Opera, my page is displayed without showing an authentication dialog.

8 Answers 8

11

I have a solution for Firefox and Internet Explorer.

For Firefox, you need to go into about:config and create the integer network.http.phishy-userpass-length with a length of 255. This tells Firefox not to popup an authentication box if the username and password are less than 255 characters. You can now use http://user:pass@domain.com to authenticate.

For Internet Explorer, you must edit the registry. In the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create the DWORD values iexplore.exe and explorer.exe and make sure their values are 0.

I had to override NTLM authentication aswell. To NTLM authenticate using the HTTP basic authentication syntax in Firefox, simply specify the domains being used in the Firefox config string network.automatic-ntlm-auth.trusted-uris (accompanying the first config option). This will work in IE with the registy edit alone.

2
11

Contributing to Druska´s answer, you can do the same configuration using Selenium 2 API:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setPreference("network.automatic-ntlm-auth.trusted-uris","yourDomain");
new FirefoxDriver(profile);

This approach is simpler and you do not have to ask every developer to change their Firefox configuration. I only tested with the Firefox driver.

UPDATE:

For some reason (maybe the ones explained at https://stackoverflow.com/a/14348701/256245), the above solution does not work with newer versions of Firefox. Here is what works for me now (tested with Firefox 19.0.2):

  1. Install AutoAuth Firefox plugin;
  2. Visit the site where the authentication is needed. Enter your username and password and make sure to choose to save the credentials;
  3. Save AutoAuth installation file at your hard drive: at the plugin page, right click at “Add to Firefox” and “Save link as”;
  4. Instantiate Firefox webdriver as following:

    FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
    File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi");
    firefoxProfile.addExtension(pluginAutoAuth);
    return new FirefoxDriver(firefoxProfile);
    

Make sure to instantiate the pluginAutoAuth File with the correct path where you saved the plugin installation. If you do not feel comfortable using the default profile, you can use Firefox Profile Manager and create one specific to your tests.

Reference to this new solution: http://watirmelon.com/2012/06/27/automatic-firefox-authentication-when-using-selenium-webdriver-with-autoauth/

7

Add a slash after the context root:

Instead of: selenium.open("http://myusername:myuserpassword@mydomain.com/mypath");

use: selenium.open("http://myusername:myuserpassword@mydomain.com/mypath/");

It makes all the difference of the world adding the slash at the end of the context root. Without the slash, the popup opens, with the slash it gets authenticated as expected.

Note, that this is not a selenium bug or whatnot, but a firefox thing. You can use your command line as well to see for yourself:

 C:\Program Files\Mozilla Firefox>firefox http://myusername:myuserpassword@mydomain.com/mypath/

For me, it works even without settings the networks uris:

FirefoxProfile profile = new FirefoxProfile();
//profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "mydomain.com");
//profile.setPreference("network.negotiate-auth.trusteduris", "mydomain.com");

WebDriver driver = new FirefoxDriver(profile);

driver.navigate().to("http://myusername:myuserpassword@mydomain.com/mypath/");


versions
Firefox 19.0,
selenium-java 2.31.0

4
  • 2
    Wish I could give you more upvotes for mentioning the that the ending slash is required... amazing. Was banging my head against a wall... Feb 12, 2016 at 12:12
  • 1
    Wow. i'm struggling from day. This is just solved my problem with one /. Thank you
    – bhasker
    Nov 16, 2016 at 8:48
  • Helps me, too. Unbelievable. I am using chromedriver. Jan 3, 2017 at 10:33
  • @DavidSpence you could award bounty, that's kind of "giving more upvotes" ;) Jun 2, 2017 at 9:19
5

If you are using the FireFox Driver ... You can create a FireFox profile and save the username/pass in password manager and use an add-on to auto login. Remember if you create a FireFox or Chrome driver in Selenium, by default it uses an anonymous profile. So none of your regular extensions/add-ons/etc will be used. So it's best ot create a profile that can be distributed and saved in source control.

1) In Windows, from the run/start menu type "firefox.exe -p" to bring up the Profile Manager and create a custom one and save it in a location with the rest of your code.

2) Don't ask at startup is checked

3) Download AutoAuth add-on https://addons.mozilla.org/en-US/firefox/addon/autoauth/

4) Visit the site that requires HTTP Basic Authentication and save the credentials

Next time you visit the site, AutoAuth will login you without the authentication required prompt showing up.

If you have NTLM, you can modify the configuration setting to include the host names: network.automatic-ntlm-auth.trusted-uris

2

You could try to manipulate the headers directly like this:

First when you start, you have to enable Selenium ti manipulate headers:

selenium.start("addCustomRequestHeader=true");

Then you have to use some basic encoding and header manipulation like this:

    String authHeader = "";
    try {
    BASE64Encoder coder = new BASE64Encoder();
    authHeader = coder.encode("developers:Str492ight".getBytes());
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    setUpSelenium();
    startSelenium();
    selenium.addCustomRequestHeader("Authorization", "Basic " + authHeader);
    selenium.open("/");
    selenium.waitForPageToLoad("10000");

The space after Basic is necessary. This is how a basic HTTP authentication header looks like..

Further more you could use some Http Watchers to see if the request contains your auth request.

Either use Wireshark, or better is Fiddler or Charles Proxy.

Hope that helped. Gergely.

3
  • Hi Gergely, thanks for the hint. I've tried it unfortunately the authorization window still pops up. I tried to capture the HTTP headers with Firebug and TamperData. With both on my initial request on the URL myusername:myuserpassword@mydomain.com/mypath I don't see any request. Only after entering the username/password in the auth window, the request is send and the HTTP header contains the Basic auth attribute.
    – Peter
    Jun 11, 2010 at 9:32
  • Damn... I can only think of what is left is looking into the log of the server it self if it was received or rejected... I'm sorry i don't have any ideas left :(
    – Hannibal
    Jun 11, 2010 at 10:15
  • Peter, You can only add request headers if you're using the Selenium server as a proxy. I've written an article up about this: mogotest.com/blog/2010/06/23/… Hopefully that helps you out.
    – nirvdrum
    Jun 25, 2010 at 17:28
0

As mentioned, the addCustomRequestHeader solution can only work with proxy injection mode. But when I tried to implement it, I got into other issues related to that proxy injection mode.

It's not clear to me if proxy injection even work at all when using the Java client. Anytime I would call open(), I got a weird error stating: "this.onXhrStateChange.bind is not a function". The only solution I found implied that you need to add an extra parameter with the value 'true' to the open() method but the Java client API only accepts a single parameter.

So I had to settle for the browser config solutions explained above which I don't really feel comfortable with since they depend on the vendor's willingness to support them.

Porting your tests to Selenium 2 (still alpha as of now) might be a better prospect but in my case it won't be possible until Selenium Grid supports Selenium 2.

Hope that can help anyone, Sebastien

2
  • addCustomRequestHeader does not require proxy injection mode. It requires that the browser use Selenium server as proxy. This is the difference between "*iexploreproxy" and "*piexplore". If you decide to create your own launcher, you even could use the HTA launcher against the Selenium proxy in IE, for example. I highly recommend you avoid proxy injection.
    – nirvdrum
    Aug 12, 2010 at 12:09
  • Thanks nirvdrum. I might give the addCustomRequestHeader another try then. Actually I got the idea from your blog but I was trying to implement it in Java. Being relatively new to Selenium, I got confused between proxy injection and using selenium rc as a proxy. Aug 12, 2010 at 15:43
0

Firefox 17 'username:password' (RFC1738) processing is disallowed by default in Firefox (it had worked earlier). However, I've found that it can be re-enabled by:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.negotiate-auth.trusteduris", hostname);
driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
selenium = new WebDriverBackedSelenium(driver, "http:// + username + ":"
    + password + "@"
    + hostname + ":" + port + baseUrl);

Works on Selenium 2.28.0, Firefox 17; used for DigestAuth login.

0

Well, you can make you use of the Sikuli script to handle this Firefox Authentication popup in Windows as well as in Linux environment.

  • Download and Setup Sikuli in Windows/Linux(Need to install dependencies)
  • Use the following Sikuli Script to handle popup: where Authentilcat1.png is the popup image and it will handle 100 popups

for i in range (100): while exists(Pattern("Authentlcatl.png").similar(0.99)): print("Found Authentication Popup") wait(2) type("admin" + Key.TAB) type("admin" + Key.ENTER)

  • Use the following code to trigger and terminate the Sikuli script from Java code:

To Trigger the Sikuli Script:

String[] lincmd = { "bash", "-c", "sudo java -jar Sikuli-X/Sikuli-IDE/sikuli-script.jar Sikuli-X/Sikuli-IDE/new1.sikuli/" };

java.lang.Runtime.getRuntime().exec(lincmd);

To Terminate the Sikuli Script:

String[] lincmd = { "bash", "-c", "sudo kill $(ps aux | grep '[s]ikuli' | awk '{print $2}')" };

java.lang.Runtime.getRuntime().exec(lincmd);

  • After triggering the Sikuli script from Java code,Sikuli script will run as another process separately,so finally in the Java code terminating the Sikuli script.

  • So whenever the popup appears in the screen,Sikuli script will handle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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