Take the 2-minute tour ×
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: stackoverflow.com/questions/5672407/… –  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

6 Answers 6

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

This is a common question among those who practice browser automation. See for example How do I handle the login modal dialog created by onLoad in a WebDriver & Java based test automation, and How do I download a file using Selenium's WebDriver?.

share|improve this answer
The first link indicates that there is an issue with Get that will block all the proposed solutions. The accepted solution for the second link indicates to not use Selenium at all but to use wget to download a file. Neither of these really helps me unless I missed something –  timDunham Dec 28 '11 at 20:06
You might try posting your question to the Selenium Google group. –  user246 Dec 28 '11 at 23:36
Thx for the suggestion –  timDunham Dec 29 '11 at 14:45
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

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", ".companyname.com");

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.

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.