Python unit test framework for Mobile App test automation using Appium
Prerequisites for writing automation tests using python and Appium
- Basic Python programming knowledge
To write test automation using Python, we need not be Python experts. A basic knowledge is sufficient. I would say at least know the below concepts
- Python numbers, string, lists, tuples, dictionaries
- Python if, for, while loops
- Python classes and Objects
- Python packages, modules and importing modules
System setup required for mobile test automation using Python and Appium
- Python
- Appium
- Emulator or real device
- IDE like PyCharm
Python unittest framework
Here is the sample code of unittest framework,import unittest from appium import webdriver class MobileTest(unittest.TestCase): def setUp(self): appium_server = 'http://127.0.0.1:4723/wd/hub' desired_capabilities = dict() # creating an empty dictionary desired_capabilities['platformName'] = 'Android' desired_capabilities['platformVersion'] = '8.0' desired_capabilities['deviceName'] = 'Nexus_6' desired_capabilities['app'] = '/Users/preparationDocs/apps/appName.apk' desired_capabilities['appPackage'] = 'com.android.package' desired_capabilities['appActivity'] = 'com.android.apps.ui.HomeActivity' desired_capabilities['appWaitActivity'] = 'com.*' self.driver = webdriver.Remote(appium_server, desired_capabilities) self.driver.implicitly_wait(20) def test_open_search(self): self.driver.find_element_by_id("android:id/id_search_form").click() self.driver.find_element_by_id("android:id/id_search_form").send_keys("search string") self.driver.find_element_by_id("android:id/submit_button").click() def tearDown(self): self.driver.quit()
Lets go line by line,
import unittest
from appium import webdriver
`unittest` framework is the Python version of Junit. we need to import `unittest` module, in which all the unittest framework related classes are available using which we can organise the tests better. The second one is `webdriver` for appium, which will be used to handle the mobile app under test.
from appium import webdriver
class MobileTest(unittest.TestCase):
In the above line of code, `unittest.TestCase` class is being inherited to `MobileTest` user define class. Its Inheritance in Python. (I would suggest to understand the Inheritance concept if you dont know about it. I will try to give a crisp meaning of Inheritance here. In general Inheritance means extending the features and behaviour from a Parent to a Child, the same applies in Python as well. So, the child class `MobileTest` now have access to the features and behaviour of parent class `unittest.TestCase`)
setUp(self), tearDown(self)
The setUp and tearDown are the 2 methods, we can implement, generally used to write the pre and post activities of a test case. For example, if you want to open the mobile app before running the actual test case, the mobile app opening steps can be done in `setUp` method. `tearDown` method can be used to close the session/browser/app or to capture a screenshot or to send an email or any action that you wish to perform after the test is run.
If you observe the code snippet in setUp and tearDown methods, mobile app opening is done in setUp and closing the session is done in tearDown methods.
setUp method will be called before running the test method. tearDown method will be called after the test method. This setUp and tearDown will be called before and after each test method.
def setUp(self): appium_server = 'http://127.0.0.1:4723/wd/hub' desired_capabilities = dict() # creating an empty dictionary desired_capabilities['platformName'] = 'Android' desired_capabilities['platformVersion'] = '8.0' desired_capabilities['deviceName'] = 'Nexus_6' desired_capabilities['app'] = '/Users/preparationDocs/apps/appName.apk' desired_capabilities['appPackage'] = 'com.android.package' desired_capabilities['appActivity'] = 'com.android.apps.ui.HomeActivity' desired_capabilities['appWaitActivity'] = 'com.*' self.driver = webdriver.Remote(appium_server, desired_capabilities) self.driver.implicitly_wait(20) If you look at the setUp method, I have declared an empty dictionary and added up all the required parameters for app initialisation on appium running on my local machine. def tearDown(self): self.driver.quit() here, I'm just quitting the driver session. You can add more steps to capture screenshot or sending an email..
def test_open_search(self): self.driver.find_element_by_id("android:id/id_search_form").click() self.driver.find_element_by_id("android:id/id_search_form").send_keys("search string") self.driver.find_element_by_id("android:id/submit_button").click() So, this code snippet enters some search string and clicks on the search button. If the setUp method is failed due to some reason then this `test_open_search` method will not be run.
You can refer this appium library manual to know the define methods to handle mobile app.
Appium Library
Summary Points till here :
- setUp and tearDown method will be called for every method which has name starting with `test_`
- If setUp method is failed, then the test method and tearDown will not be executed
- A class can have more than one test methods
- Class inheriting `unittest.TestCase` only be treated as a test case file
I have the Mobile App Test Automation with Python unit test framework using POM on git hub. Please go through it. Mobile App test automation using Python
I will keep it updated. Thanks for reading. Happy learning. Please comment if you dont understand something here or if you find anything incorrect. Thankyou.
Comments
Post a Comment