How to Handle Alerts/Pop-ups in Java Selenium Headless Mode

PrimeQA Solutions
4 min readMay 23, 2024

--

Handling alerts/pop-up in web applications is crucial for ensuring a smooth and uninterrupted user experience.

When using Selenium WebDriver with Java for automated testing, it’s essential to manage these alerts effectively. This includes accepting or dismissing alerts, retrieving alert text, and sending input to prompt alerts.

Proper alert handling prevents test scripts from getting stuck or failing unexpectedly, thereby ensuring the robustness and reliability of the test automation suite.

Key Points:

  1. Headless Mode Configuration: Explanation of headless mode and how to configure it using ChromeOptions in Selenium.
  2. Types of Alerts: Overview of different alert types (Simple, Prompt, Confirmation) and their significance.
  3. Advantages of Testing Alerts: Key benefits such as preventing workflow disruptions, enhancing user security, improving usability, and reducing errors.
  4. Practical Example: Sample Java code demonstrating how to handle alerts using Selenium, including steps to accept or dismiss alerts effectively.
  5. Alert Handling in Parallel Testing: Handling alerts in parallel testing scenarios requires careful consideration to ensure that alerts are managed effectively without causing conflicts or interruptions in test execution.

What is Headless mode and how to configure:

Headless mode means running the test cases without a visible UI, so we only see the console logs in the editor like VsCode.

Let’s Configure Headless Mode(just example):

ChromeOptions options = new ChromeOptions();
options.addArguments(“ — headless”);

Type of Alerts:

  1. Simple Alert: A simple alert displays a message to the user and only has an “OK” button to acknowledge the alert.
  2. Prompt Alert: A prompt alert asks the user for input and provides a text box along with “OK” and “Cancel” buttons.
  3. Confirmation Alert: A confirmation alert requests the user to confirm an action and offers “OK” and “Cancel” buttons.

Problem: Not able to handle alert/popups in the headless mode

We are trying handle alert/popups in the headless mode, but script wont able to handle it. Scenario was Click on the “Delete” button -> Alert popup shows up (in this cases Confirmation Alert) -> Accept the alert -> Item gets deleted. But somehow developers has written the code in JS and it was not able to handle it if we directly use the JavaScript code to handle the alert popup xpath.

Solution:

Here is a sample code in Java using JavaScript with Selenium. This code Successfully executed with the TestNG Framework with Maven.

Here we have tried to click on “Delete” button itself with the help of JS, which leads to successfully handled the alert/popup post that. It not necessary to use JS on the alert/pop-up element itself always. Try to use JS on the element from which alert/pop-up triggers, which may solve your problem.

The code first uses a JavaScript to find and click a “Delete” button on the webpage in our case. It then waits for a confirmation alert to appear, switches the Selenium WebDriver context to the alert, and accepts it by calling alert.accept(). This process ensures the automation script can handle and respond to the alert correctly.

Alert Handling in Parallel Testing:

Certainly! When conducting parallel testing, multiple test cases are executed simultaneously across different environments or browsers to accelerate the testing process. Handling alerts in parallel testing scenarios requires careful consideration to ensure that alerts are managed effectively without causing conflicts or interruptions in test execution. Here are some strategies for handling alerts in parallel testing:

Strategies:

  1. One Test Doesn’t Interrupt Another: When running multiple tests at the same time, we make sure that one test’s alerts don’t mess with another test’s results.
  2. Wait for the Right Moment: We wait for an alert to pop up before we handle it, ensuring that we don’t miss it or get ahead of ourselves.
  3. Each Test Minds Its Own Business: Each test is like its own little world, dealing with its alerts separately from the others, so they don’t get in each other’s way.
  4. We Keep Track of What Happens: We keep a record of any alerts that show up during testing, making it easier to spot any issues and understand what’s going on.
  5. Works Everywhere: We make sure our alert-handling tricks work smoothly no matter which web browser or setup we’re testing in, so you get reliable results no matter what.

Same way we can handle Alerts/Pop-ups in Python Selenium in Headless Mode.

--

--

PrimeQA Solutions
PrimeQA Solutions

Written by PrimeQA Solutions

PrimeQA is one of the global leaders in Quality Assurance and Testing. Reliability, efficiency, and expertise are the core principles of our QA services.

No responses yet