-
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.
Note: page objects of selenium tutorial web page available at page_objects/examples/selenium_tutorial_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
import os,sys,time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from page_objects.PageFactory import PageFactory
import conf.example_form_conf as conf
import pytest
def test_example_form(test_obj):
"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
# Create a test object and fill the example form.
test_obj = PageFactory.get_page_object("Main Page",base_url=test_obj.base_url)
#Set start_time with current time
start_time = int(time.time())
# Turn on the highlighting feature
test_obj.turn_on_highlight()
# Get the test details from the conf file
name = conf.name
email = conf.email
phone = conf.phone_no
gender = conf.gender
# 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) python -m pytest [options]
-s used to display the output on the screen E.g: python -m pytest -s (This will run all the tests in the directory and subdirectories)
--base_url used to run against specific URL E.g: python -m pytest --base_url http://YOUR_localhost_URL (This will run against your local instance)
--remote_flag used to run tests on Browserstack/Sauce Lab E.g: python -m pytest -s --remote_flag Y --base_url https://qxf2.com
--browser all used to run the test against multiple browser E.g: python -m pytest ---browser all(This will run each test against the list of browsers specified in the conftest.py file, firefox, and chrome in our case)
--ver/--os_version used to run against different browser versions/os versions E.g: python -m pytest --ver 44 --os_version 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: python -m pytest -h
-k used to run tests which match the given substring expression E.g: python -m pytest -k table (This will trigger test_example_table.py test)
--slack_flag used to post pytest reports on the Slack channel E.g: python -m pytest --slack_flag Y -v > log/pytest_report.log
-n used to run tests in parallel E.g: python -m pytest -n 3 -v (This will run three tests in parallel)
--tesults used to report test results to tesults E.g: python -m pytest test_example_form.py --tesults Y(This will report test report to tesults)
b) python -m pytest tests/test_example_form.py
(can also be used to run standalone test)
c) python -m pytest tests/test_example_form.py --browser Chrome
(to run against chrome)
d) python -m pytest tests/test_api_example.py
e)python -m pytest tests/test_mobile_bitcoin_price --mobile_os_version (android version) --device_name (simulator) --app_path (.apk location on local) --remote_flag Y
(to run Mobile test case on Broswestack) NOTE: For running tests in Browserstack, need to update Username/Accesskey from Browserstack Account and need to set remote platform to "BS" in ".env.remote". Refer or rename env_remote file to .env.remote and update details.
f) python -m pytest --browser headless-chrome
(to run a test against headless-chrome)
g) python -m pytest -k example_form --browser firefox --ver 57 --os_name windows --os_version 10 --remote_flag Y
(to run a test against browserstack/sauce lab)