How to handle alerts in Robot Framework 

HOME

In this tutorial, we will discuss various types of Alerts available in web application testing and how to handle alerts in Robot Framework.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

How to create a test to automate the alerts in Robot Framework?

1. Simple Alert

Step 4 – Create a new directory in the new project

Right-Click on the project, select New->Directory and provide name as Tests

Below is the image of the new directory.

Right-click on the new directory and select New File and provide the name as Confirm_Alert_Demo.robot, Ok_Alert_Demo.robot and TextBox_Alert_Demo.robot as shown below:

Step 5 – Download ChromeBinaries from the below location

The tests are going to use the Chrome browser, so we need to download the ChromeBinaries to open a blank browser in Chrome.

https://chromedriver.chromium.org/

The chromedriver is placed in a folder name drivers in the RobotFramework_Demo project. I have renamed chromedriver to Chrome.

Step 6 – Automate the Simple Alert

We are now going to write test cases. The test case details will be as follows :

To work with the Robot Framework, we need a locator. A locator is an identifier for the textbox like id, name, class, xpath, css selector, etc.

To know more about locators, refer to these Selenium Tutorials:

 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText

Dynamic XPath  in Selenium WebDriver

CSS Selector in Selenium WebDriver

The below page shows that when we click on “Alert with OK”, then click on the “click the button to display an alert box” button, a new alert opens with the message – I am an alert box!.

Below is an example of selecting the “Simple Alert”.

*** Settings ***
Documentation    To validate the Message Alert
Library     SeleniumLibrary
Test Teardown    Close Browser

*** Variables ***
${okOption}    xpath://div[@class='tabpane pullleft']/ul/li[1]/a
${alertBtn}    css:.btn-danger

*** Test Cases ***
Handle Message Alert
    Open the Browser with URL
    Select the Alert with OK Option
    Verify text on Alert Box

*** Keywords ***
Open the Browser with URL
    Create Webdriver    Chrome  executable_path=/Vibha_Personal/RobotFramework/drivers/chromedriver_linux64
    Go To    https://demo.automationtesting.in/Alerts.html
    Maximize Browser Window
    Set Selenium Implicit Wait    2

Select the Alert with OK Option
    Click Element    ${okOption}
    Click Button     ${alertBtn}

Verify text on Alert Box
    Alert Should Be Present     I am an alert box!      ACCEPT

All the below-mentioned keywords are derived from SeleniumLibrary. The functionality of keywords mentioned above:

1. Create Webdriver − The keyword creates an instance of Selenium WebDriver.

2. Go To – This keyword navigates the current browser window to the provided URL – https://demo.automationtesting.in/Register.html.

3. Maximize Browser Window – This keyword maximizes the current browser window.

4. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

5. Click Element − This keyword is used to click the element identified by the locator. In this case, it is the “Alert with OK” link.

6. Click Button – This keyword is used to click the button identified by the locator. In this case, it is the “Click the button to display an alert box” button.

7. Alert Should Be Present – This keyword is used to verify that an alert is present on the page and, by default, accepts it.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.htm.

To run this script, go to the command line and go to directory tests.

Step 7 – Execute the tests

We need the below command to run the Robot Framework script.

 robot Ok_Alert_Demo.robot

The output of the above program is

Step 8 – View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

2. Confirmation Alert

This alert comes with an option to accept or dismiss the alert.

The below page shows that when we click on Alert with OK & Cancel”, then click on the “click the button to display a confirm box” button, a new alert opens with the message – Press a Button!.

We are now going to write test cases. The test case details will be as follows :

Below is an example of selecting the “Confirmation Alert”.

*** Settings ***
Documentation    To validate the Confirm Alert
Library     SeleniumLibrary
Test Teardown    Close Browser
Documentation    To validate the Confirmation Alert
Library     SeleniumLibrary
Test Teardown    Close Browser


*** Variables ***
${okCancelOption}    xpath://div[@class='tabpane pullleft']/ul/li[2]/a
${confirmBtn}    css:.btn-primary
${text}     id:demo

*** Test Cases ***
Handle Confirm Alert
    Open the Browser with URL
    Select the Alert with OK & Option
    Verify text on Confirm Box on selecting Cancel option

*** Keywords ***
Open the Browser with URL
    Create Webdriver    Chrome  executable_path=/Vibha_Personal/RobotFramework/drivers/chromedriver_linux64
    Go To    https://demo.automationtesting.in/Alerts.html
    Maximize Browser Window
    Set Selenium Implicit Wait    2

Select the Alert with OK & Option
    Click Element    ${okCancelOption}
    Click Button     ${confirmBtn}

Verify text on Confirm Box on selecting Cancel option
    Alert Should Be Present     Press a Button !     DISMISS
    Element Text Should Be    ${text}      You Pressed Cancel

3. Prompt Alert

 In the prompt alert, we get an option to add text to the alert box. This is used when some input is required from the user.

We are now going to write test cases. The test case details will be as follows :

Below is an example of selecting the “Prompt Alert”.

*** Settings ***
Documentation    To validate the TextBox Alert
Library     SeleniumLibrary
Test Teardown    Close Browser

*** Variables ***
${textboxOption}    xpath://div[@class='tabpane pullleft']/ul/li[3]/a
${displayBtn}    css:.btn-info
${text}     id:demo1

*** Test Cases ***
Handle TextBox Alert
    Open the Browser with URL
    Select the Alert with TextBox Option
    Verify text on entered on textbox is displayed as message

*** Keywords ***
Open the Browser with URL
    Create Webdriver    Chrome  executable_path=/Vibha_Personal/RobotFramework/drivers/chromedriver_linux64
    Go To    https://demo.automationtesting.in/Alerts.html
    Maximize Browser Window
    Set Selenium Implicit Wait    2

Select the Alert with TextBox Option
    Click Element    ${textboxOption}
    Click Button     ${displayBtn}

Verify text on entered on textbox is displayed as message
    Input Text Into Alert     Hello     ACCEPT
    Element Text Should Be    ${text}      Hello Hello How are you today

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s