Sign up ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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 at 5:28

7 Answers 7

up vote 0 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

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 at 15:41

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

For Firefox this works:

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

DesiredCapabilities capability = DesiredCapabilities.Firefox();
capability.SetCapability(CapabilityType.Platform, "WINDOWS");
capability.SetCapability(CapabilityType.BrowserName, "firefox");
capability.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());

return new RemoteWebDriver(remoteAddressUri.Uri, capability);
share|improve this answer

i'm using this and it's working for me.

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.

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 at 11:39

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

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.