Mobile App Test Automation using Appium Python Behave

Mobile Native app test automation using Appium, Python and Behave framework


In this article we are going to see how we can we do the required setup and write a small test case automation for an IOS app using Appium, Python and behave framework. I have implemented POM as well.

Pre-requisites

  • Python installed
  • Behave installed
  • Appium installed
  • Xcode installed - for simulators
  • Pycharm for writing the code


Project structure for Mobile App test automation using Python using Behave and POM

I used four main directories as followed

  • commonFunctions - for all the setup, util related
  • externalResources - for having all the data, other information
  • features - for writing feature files, related step definitions and behave level functions
  • page - for maintaining the POM



commonFunctions:

  • config.py : In this module I have written the code related to appium launch and app launch
  • session_handler.py :Is all about closing the session after the test run is complete, In this project I have configured to close the session after every scenario
  • util.py : Is to have very generic util functions like reading a yaml file, etc..

externalResources:

  • deviceInfo.yaml : I used this yaml file to keep the device configuration, I read this info based on the device that needs to be picked up for running the scenario. I select the device based on tags

features:

  • All the feature files can be written in this directory
  • Create a folder with name 'steps' and write the step definition files inside that
  • create a python file and write all the behave level function in it. please ensure that behave level function should be inside features folder along with feature files.

page:

  • basepage.py is to write all the common functions at a driver instance level to handle the interactions with the driver
  • home_page.py is to have all the actions of the app home page. If we have many pages in an app, we can have those many individual pages to handle each page level actions


Lets go into more details,

How to deal with a scenario in feature file using Behave

  • Write the scenario in feature file
  • Creating respective step definitions
  • writing before and after functions based on requirement

Write the scenario in behave feature file

Here is a simple example on writing the scenarios in feature file.
  • In general Feature is similar to test scenario, behave scenario represents a low level test case
  • A Feature file may have many scenarios, each scenario should have a name representing what it does. Scenario has many test steps
  • Given, When, Then are Gherkin commands
  • @smoke, @device-iPhone8Plus are the tags given to a scenario


Step definition creation

Step definition is nothing but mapping a function to each step in the scenario. Here is the snapshot on how to write step definitions and accepting parameters from each step, accessing them in the step definition function.
Context parameter is the context object which holds feature file level information. we can access feature level information like feature name, scenario name, tags and all. You can read more here

How to write before and after functions with behave, Python

I have written behave level functions in environment.py file. Remember that the should be placed along with feature files. Then only python can identify the before and after functions, if you place it else where Python cannot read those functions.
In this project, I am starting the appium server in before feature function. In before scenario function I have written the code to yield the app configuration information for that particular scenario based on tags of that scenario. And in after scenario the session will be closed.

How to create a appium driver instance for an IOS app using Python

I have the app configuration information in deviceInfo.yaml file of externalResources directory.

dc = {
"app": "/Users/apps/{}".format(app_name),
"platformName": "{}".format(platform_name),
"platformVersion": "{}".format(platform_version),
"deviceName": "{}".format(device_name),
"automationName": "appium",
"udid": "{}".format(udid),
# "noReset": True,
"newCommandTimeout": 60
}

self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", dc)


Okay, Once you have written the tests, you can simply run the scenarios using "behave" command from the root of the project on terminal. By default behave command runs all the feature files in the features folder. So to run selectively use behave --tags=tag_name command.

Here is the complete code of this project Github Link


How to start appium using Python

Comments