-
Notifications
You must be signed in to change notification settings - Fork 191
GUI Automation Framework Quick Start Guide
The sample application used for this guide is having two web pages, the main web page having some common HTML elements that you will interact and other is a redirect page.
The main web page allows a user to do things:
- Navigate to a specific URL
- Fill text fields
- Set a dropdown
- Enable/disable a checkbox
- Click a button
- Identify elements without ids
- Check for form validation messages
- Check for successful form submit
- Hover and click
- Count the number of rows in a table
- Get the text from a table row
- Get text in a cell of a table
We are going to explain about Page Objects with a very simple test case. You can find the corresponding code in our GitHub Repository.
- Goto https://qxf2.com/selenium-tutorial-main
- Fill the example form
- Click on Click me! button
Our sample tests(test_example_form.py) would fill the example form in the selenium-tutorial-main page and get redirected to the selenium-tutorial-redirect page when the form is submitted. Each page of the application to be tested is treated like an Object which has the variables (xpaths) and methods (actions that can be performed on that particular page).
The selenium-tutorial-main web page consists of a header, footer, form and table objects. The header and footer objects are common across both the pages. The form and table objects are specific to the webpage.
We have created Page Objects for the following:
-
header_object.py
models the header objects like the logo, the tag-line and the hamburger menu as Page objects. -
footer_object.py
models the footer objects like menu and copyrights as Page objects. -
form_object.py
models the form on the selenium-tutorial-main page as Page objects. The form consists of some input fields, a drop-down, a checkbox and a button. This script has all the action methods like set_name(), set_phone()etc., to set the corresponding values in the form, which are fetched from the locators using XPath. In this guide, we will be referring to this Page Object only. -
tutorial_main_page.py
models the selenium-tutorial-main page. Since the page consists of a header, footer, form and table objects, we do an import of all the Page Objects.
from header_object import Header_Object
from footer_object import Footer_Object
from form_object import Form_Object
from table_object import Table_Object
-
tutorial_redirect_page.py
models the Page Object for the selenium-tutorial-redirect page.
Here is the sample test script for setting the name in the Name field of the example form.
#The import statements import: standard Python modules,conf,credential files
from page_objects.PageFactory import PageFactory
from utils.Option_Parser import Option_Parser
import conf.example_form_conf as conf
import conf.testrail_caseid_conf as testrail_file
def test_example_form(base_url,browser,browser_version,os_version,os_name,remote_flag,testrail_flag,tesults_flag, test_run_id,remote_project_name,remote_build_name):
"Run the test"
try:
#Initalize flags expected and actual pass are used to keep track of the number of passed and failed
expected_pass = 0
actual_pass = -1
#1. Create a test object and fill the example form.
test_obj = PageFactory.get_page_object("Main Page",base_url=base_url)
#2. Setup and register a driver
start_time = int(time.time()) #Set start_time with current time
test_obj.register_driver(remote_flag,os_name,os_version,browser,browser_version)
#3 Get the test details from the conf file
name = conf.name
email = conf.email
phone = conf.phone_no
gender = conf.gender
#4. Set name in example form
result_flag = test_obj.set_name(name)
test_obj.log_result(result_flag,
positive="Name was successfully set to: %s\n"%name,
negative="Failed to set name: %s \nOn url: %s\n"%(name,test_obj.get_current_url()))
except Exception as e:
print ("Exception when trying to run test:%s"%__file__)
print ("Python says:%s"%str(e))
assert expected_pass == actual_pass
We used XPath selector for locating the web elements and stored the locator information in the locators_conf.py file. In the Page Object form_object.py
, we are fetching the locators and setting the corresponding values in the set_name() method.
form_object.py
import conf.locators_conf as locators
class Form_Object:
"Page object for the Form"
#locators
name_field = locators.name_field
email_field = locators.email_field
For understanding XPath structure and writing XPath locators, refer to our blog Getting started with xpaths
Below are few sample commands for running tests:
a) py -m py.test [options]
-s used to display the output on the screen E.g: py.test -s (This will run all the tests in the directory and subdirectories)
-U used to run against specific URL E.g: py.test -U http://YOUR_localhost_URL (This will run against your local instance)
-M used to run tests on Browserstack/Sauce Lab E.g: py.test -s -M Y -U https://qxf2.com
-B all used to run the test against multiple browser E.g: py.test -B all(This will run each test against the list of browsers specified in the conftest.py file,firefox and chrome in our case)
-V/-O used to run against different browser versions/os versions E.g: py.test -V 44 -O 8 (This will run each test 4 times in different browser version(default=45 & 44) and OS(default=7 & 8) combination)
-h help for more options E.g: py.test -h
-k used to run tests which match the given substring expression E.g: py.test -k table (This will trigger test_example_table.py test)
-S used to post pytest reports on the Slack channel E.g: py.test -S Y -v > log/pytest_report.log
-n used to run tests in parallel E.g: py.test -n 3 -v (This will run three tests in parallel)
b) python tests/test_example_form.py
(can also be used to run standalone test)
c) python tests/test_example_form.py -B Chrome
(to run against chrome)
d) python tests/test_api_example.py