Post thumbnail
WEB DEVELOPMENT

Handling Alert Boxes using Robot Framework

By Suman Gangopadhyay

Table of contents


  1. Introduction
    • Types of Alert Boxes
    • How Alert Boxes Work
  2. Best Practices for Using Alert Boxes
  3. Working
    • Key Libraries
  4. Coding with Robot Framework
  5. Conclusion

Introduction

What happens when an unexpected pop-up interrupts your automated test execution? Alert boxes, though simple in appearance, can pose a challenge when automating web applications. They demand immediate user interaction, momentarily pausing all other operations. Whether it’s a success message, a confirmation prompt, or a text input request, handling these alerts correctly is crucial for seamless automation.

Robot Framework, a widely used automation tool, provides efficient ways to interact with these alert boxes. By leveraging SeleniumLibrary, testers can simulate user actions like accepting, dismissing, or inputting text into alerts without manual intervention. In this guide, we’ll explore the different types of alert boxes, their role in user interaction, and how to handle them effectively using Robot Framework. Mastering these techniques will not only streamline test execution but also enhance the reliability of automated workflows.

Types of Alert Boxes

Alert boxes can be categorized into three primary types which are as follows :-

  1. Information Alert 
    • Purpose : To display a message to the user.
    • Appearance : Typically, a simple box with a message and an “OK” button.
    • Usage:
      • Informing the user about successful operations (e.g., “File saved successfully”).
      • Notifying the user of errors or warnings (e.g., “Invalid input, please try again”).
      • Providing additional information or context (e.g., “This action will delete all data”).
  2. Confirmation Alert
    • Purpose: To seek user confirmation before performing an action.
    • Appearance: A box with a message and two buttons: “OK” and “Cancel.”
    • Usage
      • Preventing accidental data loss (e.g., “Are you sure you want to delete this file?”).
      • Confirming critical actions (e.g., “Do you want to proceed with the purchase?”).
      • Verifying user intent (e.g., “Are you sure you want to close this window?”).
  3. Prompt Alert
    • Purpose: To prompt the user to input text.
    • Appearance: A box with a message, a text input field, and two buttons: “OK” and “Cancel.”
    • Usage:
      • Collecting user input (e.g., “Enter your name”).
      • Customizing application behavior (e.g., “Set the number of items to display”).
      • Creating interactive experiences (e.g., “Enter a password to proceed”).

How Alert Boxes Work

When a specific event triggers an alert box, the browser pauses the execution of JavaScript code and displays the alert. The user must interact with the alert by clicking a button or entering text before the script can continue.

Best Practices for Using Alert Boxes

  • Use sparingly : Overuse of alert boxes can disrupt the user experience. Use them only when necessary.
  • Clear and concise messaging : The alert message should be clear, concise, and easy to understand.
  • Appropriate button labels : Use clear and actionable button labels like “OK,” “Cancel,” “Yes,” and “No.”
  • Consistent design : Maintain a consistent design for alert boxes throughout your application.
  • Accessibility : Ensure that alert boxes are accessible to users with disabilities by providing appropriate keyboard navigation and screen reader compatibility.
  • Mobile-friendliness : Design alert boxes to be responsive and user-friendly on mobile devices.
  • Testing : Thoroughly test alert boxes to ensure they function correctly in different browsers and devices.
  • Clear Test Case Design : Structure your test cases to accurately simulate user interactions with alert boxes.
  • Robust Keyword Library : Create custom keywords to encapsulate common alert handling patterns, improving code reusability and maintainability.
  • Effective Error Handling : Implement robust error handling to identify and address issues promptly.
  • Regular Maintenance : Keep your test scripts up-to-date with changes in the application’s behavior and alert box implementation.

Working 

Alert boxes are a common UI element in web applications used to display important messages or to prompt user input. They can be categorized into three main types:

  1. Information Alert : Displays a message without requiring user interaction.
  2. Confirmation Alert : Asks the user to confirm an action with a “Yes” or “No” button.
  3. Prompt Alert : Prompts the user to input text.

Robot Framework, a powerful and versatile open-source automation framework, provides robust libraries to interact with web elements, including alert boxes. By leveraging these libraries, you can efficiently automate the handling of alert boxes in your web application testing.

MDN

Key Libraries

To work with alert boxes in Robot Framework, we primarily use the following library:-

  • SeleniumLibrary : This is the core library for the Robot web automation testing framework. It provides keywords to handle various web elements, including alert boxes.

Coding with Robot Framework

Here is the comprehensive Robot Framework code to work with different types of Alert Boxes given below. Below the code you will find the description of the code in detail.

*** Settings ***

Documentation   Working with different alert boxes

Library     SeleniumLibrary

*** Variables ***

${URL}      https://qavbox.github.io/demo/alerts/

${alert_text}       Have a nice day!!!

${PROMPT_DATA}      Suman Gangopadhyay

${CONFIRM_ALERT_BOX_OK_TEXT}   You pressed OK!

${CONFIRM_ALERT_BOX_DISMISS_TEXT}    You pressed Cancel!

*** Test Cases ***

