In Testing, it is essential to understand how to interact with the browser and locate the DOM elements. It is very easy to handle Alerts with the Robot framework. In this blog, we will learn about how to handle Alert using the Selenium library.
There is a set of commands provided by the Selenium library for handling alerts, such as accept, dismiss, and leave.
In this blog, we will discuss-
- Project setup for Alert
- Handle alert with accept, dismiss, and leave.
- Verify alert text
Project setup for textbox:
Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/ )
After this, we can check our libraries by pip list command, or we can check manually also
File > settings > project: robot-automation > python interpreter

After verifying the prerequisite part create a file Alerts.robot in the TestCases directory.
— go to the new file > give a name of with .robot >Enter


Keywords:
- Handle Alert – Handles the current alert and returns its message.
- Input Text Into Alert – Types the given text into an input field in an alert.
- Alert Should Be Present – Verifies that an alert is present and by default, accepts it
- Alert Should Not Be Present – Verifies that no alert is present.
Now let’s write a test case for better understanding with Alert Keywords.
TestCase:
Step1: Go to https://demo.automationtesting.in/Alerts.html
Step2: Click on the element for the alert.
Step3: Accept alert.
Step4: Dismiss alert.
Step5: Leave alert.
Step6: Verify alert text which is present or not present.
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${url} http://demo.automationtesting.in/Alerts.html
${browser} chrome
*** Test Cases ***
HandlingAlerts
open browser ${url} ${browser}
maximize browser window
click element //button[contains(text(),'alert box:')]
sleep 3
#handle alert accept
#handle alert dismiss
#handle alert leave
alert should be present I am an alert box!
#alert should not be present I AM AN ALERT BOX!
close browser
*** Keywords ***
***Settings*** – To work with robot framework keywords we need to import the selenium library in the settings section because all these keywords are built in the selenium library-
Library SeleniumLibrary
Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section.
***Variables***– This section contains the defined variables that are used commonly, here we declared variables of browser and URL so we can reuse these variables.
${url} ${browser}
In the same way, we can create more variables like this according to the requirement of the project.
***Test Cases***-

Code Explanation:
The first step in this test is to visit the URL https://demo.automationtesting.in/Register.html in Chrome. We have already set up variables for this. Then we gave a name to our test HandlingAlerts. Open Browser * This keyword opens a specific browser for the given URL and maximizes browser window for maximizes the current browser window. Now we must click on the button element so we can see the alert. After this, we have set a time delay so we can see the alert sleep 3.
Handle alert accepts this keyword accept the alert, Handle alert dismiss this keyword dismiss the alert, Handle alert leave and this keyword leaves the alert. Through these three ways, we can handle alerts.
Now in the verification part, we are verifying the text of alert box by alert should be present keyword and if the alert text is “I am an alert box!” this will pass.
Similarly, we can verify text which is not present in the alert box by alert should not be present keyword and if the alert text is different from actual this will pass otherwise fail.
After executing all-possible keywords for the alert now we can go for close browser to close the browser and end the test.
Execution:
To execute the test case we need to use the command on the terminal:

After execution, we should get the result as a pass.

Report:


Conclusion:
In this Blog, we learned how to handle Alerts using the Selenium library in the Robot Framework. With the keywords provided by the robot framework and the library imported, we can get a text from alert, accept, dismiss and leave.
Read our next blog on “How to handle textbox in Robot Framework”.