Reading Time: 3 minutes
Welcome again! Ever think of handling Log-in Pop-up via Selenium? Let’s take a quick overview of the scenario.
What’s the need for automating log-in Pop-up?
- For security reasons, some of the web pages did not have access to normal users.
- As only authenticated users with correct credentials can access web pages.
- We can automate this by using selenium.
What the pop-up mean and where it arises?
- Simple pop-up notifications are those that show details to the user.
- Information can be of any kind, sometimes requiring certain operating permissions, sometimes requesting a username and password.
- The Alers Box displayed below gives some information or message to the user ” Hi… this is the alert message” and asking the user to press OK for action.
- This article mainly focuses on Authentication pop up on the website and different way to handle it using Selenium Web driver
- Let us learn how to handle authentication pop-up in Selenium WebDriver using Java.
Different ways to handle login pop-up in Selenium?
Below are the ways to handle login pop-up using Selenium in java .
- Pass the credentials in the URL of the web page.
- Using AutoIT tool.
- General Information: AutoIt scripts run only in Windows Environment. To run in any other Environment Use the WINE tool.
Handling login pop-up in Selenium by passing the credential in URL
- The Basic authentication pop-up is like the alert pop-up .
- When we navigated to a specific web page that asks for the credential.
- To handle this, we can pass credentials (Username + Password) to the web page’s URL.
- Syntax and Some Basic information is prerequisite.
username : "dummyusername".
password : "dummypassword".
URL : "XYZ.com".
Syntax for handling pop-up is:
https://username:password@URL
- Whenever we navigate to specific web pages where authentication is required. Login pop-up is prompted.
- In this example, we had taken the username and password as stated above.
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.io.IOException;
import java.net.URLEncoder;
import static java.net.URLEncoder.*;
public class BrowserTest {
public static void main(String[] args)throws IOException {
//To set system property System.setProperty("webdriver.gecko.driver","drivers/gechodriver/geckodriver.exe");
//Open the driver
WebDriver driver = new FirefoxDriver();
String MyURL="https://"+"dummyURL.com";
String URL = "https://" +dummyusername +":" +dummypassword +"@"+ "dummyurl.com";
driver.get(URL);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
String currenturl = driver.getCurrentUrl();
if(currenturl==Myurl){
System.out.println("Hurray You are successfully done login");
}
else
System.out.println("login Unsuccessfull");
System.out.println("The page URL is "+currenturl);
driver.close();
driver.quit();
}
}
- As soon as we enter correctly the credential.You will be logged in successfully.
Handling login pop-up in Selenium web driver using AutoIt tool
- Before starting with the AutoIt tool, Firstly we have to complete the installation part
- AutoIT download -> click here to download. Once downloaded, Install AutoItV3 Setup in System.
What is AutoIt?
- AutoIt is a tool used to do anything in the Windows environment.
- The AutoIt script is written in the BASIC language.
- It can promote any key-click combination, mouse movement, and window/control functionality.
Now let us handle the login pop
WinWaitActive("Mozilla Firefox","","10")
if WinExists("Mozilla Firefox") Then
Send("dummyusername{TAB}")
Send("dummypassword{Enter}")
EndIf
- Navigate to the folder and open AutoIt editor.
- In the editor, enter the above code with user authentication.
- Save the file by name seleniumbug.au3.
- Now convert the script into the executable file. By right-clicking and compiling. You will see the executable file created.
- In IDE set the driver, open the URL, and include the path of executable as below.
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.io.IOException;
import java.net.URLEncoder;
import static java.net.URLEncoder.*;
public class BrowserTest {
public static void main(String[] args)throws IOException {
System.setProperty("webdriver.gecko.driver","drivers/gechodriver/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String MyURL="https://dummyURL.com";
driver.get(MyURL);
Runtime.getRuntime().exec("AutoIT/seleniumbug.exe");
System.out.println("Hurray you are logged in");
String title=driver.getTitle();
System.out.println("Title of page is"+title);
driver.close();
}
}
- By running AutoIt.java it will successfully log you .
Conclusion.
- I hope this blog is useful to all And shows how to automate and deal with a log-in pop-up.Thanks for reading Blog.
Happy Learning!!!