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 5 of Mock Exam 3.
Q5. Open https://ata123456789123456789.appspot.com/ and select the “Euclid(+)” option with
first and second numbers 5 and 2 respectively. Write a PyTest / WebDriver test for Firefox to
confirm whether the result value is indeed generated by the equation
(a*a) + 2 * (a *b) + (b*b).
We need to perform following test by visiting https://ata123456789123456789.appspot.com/ and
- Input First number as 5 and
- Input Second number as 2
- Click on Euclid+ Radio Button
- Click on Calculate Button
- Get the Value displayed in Output Result Box
- Compare expected value with Actual Value
We would be using PyTest and automate testing for FireFox browser. IDE used for the purpose is PyCharm. Following are the important sections of any PyTest program:
- Setup method
- Teardown method
- Test Methods
Let us start with import section of the python solution
In this section we import all the modules/packages that are going to be used in subsequent sections. We have imported time as well as webdriver module as illustrated below
In the setup method, we perform all the preparatory activities before carrying out actual test like
- Declaring driver object as global so that the same driver object is referred in the entire program
- Initialisation of driver object as Firefox browser
- Maximising the window
- Setting up implicitly waits etc.
In contrast to the setup method, teardown method is intended to carry out closing activities towards the end like closing or quitting the browser window in order to release the resources.
As per PyTest nomenclature, all test method names should start with test_ like test_ata_calc_euclid() in Line 16.
Load the url through driver.get() method at Line17.
Locating various web elements with the help of locator strategies. Following web elements are of our interest for the current exercise:
- Input Box 1 (input1 at Line29)
Its always recommended to clear the Input Fields before sending in the input values. Hence input1.clear() at Line30 and inpu1.send_keys() to pass input1 value.
- Input Box 2 (input2 at Line33)
Input2.clear() at Line33 and inpu2.send_keys() to pass input2 value.
- Radio Button for selecting Euclid+ operation (eucplus at Line37)
Performing click() on this element to select among the group of radio buttons.
- Calculate Button (calc at Line40)
Calc.click() to performing calculation and result to be shown in result box.
- Result Box which is a Read Only Box(result at Line43)
Result is displayed in this output box. The result Box is readonly and therefore to get the value displayed in the Result box could be obtained by result.get_attribute(‘value’). This returned value is in String type thus we may convert to int at Line45.
- Putting an assert (at Line47) by comparing the expected result computed at Line27 and Actual Result at Line45.
Right clicking on the python file and click on Run ‘Pytest test_Question_05.py’
We have gone through the step-by-step process to solve CPSAT Python Mock Exam 3 – Question 5.
The complete python code may be referred at below link