How To Handle Login Pop-up In Selenium WebDriver Using Java?

Posted by Shalini Baskaran | March 23, 2021
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.

Source

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.

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:

https://username:[email protected]

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.

  1. Download AutoIT from the official website of AutoIT or using the AutoIT download link.
  2. Navigate to the installed folder and open the AutoIT editor.
  3. In the editor, enter the below code to send the username and password for handling the authentication pop-up in the website.
  4. Save the file with the extension .au3.
  5. 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.
  6. 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.

TestNG.xml

To run this test in the LambdaTest platform, please follow the below-mentioned steps:

  1. Right-click on the TestNG.xml that you have created in the IDE.
  2. Click Run As–> TestNG Suite. Now the test will be executed on the LambdaTest platform.
  3. Once your tests are executed, navigate to your LambdaTest account in the browser and click Dashboard in the left-hand side panel.
  4. This page shows an overview of the tests that are run on the platform.

  5. Next, click the Automation label in the left-hand side panel.
  6. 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.

  7. Click the Build View to view the tests that were run.
  8. 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.

Test Execution Process

Wrap Up

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?

  1. First you need to check for the active window in your application (Popup or main window).
  2. You can use driver.close() if the popup is active, else
  3. You need to use a window handler to close this popup (If the main window is active).

Written by

Related Articles

Use TestNG Reporter Log In Selenium

How To Use TestNG Reporter Log In Selenium

121411 Views | 12 Min Min Read

data driven framework in selenium

How To Create Data Driven Framework In Selenium

98155 Views | 29 Min Min Read

Leave a Reply

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