Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Creating Mobile Automation Testing Framework for Android Using Appium with Python

License

Notifications You must be signed in to change notification settings

ikostan/TestAutomationFrameworkUsingAppiumWithPython

Repository files navigation

License: Unlicense HitCount

Creating Mobile Automation Testing Framework for Android Using Appium with Python



Table of Contents:

  1. Main Objectives
  2. Official Documentation Sources
  3. Additional Tech Info Resources
  4. Dev Environment
  5. Nice to have tools
  6. General guidelines: How to set up dev environment
  7. Tech Issues and Problem Solving
  8. Drivers
  9. Images
  10. Page Locators
  11. Page Object Models
    i. Calculator:
    - Page Object Model
    - Base Element Model
  12. Selendroid Test App
  13. Tests
  14. Utils

Main Objectives:

  • Creating Mobile Automation Testing Framework for IOS and Android
  • Build fast and readable automation using minimal code
  • Showcase with effective way to identify app elements
  • Page Objects and Element Objects implementation
  • Industry-ready test structure
  • Ability to pass arguments from CLI (see get_args_from_cli file)
  • Build readable test report using Allure Framework
  • Test code should avoid violating principles like DRY, YAGNI, SRP and KISS.
  • Using built-in apps in order to accomplish all the above

Official Documentation Sources:

Additional Tech Info Resources:

Dev Environment:

Python Packages:

Full list of dependencies see here.

Nice to have tools:

  1. Fiddler: The free web debugging proxy
  2. Kite: Code Faster in Python
  3. Ragnorex: Smart Selector Generator
  4. Pynsource: Python source code into UML diagrams
  5. TeamViewer
  6. AirDroid
  7. QuickSupport
  8. App Info, Application Info

General guidelines: How to set up dev environment.

  1. Install Python
  2. Install PyCharm
  3. Configure Python virtual environment and activate it
  4. Install Python prerequisites/packages
  5. Install Java JDK > configure JAVA_HOME PATH
  6. Install Android Studio > configure ANDROID_HOME PATH
  7. Install Node JS
  8. Install Appium NPM package: open cmd > npm install -g appium
  9. Install Appium GUI client (optional)

NOTE: for more detailed info please see "Tech Issues and Problem Solving" section

Tech Issues and Problem Solving:

Changing the project interpreter in the PyCharm project settings


1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Project | Project Interpreter.
2. Expand the list of the available interpreters and click the Show All link.
3. Select the target interpreter. When PyCharm stops supporting any of the outdated Python versions, the corresponding project interpreter is marked as unsupported.
4. The Python interpreter name specified in the Name field, becomes visible in the list of available interpreters. Click OK to apply the changes.

For more info please check here

PyCharm - Choosing Your Testing Framework


1. Open the Settings/Preferences dialog, and under the node Tools, click the page Python Integrated Tools.
2. On this page, click the Default Test Runner field.
3. Choose the desired test runner:



For more info please see Enable Pytest for you project

Setting up Python3 virtual environment on Windows machine
  1. open CMD
  2. navigate to project directory, for example:
cd C:\Users\superadmin\Desktop\Python\CodinGame
  1. run following command:
pip install virtualenv
  1. run following command:
virtualenv venv --python=python
Activate Virtual Environment
In a newly created virtualenv there will be a bin/activate shell script. For Windows systems, activation scripts are provided for CMD.exe and Powershell.

  1. Open Terminal
  2. Run: \path\to\env\Scripts\activate


Source: https://pypi.org/project/virtualenv/1.8.2/

Auto generate requirements.txt


Any application typically has a set of dependencies that are required for that application to work. The requirements file is a way to specify and install specific set of package dependencies at once.
Use pip’s freeze command to generate a requirements.txt file for your project:

 pip freeze > requirements.txt

If you save this in requirements.txt, you can follow this guide: PyCharm - Manage dependencies using requirements.txt, or you can:

 pip install -r requirements.txt

Source: https://www.idiotinside.com/2015/05/10/python-auto-generate-requirements-txt/

JAVA_HOME PATH


A. In order to configure JAVA_HOME path do the following:

  1. Go to "Advanced System Settings" > Advanced Tab > Environment Variables
  2. Go to System Variables section > click on New... button
  3. Type Variable name: JAVA_HOME
  4. Enter Variable Value: C:\Program Files\Java\jdk1.8.0_221
  5. Press OK button

B. Edit environment variable:

  1. From System variables list select path > Press Edit... button
  2. Press on New button > type: %JAVA_HOME%\bin
  3. Press OK button
Environment variables for Android ADB (ANDROID_HOME PATH)

NOTE: Android Studio must be installed first.


