Blogs

Solutions to CPSAT Python Mock Exams Mock Exam 2 – Question2

In the current blog series, Let us try to solve the problems of Mock Exam of CPSAT Python that can be found at following links

In the present blog we would be attempting Question 2 of Mock Exam 2.

Problem statement

Qn2. Using PyTest / WebDriver script,

  • open http://www.imdb.com/ in Google Chrome
  • Search for movie “Gangs of New York” and
  • open the movie details page and
  • perform the following :
    1. Verify that the movie’s run time is less than 180 min.
    2. Verify that the movie’s genre contains “Crime”
    3. Verify that the MPAA rating of the movie is “R”
    4. Go to the User Reviews section of the movie and display the name of the Reviewer on the PyCharm IDE console

Description

The Steps for automating the test for the given problem statement are as follows

Step 1 : Load the URL ‘http://www.imdb.com/’ in Chrome Browser

Step 2 : Search for ‘Gangs of New York’

Step 3 : Click on movie Link on search result page

Step 4 : Locate Quick Link Bar containing the below details required to validate

         R | 2h 47min | Crime, Drama | 20 December 2002 (USA)

Step 5 : Validate Runtime to be less than 180 mins

Step 6 : Validate Genre to be Crime

Step 7 : Validate MPAA Rating to be R

Step 8 : Click on User Reviews and print reviewer’s name

We will automate the test 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_mock2_Q2.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

Teardown method

Towards the end, we should release our resources like closing the driver instance is performed through another python method teardown() 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_* .

test_validate_movie_details()  is the test method written for this purpose and code walkthrough is as follows

  • Load the URL ‘http://www.imdb.com/’ in Chrome Browser (Line26)
  • Search for ‘Gangs of New York’ in the Search Box (Line28-Line32)
  • Click on movie Link on search result page(Line34)
  • Locate Quick Link Bar containing the below details required to validate(Line38-Line50)
    • R | 2h 47min | Crime, Drama | 20 December 2002 (USA) Splitting this Text String into a python list containing different details and each field is processed further for next set of validations
  • Validate Runtime to be less than 180 mins : Runtime field text obtained from step 4 is further splitted in hour part and min part and thus we can compute runtime in minutes(Line68) and therefore can compare if it is less than 180 mins or not.
  • Validate Genre to be Crime
  • Validate MPAA Rating to be R
  • Click on User Reviews and print reviewer’s name

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.

  • Open a terminal in pycharm and run at command prompt (>pytest test_Question_02.py )

Or

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

Conclusion

We have gone through step-by-step process to automate the Question2 of the Mock Exam 1 for CPSAT-Python through selenium automation for Chrome Browser using  pytest in python.

The complete python file may be referred at below link

Mock2/test_Question_02.py

*****

*******

Leave a Comment