How To Handle Authentication Popup Window Using Selenium WebDriver

Handling Authentication Popup Window:

In this post, we see how to handle Authentication Popup using Selenium WebDriver.

What is Authentication Popup window and why these Authentication Popup windows showed up while opening few websites.

Authentication popup window looks like the below mentioned image.

Let’s see “How To Handle Authentication Popup using Selenium WebDriver”:

Companies have their own proxy settings to access some servers. If you open a server in the browser a popup window will load and force you to enter user credentials to do login to access the server. Even while accessing the same server using selenium script we need to handle the popup before hand accessing the server. If we can’t handle this, we couldn’t do further testing.

We could handle this type of authentication pop up in multiple ways. Let’s see in detail.

Approach 1: Handling Authentication/Login Popup Window using Selenium WebDriver

By passing user credentials in URL. Its simple, append your username and password with the URL.

e.g., http://Username: [email protected]

http://rajkumar: [email protected]

here, Username is rajkumar
Password is myPassword
SiteURL is www.softwaretestingmaterial.com

Sample code:

1
2
3
4
String URL = “http://” + rajkumar + “:” + myPassword + “@” + www.softwaretestingmaterial.com;
driver.get(URL);
Alert alert = driver.switchTo().alert();
alert.accept();

Approach 2: Handling Authentication/Login Popup Window using Selenium WebDriver

By using AutoIt, we could handle authentication pop up.

Also Read:

How to download file Using AutoIt

How to upload file using AutoIt

Sample AutoIT Script:

1
2
WinWaitActivate(“Authentication Required”,“”)
Send(“rajkumar{TAB}myPassword{ENTER}”)

Sample Java Code:

1
2
3
4
5
6
7
8
9
public static void login(String email, String password) throws Exception{
driver.get(URL);
//Passing the AutoIt Script here
Runtime.getRuntime().exec(“D:\\Selenium\\workspace\\AutoItFiles\\ExecutableFiles\\FirefoxBrowser.exe”);
driver.findElement
loginpage.setEmail(email);
loginpage.setPassword(password);
loginpage.clickOnLogin();
}

Approach 3: Handling Authentication/Login Popup Window using Selenium WebDriver

By using Alerts in Selenium, we could handle authentication pop up.

1
2
3
4
5
6
driver.switchTo().alert();
//Selenium-WebDriver Java Code for entering Username & Password as below:
driver.findElement(By.id(“userID”)).sendKeys(“userName”);
driver.findElement(By.id(“password”)).sendKeys(“myPassword”);
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();