How to handle authentication required in selenium webdriver

How to handle authentication required in selenium webdriver

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.

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.

ALSO READ  How To write Test Case is PASS or FAIL using Selenium

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.");
}
}

 

ALSO READ  How to get the number of Elements in a list in Python

 

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.

Must Read:

Verify element is enable in Selenium
Read Data From properties file using Selenium
Launch Firefox Browser using GeckoDriver
Selenium-Testng examples to execute multiple classes
Selenium WebDriver Methods
Generate HTML Reports using Selenium

Rajesh K

Leave a Reply

Your email address will not be published. Required fields are marked *