Handling Authentication Window with WebDriver (In Firefox, Chrome and IE)

When you are working in a test environment, Stage or Pre Production, there are cases where you may need to work with applications which are secured with Authentication (Basic Auth).

When ever you enter the URL, it will prompt you to enter the User name and the password and It will not allow to perform any further operations until you provide username and password. And this Authentication pop-up is not a JavaScript pop-up, it is a Browser dialog window which selenium cannot handle simply using sendKeys method which we do for normal JavaScript pop-ups..

To work with Basic Authentication pop-up (which is a browser dialogue window), you just need to send the user name and password along with the application URL.

Syntax:

driver.get("http://admin:admin@yoururl.com");

Check out the example below to execute in Firefox browser:

@Test
	public void testBasicAuth_Firefox() {

		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.get("http://admin:admin@yoururl.com");
		//To check if we have landed in the correct place
		String text = driver.findElement(By.className("home")).getText();
		Assert.assertTrue(text.contains("Welcome"), "Basic Authentication failed");

	}

Check out the example below to work with Chrome browser:

@Test
	public void testBasicAuth_Chrome() {

		System.setProperty("webdriver.chrome.driver", "G:/Jars/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("http://admin:admin@yoururl.com");
		//To check if we have landed in the correct place
		String text = driver.findElement(By.className("home")).getText();
		Assert.assertTrue(text.contains("Welcome"), "Basic Authentication failed");
	}

The best way to make it work with IE is using AutoIt tool. If not, you may need to change the stuff in registry, To change for the current user, you need to edit in 'HKEY_CURRENT_USER...' and if you want to do that for all users, you can set the value of register keys as 'HKEY_LOCAL_MACHINE...' etc.

Once you open the URL in IE it will look like the below screen shot: -
Basic authentication in IE using Selenium

Check out the example to work with IE using AutoIt tool.

First create AutoIt script as below and save it as basicauth.au3

; To pass user name and password
WinWaitActive("Windows Security")
Send("admin")
Send("{TAB}")
Send("admin")
Send("{ENTER}")

After creating the above AutoIT script, compile the script and take the location of the script exe file. Now the selenium code should look like below :

@Test
	public void testBasicAuth_IE() {
		DesiredCapabilities caps = DesiredCapabilities.internetExplorer();   
		caps.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");

		System.setProperty("webdriver.ie.driver", "G:/Jars/IEDriverServer.exe");
        WebDriver driver=new InternetExplorerDriver(caps); 
		driver.manage().window().maximize();
		driver.get("http://yoururl.com");
		try {
			Runtime.getRuntime().exec("G:/basicauth.exe");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

Hope the above solutions works.

Selenium Tutorials: 

Comments

How to pass the username and password to the windows authentication popup by using selenium webdriver commands

Hi there, this weekend is good in support of me, since this point in time i am reading this impressive informative post here at my home.

I am receiving the authentication pop up message (Firefox browser) between the test cases and not able to handle the pop up message there.

It results in the failure of the test cases run.

how to compile AutoIt file?

I have the same type of scenario wherein the windows popup doesnt come all the time and it is irregular
how to handle that type of scenario's.

You should know network.proxy.autoconfig_url, network.proxy.http, network.proxy.http_port
Example:
network.proxy.autoconfig_url = "http://abc.com/abc.pac"
network.proxy.http = "abc-proxy-xyz.com"
network.proxy.http_port = "8080"

Do below setup:

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("network.proxy.type", 1);
firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://abc.com/abc.pac");
firefoxProfile.setPreference("network.proxy.http", "abc-proxy-xyz.com");
firefoxProfile.setPreference("network.proxy.http_port", 8080);
firefoxProfile.setPreference("network.proxy.ssl", "abc-proxy-xyz.com");
firefoxProfile.setPreference("network.proxy.ssl_port", 8080);
firefoxProfile.setPreference("network.proxy.socks", "abc-proxy-xyz.com");
firefoxProfile.setPreference("network.proxy.socks_port", 8080);
firefoxProfile.setPreference("network.proxy.ftp", "abc-proxy-xyz.com");
firefoxProfile.setPreference("network.proxy.ftp_port", 8080);

WebDriver driver = new FirefoxDriver(firefoxProfile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("www.google.com");
return driver;

Your code worked perfectly. But I had one snag that users need to be aware of. AutoIt has some reserved characters. If they are in your password they need to be handled differently. In my case my password has a "!" in it which made it and the next character not show up. With long passwords that replace characters with dots 14 dots look a lot like 16 dots. It took a while to figure out what happened. In this case it needed "!" replaced with "{!}"

Hi ,

I need to handle window authentication using c#.

I am unable to use the same script file - compile and RUN command in c#.

tried adding the AUTOITX dll to the solution and using the script directly. still unable to achieve.
Please suggest.

Hi,
Does anyone has a solution for Chrome browser to interact with Basic Authentication using AutoIt. I tried the code below and it does not work. The same code works on FF.

WinWaitActive("Authentication Required","","120")
If WinExists("Authentication Required") Then
Send("username{TAB}")
Send("password{Enter}")
EndIf

Does anyone has any other solution?

After sending username and password to the authentication pop up , it again pops up.

WinWaitActive("Authentication Required")
Send("digicel")
Sleep(2000)
Send("{TAB}")
Sleep(2000)
Send("digicel@1234#")
Send("{TAB}")
Sleep(2000)
Send("{ENTER}")

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.