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

8

40

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?

Tut answered 15/4, 2011 at 4:29 Comment(3)
For chrome NTLM authentication issues, this answer might help https://mcmap.net/q/408830/-how-to-override-basic-authentication-in-selenium2-with-java-using-chrome-driverGrub
Browser dialog window Authentication Headers driver.get("http://username:[email protected]/")Leoni
How to open URL when password contains @ symbol ? Any suggestions.Biancabiancha
T
6

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.

Tut answered 19/4, 2011 at 0:12 Comment(1)
This is redeircted now to github.com/seleniumhq/selenium-google-code-issue-archive/issues/…Neubauer
R
10

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]");
Republicanize answered 5/10, 2012 at 22:47 Comment(1)
What to do for Chrome an IE?Multiparous
T
6

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.

Tut answered 19/4, 2011 at 0:12 Comment(1)
This is redeircted now to github.com/seleniumhq/selenium-google-code-issue-archive/issues/…Neubauer
J
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.

Jameejamel answered 26/4, 2013 at 5:48 Comment(4)
I cannot see where your code specifies username and password.Neubauer
@Neubauer this script bypass your username and passwordJameejamel
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.Neubauer
Maybe in your case it's not neededJameejamel
N
3

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:

Noel answered 3/5, 2015 at 20:17 Comment(0)
E
2

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
Elis answered 19/11, 2013 at 13:19 Comment(2)
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.Cosmorama
And where do you specify username and password?Neubauer
B
1

There is a solution for performing authentication with Selenium 1.x by manually setting the HTTP headers at http://mogotest.com/blog/2010/06/23/how-to-perform-basic-auth-in-selenium but I don't think this is transferable to Selenium 2, as you don't have access to the headers.

According to the information here 'Basic Authentication support for Selenium 2' was added in Selenium 2 Beta 2 but looking through the source code I can only see it implemented as a way of securing Remote Selenium Servers against anonymous access.

So I think the answer is that BASIC HTTP authentication is not currently supported.

Billingsley answered 15/4, 2011 at 15:52 Comment(0)
F
1

I was not able to use the basic authentication with Selenium 2 and Chrome (Due a bug with Chrome), so I created an extension for Chrome that sends the basic authentication credentials automatically (See https://chrome.google.com/webstore/detail/basic-authentication-auto/dgpgkkfheijbcgjklcbnokoleebmeokn).

Fetter answered 10/8, 2013 at 20:32 Comment(3)
Nice job! Would you mind sharing the source code of the extension?Taphouse
It is available in the extension itself.Fetter
I found an another Chrome plugin that does the same thing: "MultiPass for HTTP basic authentication" It works great.Graphics
S
0

Multipass extension of Firefox made automation engineers life easy. Through this, we can handle the basic authentication pop-up in any browser using any programing language. PFB the steps:

  1. Open the Firefox browser and download the plug-in -> https://addons.mozilla.org/en-US/firefox/addon/multipass/

  2. Now go to the below location to get the XPI file that is the 'multipass' executable file for firefox -> C:\Users\Your user name\AppData\Roaming\Mozilla\Firefox\Profiles\oleovwxr.extensionUser\extensions

  3. Copy the file '[email protected]' from the above directory and past it to your project directory inside any folder of the resource package.

  4. Now use the below code snippet to configure the Firefox driver.

    public WebDriver config() {

         System.setProperty("webdriver.gecko.driver", "Path to geco driver");
         DesiredCapabilities capabilities = new DesiredCapabilities();
         capabilities.setCapability(CapabilityType.BROWSER_NAME, "firefox");
         capabilities.setCapability(CapabilityType.PLATFORM_NAME, "WINDOWS");
         capabilities.setCapability("acceptSslCerts", true);
         capabilities.setCapability("marionette", true);
         FirefoxProfile profile = new FirefoxProfile();
         //Give the multipass path
         profile.addExtension(new File("c:/your project name/src/main/resources/[email protected]"));
         FirefoxOptions firefoxOptions = new FirefoxOptions();
         firefoxOptions.setProfile(profile);
         firefoxOptions.merge(capabilities);
         return new FirefoxDriver(firefoxOptions);
     }
    
    1. Now the main challenge is to get the UUID of the downloaded multipass extension as whenever we run it changes. So we are taking every time when run.

    public void setup() throws InterruptedException {

         WebDriver driver = config();
         driver.get("about:debugging#/runtime/this-firefox");
         Thread.sleep(4000);
         String uuid = driver.findElement(By.xpath("//span[@title='MultiPass for HTTP basic authentication']/parent::li/section/dl/div/dt[contains(text(),'UUID')]/parent::div/dd")).getText();
         System.out.println("My Url:::" + "moz-extension://" + uuid + "/popin.html");
         driver.get("moz-extension://" + uuid + "/popin.html");
         //change below URL with your URL and username and password
         driver.findElement(By.id("url")).sendKeys("http://mywebsite.com");
         driver.findElement(By.id("username")).sendKeys("site user name");
         driver.findElement(By.id("password")).sendKeys("site password");
         driver.findElement(By.xpath("//button[.='Add']")).click();
         //Now change below URL with your url, note:: the domain should math with above multipass url
         driver.get("http://mywebsite.com/homeLogin.html");
     }
    
  5. I have tested with the below selenium version selenium-java 4.1.1 and selenium-server 3.141.59.

Specify answered 29/3, 2022 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.