Blogs

How to handle Alerts in Selenium Automation Testing with PyTest

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.

Problem statement:

1. Go to “https://the-internet.herokuapp.com/javascript_alerts”

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

  1. JS Alert :
    1. This provides only one interaction and that is to accept it by clicking OK Button.
  2. JS Confirm
    1. This allows either Accept by clicking OK button
    1. Or dismissing the Alert by clicking CANCEL button
  3. JS Prompt
    1. It facilitates proving some text message through input text box and
    1. Either Click OK to accept it
    1. Or click Cancel to dismiss it

We will automate these tests by using

  • Python
  • 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 .

Import section

In this section we import the modules that we are going to use subsequently in our test script eg. time, webdriver etc

Setup method

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.

Teardown method

Towards the end, we should release our resources like closing the driver instance is performed through another python method teardown() method.

Run PyTest

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.

  1. 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” ).

  1. Right click on test_Ex04_Alerts.py file on left panel, and Run ‘PyTest ……….’ As shown below

Conclusion

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

Test_E04_Alerts.py

If you want to attend CP-SAT Events / programs please do not miss to visit the following websites

Leave a Comment