How To Handle Login Pop-up In Selenium WebDriver Using Java?
Selenium Java • Selenium Webdriver •
44641 Views | 7 Min Read
Have you ever been asked for credentials while accessing a website on a browser? Let us understand why we are asked to fill up the credentials while accessing a few websites. This article mainly focuses on the introduction to authentication pop-ups on the website and the different ways to handle them using Selenium. Before we dive in deep and see how to handle login pop-up in Selenium WebDriver, let us first take a look at how the authentication pop-up looks on a website.
Let us navigate to a sample website. While accessing the website, the user would be prompted with a login pop-up. The user has to enter the username and password to further navigate into the site. Below is the screenshot of the authentication pop-up prompted on the website.
Why does this happen?
Some of the websites that have access to the servers require credentials to navigate further. Hence, while accessing these websites, the user would be prompted with an authentication pop-up to enter his credentials.
In this Selenium WebDriver Java tutorial, we will see different ways to handle login pop-up in Selenium WebDriver using Java.
TABLE OF CONTENT
- How to handle login pop-up In Selenium WebDriver?
- Running the login pop-up test on LambdaTest
How to handle login pop-up In Selenium WebDriver?
To handle login pop-ups in Selenium WebDriver using Java, we can use either of the below ways.
- Pass the credentials in the URL of the web page.
- Using AutoIT.
- Using Selenium 4 Beta version.
Let us now understand each method in detail.
Handling login pop-up in Selenium WebDriver by passing the username and password in the URL
The basic authentication pop-up is similar to the alert that pop-ups when the browser is navigated to a specific web page. To handle the basic authentication popup, we can pass the username and password along with the web page’s URL.
The syntax for handling this login pop up is:
When the login pop-up is prompted, we enter the username as “admin” and the password as “admin” and then login. Thus, the user would be successfully logged into the website.
Below is the code to handle the basic login pop-up on the web page by passing the username and the password in the URL of the web page.
Once the credentials are entered correctly, the user will be authenticated successfully, and a proper message will be displayed on the website.
This certification demonstrates your knowledge of Selenium and Java, and your expertise at automating tests for any project.
Here’s a short glimpse of the Selenium Java 101 certification from LambdaTest:
Handling login pop-up in Selenium WebDriver using AutoIT
AutoIT is a scripting language, which is popularly used for automating GUI applications and can automate tasks involving keyboard and mouse interactions. It is popularly used for automating the tasks involving file upload, file download, handling authentication pop-ups, etc. Now, let us install AutoIT and use it to handle the login pop-up on the web page.
- Download AutoIT from the official website of AutoIT or using the AutoIT download link.
- Navigate to the installed folder and open the AutoIT editor.
- In the editor, enter the below code to send the username and password for handling the authentication pop-up in the website.
- Save the file with the extension .au3.
- Now, to convert the script to an executable file, right-click on the saved file and click Compile Script. You will see an executable file has been created.
- In the IDE, set the driver file’s path, enter the URL of the web page, and enter the AutoIT executable file path as below.
So far, we have seen different ways of handling the authentication pop-ups in Selenium. To get into an advanced level, let us try the same using the new features of the latest Selenium 4 beta version.
Handling login pop-up in Selenium WebDriver using Selenium 4 beta version
The latest version of Selenium has introduced the ChromiumDriver class, which has two methods, namely getDevTools() and executeCdpCommand() to access the chrome DevTools.
The getDevTools() method returns the new DevTools object, which allows the user to send the built-in Selenium commands. In previous versions of Selenium, we have used the technique of passing the credentials in the URL of the web page to handle the basic login pop-up in the website. Now in the Selenium 4 beta version, we can easily set the basic authentication by sending extra HTTP headers.
Below is the code snippet of handling the login pop up.
Running the login pop-up test on LambdaTest
LambdaTest provides an online cloud platform to perform Selenium automation testing with ease on cloud Selenium Grid. LambdaTest offers the largest web and mobile cross browser testing platform, giving you the ability to test on 2000+ different browser/OS combinations.
Once you are logged into the website, you will be provided with a username and an access key that can be used to run your scripts in the cloud. You can get the username and access key from the Profile Section.
Below is the code snippet for executing the login pop-up test in LambdaTest.
To run this test in the LambdaTest platform, please follow the below-mentioned steps:
- Right-click on the TestNG.xml that you have created in the IDE.
- Click Run As–> TestNG Suite. Now the test will be executed on the LambdaTest platform.
- Once your tests are executed, navigate to your LambdaTest account in the browser and click Dashboard in the left-hand side panel.
- Next, click the Automation label in the left-hand side panel.
- Click the Build View to view the tests that were run.
This page shows an overview of the tests that are run on the platform.
This is the place where you have to identify your executed tests. It contains multiple tests, and to identify the specific test, you have to search using the build name that you have provided in the script.
In the code above, we have named the build as “AuthPopUp” and now search for that build. This page also shows the status of your build and the time taken for execution.
You can see the test “AuthPopUpTest,” which was provided in the script, and this is how we identify our tests in LambdaTest. Here you can verify all the test details that have been provided in the script, like the browser, browser version, operating system, and version in which the test was executed. This would be the same as provided in the test script.
As we have enabled video recording in the code, you will see a video attached to the test on this page, which has recorded all the test steps while executing the test.
By playing the video, you can view how the tests were executed. In case of failure, this would help to analyze the point where the failure has occurred.
To summarize, we have seen different ways of handling the login pop-up on the web page using Selenium WebDriver in Java. I hope you all have gained good knowledge through this article. Please share this article with your friends and peers, and I would love to hear your comments on this. Happy Testing…! 😊
Frequently Asked Questions
How do I stop pop ups in selenium?
- First you need to check for the active window in your application (Popup or main window).
- You can use driver.close() if the popup is active, else
- You need to use a window handler to close this popup (If the main window is active).
51100 Views | 16 Min Min Read
59592 Views | 22 Min Min Read
121411 Views | 12 Min Min Read
98155 Views | 29 Min Min Read