TestCase-1

   [Documentation]     open automation

   [Tags]      suman-1

   Open Browser    ${URL}      chrome

   Maximize Browser Window

   Set Browser Implicit Wait    10s

TestCase-working with simple alert box

   [Documentation]     working with simple alert box

   [Tags]      suman-2

   Click Button    xpath=//input[@value=’Submit’]

   Alert Should Be Present    ${alert_text}    ACCEPT

TestCase-working with Prompt alert

   [Documentation]     working with Prompt alert

   [Tags]      suman-3

   Click Button    xpath=//input[@value=’PromptMe’]

   Input Text Into Alert    ${PROMPT_DATA}     action=ACCEPT

   Page Should Contain    Hello Suman Gangopadhyay! How are you today?

   Sleep    3s

TestCase-working with Confirm Dialog Box

   [Documentation]     working with Confirm Dialog Box

   [Tags]      suman-4

   Click Button    xpath=//input[@value=’Confirm’]

   Handle Alert    ACCEPT

#    Handle Alert    DISMISS

   Page Should Contain    ${CONFIRM_ALERT_BOX_OK_TEXT}    You pressed OK!

   Sleep    3s
  

TestCase-5

   [Documentation]     shutdown automation

   [Tags]      suman-5

   Close All Browsers

This Robot Framework code which is given above describes a set of test cases for interacting with different types of alert boxes on a web page. Let’s break down the sections:

Settings:

  • Documentation: This section provides a brief description of the overall test suite, in this case, it explains that the tests will work with different alert boxes.
  • Library: This line imports the SeleniumLibrary which allows the tests to interact with web elements using the Selenium WebDriver.

Variables:

  • These variables store reusable data used throughout the test cases.
    • URL: Holds the URL of the web page containing the alert boxes (https://qavbox.github.io/demo/alerts/)
    • alert_text: The expected text displayed in the simple alert box (“Have a nice day!!!”)
    • PROMPT_DATA: The text to be entered into the prompt alert box (“Suman Gangopadhyay”)
    • CONFIRM_ALERT_BOX_OK_TEXT: The expected text displayed after clicking OK on the confirm dialog (“You pressed OK!”)
    • CONFIRM_ALERT_BOX_DISMISS_TEXT: The expected text displayed after dismissing the confirm dialog (“You pressed Cancel!”)

Test Cases:

  • There are five test cases in total, each documented for clarity.
  • Each test case also has tags associated with them (e.g., suman-1, suman-2). These tags can be used for filtering and organizing test runs.

1. TestCase-1:

  • This case opens the web page using Chrome browser and sets an implicit wait of 10 seconds (waits for elements to be available before failing).

2. TestCase-working with simple alert box:

  • Clicks the button with the value “Submit” which triggers a simple alert box.
  • Verifies that an alert with the expected text (alert_text) is present and accepts it (clicks OK).

3. TestCase-working with Prompt alert:

  • Clicks the button with the value “PromptMe” which triggers a prompt alert box.
  • Inputs the text stored in PROMPT_DATA into the alert box and accepts it (clicks OK).
  • Verifies that the page content contains the text “Hello Suman Gangopadhyay! How are you today?”
  • Waits for 3 seconds (optional pause).

4. TestCase-working with Confirm Dialog Box:

  • Clicks the button with the value “Confirm” which triggers a confirm dialog box.
  • Note: The commented line (Handle Alert DISMISS) allows you to test dismissing the dialog instead of accepting.
  • Verifies that the page content contains CONFIRM_ALERT_BOX_OK_TEXT after clicking OK on the dialog.
  • Waits for 3 seconds (optional pause).

5. TestCase-5:

  • Closes all open browser windows, effectively shutting down the automation session.

Overall

This code which has been demonstrated and described above tells us how to utilize Robot Framework with SeleniumLibrary to interact with different types of alert boxes on a web page. It includes test cases for simple alert boxes, prompting user input, and confirming actions with dialog boxes.

MDN

Conclusion

Handling alert boxes efficiently is a fundamental skill in web automation testing. Ignoring or improperly managing these pop-ups can lead to test failures, false negatives, and incomplete test coverage. By leveraging Robot Framework’s SeleniumLibrary, testers can seamlessly navigate different alert types—whether it’s verifying messages, confirming actions, or capturing user input.

Beyond basic automation, it’s essential to incorporate structured test cases, robust error handling, and regular maintenance to ensure long-term reliability. As web applications evolve, so should testing strategies. The more refined and adaptable your automation framework is, the more resilient your test scripts will be against UI changes and unexpected behaviors. Keep optimizing, refining, and adapting—because in automation, precision is the key to success.

Career transition

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Share logo Copy link
Power Packed Webinars
Free Webinar Icon
Power Packed Webinars
Subscribe now for FREE! 🔔
close
Webinar ad
Table of contents Table of contents
Table of contents Articles
Close button

  1. Introduction
    • Types of Alert Boxes
    • How Alert Boxes Work
  2. Best Practices for Using Alert Boxes
  3. Working
    • Key Libraries
  4. Coding with Robot Framework
  5. Conclusion