How to handle browser authentication window in Selenium WebDriver?

In the previous tutorial, we discussed checkpoints in Selenium. Today we are going to discuss the technique to handle browser authentication window in Selenium; both tutorials are related to each other indirectly. Let me tell you how! Popping up of browser authentication window could be one of the checkpoints for testing the web application.

What is Browser authentication window?

A browser authentication window is pop up window which asks for user name and password to further proceed to the web application.

Followings are the pictures of the browser authentication window appeared on Mozilla Firefox & Google Chrome.

browser authentication window firefox

 

browser authentication window chrome

Why does the browser authentication window appear?

Some organizations have their private proxy server; hence, they have their own settings to get access to the server. If any user tries to open those servers through the browser then a window pop-up and asks the user to enter the login credentials to the server.

This window appears even when we are running our test automation pack. Such window hinders the test execution. Hence, testing gets discontinued, therefore, it is the need to handle browser authentication window in Selenium WebDriver.

Techniques to handle Browser Authentication window in Selenium WebDriver

We discussed WHAT & WHY of browser authentication window. Let’s discuss the different techniques to handle it in Selenium WebDriver.

Here we go!

Technique# 1: By passing user name & password in URL

This is the most common technique. Here we add user name and password as the prefix in URL. Hence the structure of the URL will become:

http://username:password@www.inviul.com

For example: If Username is Robert & Password is Pass12345 then the URL will be- http://Robert:Pass1234@www.inviul.com

On passing this URL, if you get any pop up then simply handle pop up by accept method. Click on below link to know pop up handling in Selenium:

How to handle pop up in Selenium?

Technique# 2: By using 3rd party tool

We can use a third party tool like AutoIT to handle the browser authentication window in Selenium WebDriver. Click on below link to know more about AutoIT integration in Selenium.

 How to integrate AutoIT in Selenium?

Now I assume you know AutoIT, hence, you just need to add the following code in your AutoIT IDE.

WinWaitActivate("Authentication Required","")
Send("Username{TAB}Password{ENTER}")

Technique# 3: Through WebDriver Wait for Alert

You can use the below program as well to handle the browser authentication window in your Selenium project. It is similar to Explicit wait. Here is the code:

WebDriverWait webdriverWait = new WebDriverWait(driver, 10);     
Alert alert = webdriverWait.until(ExpectedConditions.alertIsPresent());    
alert.authenticateUsing(new UserAndPassword(username, password));

Technique# 4: Through Proxy settings

You can set proxy and desired capabilities to handle browser authentication window. Here is the sample code:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
Proxy proxy = new Proxy();
proxy.setHttpProxy("proxy address");
capabilities.setCapability(CapabilityType.PROXY, proxy);
driver = new ChromeDriver(capabilities);

How to set Proxy in Selenium WebDriver?

Technique# 5: Through Profile Handling

Last but not least, you can handle the browser authentication window through profile handling also. Handling profile efficiently allows you to easily manage the authentication. Here are some of the article suggestions for you to handle profiles in the browser.

Join Inviul fb group

Leave a Reply