Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm attempting to create some selenium tests for a website that requires windows authentication. I got my first test up and running fairly easily, but it pauses as soon as the system prompts for credentials.

Is there a way to provide these credentials that will still allow me to run the tests across all the major browsers?

share|improve this question
There is an answer here that may help:… – Sam Woods Dec 29 '11 at 18:11
I tried that one, however it only works for Basic authentication. I need windows integrated authentication. – timDunham Dec 29 '11 at 22:28
Refer to my response here [Windows Authentication using Selenium and Java Thread][1]. [1]:… – sadipan bhattacharjee May 6 '15 at 5:28

I've found that WebDriver works with IE 9 and Windows / NTLM authentication via using Windows Impersonation and IE's automatic logon feature.
An example of the impersonateValidUser method you'll need to call can be found here: Impersonate a Specific User in Code.

Essentially you want to do the following (ensuring that IE 9 is configured to "Log me in automatically"):

impersonateValidUser("DifferentUser", "DOMAIN", "Password");
// WebDriver now logged in as "DifferentUser"
IWebDriver webDriver = new InternetExplorerDriver();

To revert back to the user running the tests / Selenium RC just call:

share|improve this answer
did not work for me – ColacX Feb 12 '15 at 15:41

For Firefox this works:


var profile = new FirefoxProfile();
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "");

return new FirefoxDriver(profile);


from selenium.webdriver import Firefox, FirefoxProfile

profile = FirefoxProfile()
profile.set_preference('network.automatic-ntlm-auth.trusted-uris', '')

return Firefox(firefox_profile=profile)
share|improve this answer
up vote 1 down vote accepted

I ended up creating a custom profile for Firefox and adding the AutoAuth addon. Not particularly elegant but it works. Credit goes to the following blog post:

share|improve this answer

Impersonation is tricky. I haven't done this with Selenium, but I have done it with WatiN and IE. I will post my WatiN solution which you could make work with Selemium (I think).

The trick is that you need to separate the launching of IE and the automation of IE as you can't just do them both in one hit.

First launch ie using System.Diagnostics.Process. Once you have launched IE you can then use the code from here to attach and talk to IE using impersionation

Here is the code

    public void TestMethod()
        SecureString password = new SecureString();

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.UserName = "localtest";
        psi.Password = password;
        psi.UseShellExecute = false;
        psi.LoadUserProfile = true;
        psi.FileName = "c:\\Program Files\\Internet Explorer\\iexplore.exe";
        psi.Arguments = "about:blank";

        Process proc = new Process();
        proc.StartInfo = psi;



    private static void DoWorkAs(object o)
        User u = o as User;

        IntPtr hToken = IntPtr.Zero;
        IntPtr hTokenDuplicate = IntPtr.Zero;

        if (Win32.LogonUser(u.UserName, u.Domain, u.Password, 2 /*LOGON32_LOGON_INTERACTIVE*/, 0 /*LOGON32_PROVIDER_DEFAULT*/, out hToken))
            if (Win32.DuplicateToken(hToken, 2, out hTokenDuplicate))
                WindowsIdentity windowsIdentity = new WindowsIdentity(hTokenDuplicate);
                WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate();

                // domain\username
                Console.WriteLine(" Thread 2 : " + WindowsIdentity.GetCurrent().Name);

                // This is WatiN code, you will need to replace with Selenium
                IE ie = IE.AttachToIE(Find.ByUrl("about:blank"));



        if (hToken != IntPtr.Zero) Win32.CloseHandle(hToken);
        if (hTokenDuplicate != IntPtr.Zero) Win32.CloseHandle(hTokenDuplicate);

    public class User
        public User(string u, string d, string p)
            Domain = d;
            UserName = u;
            Password = p;
        public string UserName;
        public string Domain;
        public string Password;
    public class Win32
        // P/Invoke snask
        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(
            string lpszUsername,
            string lpszDomain,
            string lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            out IntPtr phToken);

        [DllImport("advapi32.dll", SetLastError = true)]
        public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int
           SECURITY_IMPERSONATION_LEVEL, out IntPtr DuplicateTokenHandle);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool CloseHandle(IntPtr hHandle);


This code needs a refactor, and wont work on Vista with IE7, because of an IE bug that was fixed in IE8.

share|improve this answer

There are two ways to achieve this:

  1. You can directly pass the credentials in url so it will skip authentication window.

  2. You can also use AutoIT which will help you to acheive the same in your test.

Here is the article will guide you:

share|improve this answer

What I'm using as solution that works across OS and Browsers is a MITM Proxy. See which alows you to set the SSL Context yourself giving you the freedom wo use any method you want to gather that context (I use this for an Entrust based authentication using Smartcards and from a special Test certificate store) but usually stick with client certificates as this works across OS and especially from our CI Setup.

share|improve this answer

Javascript support for webdriver is pretty rough, so here's an example of this in Javascript, in case you're like me and need to go spend another half hour digging through documentation to find the right method names in JS:

let profile = new firefox.Profile();
profile.setPreference('network.automatic-ntlm-auth.trusted-uris', '')
let options = new firefox.Options().setProfile(profile);

let driver = new webdriver.Builder().

(Obviously ES6; if you're on Node < 4, change all instances of let to var)

share|improve this answer

I used Java Thread and Robot with Selenium to automate windows active directory authentication. You may refer to my response here Windows Authentication using Selenium and Java Thread.

share|improve this answer
First, for a question this old, your answer doesn't add anything of value. Second, your proposed solution from SE doesn't actually answer the OP's question, which is for a solution that works across ALL major browsers. – Kate Paulk May 6 '15 at 11:39

i'm using this and it's working for me. It parses the url and i enter inside it the username and password, this way i can do the authentication via URL.

public void login(String username, String password){
        WebDriver driver = getDriver();
        String URL = "http://" + username + ":" + password + "@" + "link";
share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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