How to handle authentication required in selenium webdriver

Selenium web driver provides options to handle authentication required,here we are using Google chrome browser for authentication.While working with separate Browsers i.e IE,Fire Fox and Chrome we will face Authentication problems in order to open web applications which is under test whether it may be QA or UAT . In order to overcome this problem ,please follow the below easy code.

ALSO READ

TestNG Classpath error
Session ID is null using webdriver after calling quit
How to send extent reports in email with screenshots
Write Test Cases PASS or FAIL using Selenium
Read data from properties file in selenium
Run Selenium scripts using Command Line
Run Selenium Scripts using Runnable JAR
TOP Selenium Interview Questions asked in MNC
How to Send attachments in Jenkins emails
Selenium XPATh tutorial step by step


How to handle authentication required in selenium webdriver:

1.Robot Object
2.KeyEvents :Useful for Keyboard operations
3.Username and Password in order to authenticate the Pop Up window.

Authentication Script:


package com.gmail.account;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

//Create Class
public class Selwebddriver {
//Declare Webdriver ,baseUrl
WebDriver driver;
String baseUrl;

@BeforeClass
public void setup(){

//Initialize the Chromedriver with exe file
File ChromeDriver = new File("F:\\Java_Applications\\Zip Files\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver",ChromeDriver.getAbsolutePath());

//TO maximize the chrome window
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
driver = new ChromeDriver(options);

//Declare the url
baseUrl="http://www.gmail.com";

}

@Test
public void Confirmation() throws AWTException {

driver.get(baseUrl);

//Create Robot object
Robot rb = new Robot();
//Save the Username in user string
StringSelection user = new StringSelection("admin");
//User which replace with entered user name
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(user, null); //Copy Username data using Ctrl+v as below rb.keyPress(KeyEvent.VK_CONTROL); rb.keyPress(KeyEvent.VK_V); //Now release the KEYs rb.keyRelease(KeyEvent.VK_V); rb.keyRelease(KeyEvent.VK_CONTROL); //Press TAB to enter password rb.keyPress(KeyEvent.VK_TAB); //Now Release the TAB key rb.keyRelease(KeyEvent.VK_TAB); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Same above code for Password StringSelection passwd = new StringSelection("admin"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(passwd, null); //passwd which replace with entered password //Copy password data using Ctrl+v as below rb.keyPress(KeyEvent.VK_CONTROL); rb.keyPress(KeyEvent.VK_V); //Now release the KEYs Ctrl+v as below rb.keyRelease(KeyEvent.VK_V); rb.keyRelease(KeyEvent.VK_CONTROL); //Press Enter using VK_ENTER key rb.keyPress(KeyEvent.VK_ENTER); //Now Release the key ENTER rb.keyRelease(KeyEvent.VK_ENTER); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); System.out.println("Authentication is successfull....WebSite is opened."); } }

Test Results:

Authentication required using selenium webdriver


Please provide your valuable suggestions,feedback or comments in case you like this post "How to handle authentication required in selenium webdriver".Thank you for reading.

Post a Comment

0 Comments