A. In order to configure ANDROID_HOME path do the following:

  1. Go to "Advanced System Settings" > Advanced Tab > Environment Variables
  2. Go to System Variables section > click on New... button
  3. Type Variable name: ANDROID_HOME
  4. Enter Variable Value: C:\Users\username\AppData\Local\Android\Sdk
  5. Press OK button

B. Edit environment variable:

  1. From System variables list select path > Press Edit... button
  2. Press on New button > type: %ANDROID_HOME%\tools
  3. Press OK button
  4. Press on New button > type: %ANDROID_HOME%\platform-tools
  5. Press OK button

C. Test:

  1. Open CMD > run "adb devices"
  2. If everifyng is configured properly you will something like this:
Inspect devices with Chrome Developer
1. Open Chrome web browser > new tab
2. Paste following address: chrome:..inspect/#devices


If evryfing connected properly you will see something like this:

Source:

How to restart ADB server
  1. Open Command promt > run: adb kill-server
  2. press enter
  3. run: adb start-server
  4. press enter


Source: https://stackoverflow.com/questions/29826101/how-to-restart-adb-manually-from-android-studio

Connect to a device over Wi-Fi using Android Debug Bridge (adb)

adb is included in the Android SDK Platform-Tools package. You can download this package with the SDK Manager, which installs it at android_sdk/platform-tools/. Or if you want the standalone Android SDK Platform-Tools package, you can download it here.

Run Appium Tests On Real Android Device over Wifi:

  1. Connect your device to computer via USB and check if it is connected using ‘adb devices’ which will list you the devices connected to your machine.
  2. We should make sure that android device and the Laptop are connected to the same Wifi network. In my case wifi is on for laptop and mobile device and not need to purchase any wireless wifi adapter.
  3. Restart adb and make it work over tcpip by specifying the port value (**adb tcpip 5555). If no port number is specified, Port 5555 is used by default. This step will make your adb daemon to re-start on the specified port.
  4. We need to get the IP address of the the device. There are two ways to get IP address of your device by using below options:
    • Your device -> Settings -> Wifi -> Wifi Settings
    • adb shell ip -f inet addr show wlan0
  5. Run the below command to connect adb to your device over Wi-Fi using IP address of your device:
    • adb connect ip_address:tcpip_port
  6. Confirm that your host computer is connected to the target device:
    • adb devices

Source: https://developer.android.com/studio/command-line/adb
Source: https://rajeevkumarweb.wordpress.com/2017/06/03/run-appium-tests-on-real-android-device-over-wifi/

Discover current Activity name with dumpsys

dumpsys is a shell command. You can run it from the command line as follows:
- adb shell
- dumpsys window windows | grep -E 'mCurrentFocus | mFocusedApp'

If you have multiple devices connected, first get their IDs by executing:
- adb devices

Then, replace with the relevant device ID:
- adb -s shell
- dumpsys window windows | grep -E 'mCurrentFocus | mFocusedApp'


You should see something like that:

Source: https://developer.android.com/studio/command-line/dumpsys
Source: https://stackoverflow.com/questions/43178672/dumpsys-window-windows-grep-e-mcurrentfocusmfocusedapp-command-is-not-retu/46545726#46545726

Install the given app from .apk file onto the device

Full Documentation:

** Python code example (Appium):**

self.driver.install_app('/Users/johndoe/path/to/app.apk');

** Code example (adb):**

adb install path/to/your/app.apk('/Users/johndoe/path/to/app.apk');
Common Selenium errors


- How to fix common Selenium errors?

How to start/stop Appium server programmatically in Python

Using AppiumService Class

The Python client actually comes with a handy module called AppiumService that you can use to programmatically start/stop an Appium server.

To start:

from appium.webdriver.appium_service import AppiumService
appium_service = AppiumService()
self.appium_service.start()

To stop:

self.appium_service.stop()

Using CMD/scripting

To start:

import os
os.system("start /B start cmd.exe @cmd /k appium") 

In case you want to change the port(e.g. to 4728) of the appium server (may be when you have multiple servers for multiple devices) you can use following:

import os
os.system("start /B start cmd.exe @cmd /k appium -a 127.0.0.1 -p 4728")

To stop:

os.system("taskkill /F /IM node.exe") 

Source:

error: RPC failed; curl 56 Recv failure: Connection was reset
1. Open Git Bash
2. Run: "git config --global http.postBuffer 157286400"


Source: https://stackoverflow.com/questions/36940425/gitlab-push-failed-error

How to fix in case .gitignore is ignored by Git


Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore.

NOTE:
- First commit your current changes, or you will lose them.
- Then run the following commands from the top folder of your Git repository:

 git rm -r --cached .
 git add .
 git commit -m "fixed untracked files"