i

Selenium Step By Step Guide

Handling User Authentication and Input alerts

Sometimes we face a situation where organizations have their proxy settings to access the servers. If we open a server URL in a browser, it asks to provide the user credentials to continue the further operations. Users can not continue until it provides the credentials to the server.

We have different types of solutions available in selenium to handle this situation. Let us talk about the most used way to handle such a situation.

Passing the username and password in server URL:

This is the primary and most used technique to handle this situation where we pass the user credentials through the URL of the web page.

Syntax: http://username:password@domainName.com

Now we can pass this URL to the driver object to load the web page. Now the web page does not show the authentication pop-up.

Let us check the complete sample code to handle this situation.

Package SeleniumTest.SeleniumTest;

import org.openqa.selenium.Alert;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

public class WebDriverTest

{

    public static void main( String[] args )

    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.get("http://username:password@domainName.com");

    }

}

 

Input Alerts:

We have discussed this topic earlier as well in “Handling Java script Alerts & Keyboard Events." Here user needs to pass some information required by an alert pop-up. We handled this situation by using the Alert interface. We need to first switch to the alert and then enter the user inputs using the sendKeys() method. Finally, we need to accept the alert to close the alert pop-up.

Alert alert=driver.switchTo().alert();

alert.sendKeys(“Happy Testing”).accept()