Alerts appear quite frequently in most of the Web Applications. Many a times we experience that while clicking a button or submitting a form results into appearance of a popup window. This popup window is known as an Alert. This alert window is different in behaviour than other regular windows. The difference lies in the fact that the Alert has to be handled first, without which we may not perform any other action.
In order to better understand the Alerts and their handling through Selenium Automation using PyTest, Let us consider an below mentioned Exercise.
2a. Click on JS Alert and
Click ok button on Alert
Validate Result: “You successfully clicked an alert”
2b. Click for JS Confirm
i) click Ok and validate Result
ii)click cancel and validate result
2c. Click for JS Prompt and then
i) Input your name and click ok to validate result
ii) Input your name and click cancel to validate result
iii) No Input on JS Prompt and click ok to validate result
iv) No input on JS Prompt and click cancel to validate result
Here in this example, we would be considering three types of Alerts namely
- JS Alert :
- This provides only one interaction and that is to accept it by clicking OK Button.
- JS Confirm
- This allows either Accept by clicking OK button
- Or dismissing the Alert by clicking CANCEL button
- JS Prompt
- It facilitates proving some text message through input text box and
- Either Click OK to accept it
- Or click Cancel to dismiss it
We will automate these tests by using
- Selenium package
- PyTest library which is used as a unit testing framework along with python
- PyCharm IDE (Integrated Development Environment)
We create a python file whose name starts with “test_” and having extension of .py like test_AlertsHandling.py .
In this section we import the modules that we are going to use subsequently in our test script eg. time, webdriver etc
Inside setup method usually we carry out all the preparatory activities like setting driver properties, instantiating Chrome/Firefox Browser instance and maximise window and optionally setting the waits. It could be considered as an ordinary python method
Pytest test methods
Now we would be writing test methods and these pytest test should follow the specific nomenclature and name of each method must start with test_* .
In our case, first test method is test_validate_on_jsalert() that corresponds to task 2a of above problem statement.
Alert window could be thought as a separate window and before performing any task over Alert window, it is imperative to switch our driver object pointing to current window to this Alert window.
This switching is accomplished through driver.switch_to.alert operation on Line18. Thereafter we could perform accepting alert which is equivalent to clicking OK button available on the Active Alert.
And for validation we could get the Result message displayed at the left bottom of the page through Line20.
In similar way we can write our test methods corresponding to task 2b and 2c for the problem statement.
Towards the end, we should release our resources like closing the driver instance is performed through another python method teardown() method.
Now to run our tests, we need to configure pytest as our default test runner (for details please refer https://www.jetbrains.com/help/pycharm/pytest.html ).
We can run pytest in either of the two ways described below.
- Open a terminal in pycharm and run at command prompt (>pytest test_E04_Alerts.py )
It identifies all the test methods (here no. of test methods are 3 indicated on console by the messages “collected 3 items” ).
- Right click on test_Ex04_Alerts.py file on left panel, and Run ‘PyTest ……….’ As shown below
We have gone through step-by-step process to the exercise to handle Alerts through selenium automation through pytest in python.
The complete python file may be referred at below link
If you want to attend CP-SAT Events / programs please do not miss to visit the following websites