How to perform Basic Authentication for FirefoxDriver, ChromeDriver and IEdriver in Selenium WebDriver?

64,282

Solution 1

True, BASIC HTTP authentication is not currently supported but I got it working now for FF and for Chrome.

The code I wrote in the questions works for those drivers. I just tried using FF3.6 as Firefox default browser (installed in Firefox folder) instead of FF4 (not supported yet). For IE, i may try to disable the authentication through Windows Registry.

This page http://code.google.com/p/selenium/issues/detail?id=34 may help.

Solution 2

I got it to work with Firefox webdriver by the following:

profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "google.com");
driver = new FirefoxDriver(profile);

driver.Navigate().GoToUrl("http://user:[email protected]");

Solution 3

Add this New Firefox Profile on your code

ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("myProjectProfile"); //replace "myProjectProfile" with your profile"
WebDriver driver = new FirefoxDriver(myprofile);

Firefox configuration settings

This works fine without prompting any authentication when you do the following settings..

  • Type "about:config" on your FF url
  • Now type "Proxy" in the search field
  • Make sure "signon.autologin.proxy" is set "true" (By default it is "false")

Load Default/Custom Chrome Profile to run tests using Selenium WebDriver


  1. Download chromedriver.exe
  2. Extract the chromedriver_win_26.0.1383.0.zip folder and locate .exe file to C:/ folder

Add this Script on your JAVA code

DesiredCapabilities capability = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
capability.setCapability("chrome.switches", Arrays.asList("–disable-extensions"));
capability.setCapability("chrome.binary", "C:/Users/user_name/AppData/Local/Google/Chrome/Application/chrome.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/Default");
driver = new ChromeDriver(capability);

Note: IE doesn't need profile setup to run tests because they run on Server user while Firefox and Chrome works with binary.

Solution 4

For more portability, this can be handled by stub API and using Alert.

Example Java code (sample):

import org.openqa.selenium.Alert;
import org.openqa.selenium.security.Credentials;
public void authenticateUsing(Credentials credentials) {
    private final Alert alert;
    alert.authenticateUsing(credentials);
}

See also: auth_tests.py

Or by sending keys manually like:

SendKeys("user");
SendKeys("{TAB}");
SendKeys("password");
SendKeys("~"); // Enter

See also the following feature request: #453 Portable BASIC Auth at GitHub

Related:

Solution 5

If you want to enable the http auth in Internet explorer, you have to edit the registry and add this (create keys if they are not present):

  1. in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create a DWORD iexplore.exe with a value of 0

  2. in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create a DWORD iexplore.exe with a value of 0

  3. Close and reopen Internet explorer

If you have a x64 IE, the path is a bit different :

  • HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
Share:
64,282
sebarmeli
Author by

sebarmeli

Software engineer. UI enthusiast.

Updated on February 03, 2020

Comments

  • sebarmeli
    sebarmeli over 4 years

    I am using the Selenium-Firefox-driver and Selenium-Chrome-Driver version 2.0a5 (Web Driver API), and I am trying to test a web app that has BASIC authentication (there is a popup that come up to authenticate the user when I hit whatever page, the popup is not part of the HTML).

    Now, I need to a strategy to authenticate the user in Firefox, Chrome and IE (I'm going to import the IE Driver soon).

    I was reading in few articles that I can set a Firefox profile for instance..something like:

    FirefoxProfile ffProfile = new FirefoxProfile();
    ffProfile.setPreference("network.http.phishy-userpass-length", 255);
    WebDriver driver = new FirefoxDriver(ffProfile);
    driver.get("http://username:password@hostname");  
    

    but it doesn't seem to work for me. Does anyone have a working solution for those browsers?

  • poisson
    poisson over 10 years
    Nice job! Would you mind sharing the source code of the extension?
  • Juan Lago
    Juan Lago over 10 years
    It is available in the extension itself.
  • LordOfThePigs
    LordOfThePigs almost 9 years
    This improves the situation, but doesn't seem to be enough if the server you are running your tests on uses http instead of https. On http, you get the login prompt, and you are still stuck.
  • user64141
    user64141 over 8 years
    I found an another Chrome plugin that does the same thing: "MultiPass for HTTP basic authentication" It works great.
  • Ripon Al Wasim
    Ripon Al Wasim about 8 years
    What to do for Chrome an IE?
  • Elmue
    Elmue almost 7 years
    And where do you specify username and password?
  • Elmue
    Elmue almost 7 years
  • Elmue
    Elmue almost 7 years
    I cannot see where your code specifies username and password.
  • Prashanth Sams
    Prashanth Sams almost 7 years
    @Elmue this script bypass your username and password
  • Elmue
    Elmue almost 7 years
    It may work if you are in a domain and Windows sends the credentials of your current session. But for me this is usless because my computer is not in a domain and I have to enter them manually into te browser. In this case your code will not work.
  • Prashanth Sams
    Prashanth Sams almost 7 years
    Maybe in your case it's not needed