In this session you will learn very important topic on Automation - Handling Authentication popup
To learn selenium step by step yourself with real time examples , please check below playlist
• How to handle New Window or multiple windo...
To Download the project or programs for practice , please dowload from below GitHub Repository
https://github.com/knowledgeshare-tec...
Overview on Authentication Popup
===============================
What is Authentication popup
A pop that appears when a webpage is launched with username and password textboxes for Authentication
4 Ways to Handle Authentication popup
Using credentials in URL - http://Username:Password@SiteURL
Using Alert - driver.switchTo. Alert() and sending credentials
Using Robot Class – Robot robot=new Robot() and sending credentials
Using Auto IT- Installing Auto IT and sending credentials
Program for your practice
============================
{ Using Simple Method, Passing Credentials }
--------------------------------------------------------------------------
package com.seleniumbasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Authentication_popup_usingCredentials_in_URL {
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
String url="http://" + "admin" + ":" +"admin" + "@" + "the-internet.herokuapp.com/basic_auth";
driver.get(url);
driver.manage().window().maximize();
String displayed_message=driver.findElement(By.xpath("//p")).getAttribute("innerHTML");
if(displayed_message.contains("Congratulations"))
{
System.out.println("Authentication Successfull");
}
else
{
System.out.println("Authentication NOT Successfull");
}
}
}
==================================
{ Handling Using Robot Class }
--------------------------------------------------------
package com.seleniumbasics;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
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.interactions.Actions;
public class Authentication_popup_Example_usingRobotClass
{
public static void main(String args[]) throws AWTException, InterruptedException
{
System.out.println("Handling Authentication - Using ");
System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
String URL = "http://the-internet.herokuapp.com/bas...";
driver.get(URL);
//Type UserName
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_D);
robot.keyRelease(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_M);
robot.keyPress(KeyEvent.VK_I);
robot.keyRelease(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
//Press Tab
robot.keyPress(KeyEvent.VK_TAB);
//Type Password
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_D);
robot.keyRelease(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_M);
robot.keyPress(KeyEvent.VK_I);
robot.keyRelease(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(5000);
String expected_text=driver.findElement(By.xpath("//p")).getAttribute("innerHTML");
if(expected_text.contains("Congratulations! You must have the proper credentials."))
{
System.out.println("Authentication successfull and expected text is present");
}
else
{
System.out.println("Authentication not successfull and expected text is present");
}
}
}
{Handling using Robot Class}
----------------------------------------
package com.seleniumbasics;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Authentication_popup_Example_with_AutoIT
{
public static void main(String args[]) throws IOException
{
System.out.println("Handling Authentication - Using ");
/*
System.setProperty("webdriver.chrome.driver",
".\\drivers\\chromedriver.exe"); WebDriver driver=new ChromeDriver();
*/
System.setProperty("webdriver.gecko.driver", ".\\drivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
String URL = "http://the-internet.herokuapp.com/bas...";
driver.get(URL); Runtime.getRuntime().exec("E:\\Scripts\\authentication_popup.exe");
}
}