Skip to content

itsGaryM/pytest-appium-demo

Repository files navigation

pytest-appium-demo

Automation tutorial on an Android calculator app using Appium with Pytest-BDD with parallel testing support. The updated repo now has gesture support as well as reporting with Allure.

Additionaly, the framework now supports gesture controls when the element ids are known. More information on the gesture plugin can be found here

GETTING STARTED

In order to successfully run this demo and build off of it, there are a couple of things that need to be done first. Here are the prerequisites:

- Python 3.8.10
- Pytest
- Pytest-BDD
- Android Studio
- Android SDK

Now go ahead and use the following command to download the Appium client for Python: pip install Appium-Python-Client

Download the Appium inspector, this will help us view the Android application's locators: https://github.com/appium/appium-inspector/releases

Add the following lines to your .bashrc file, with your respective path:

export ANDROID_HOME=/home/gmachorro/Android/Sdk

export PATH=$PATH:/home/gmachorro/Android/Sdk/tools

export PATH=$PATH:/home/gmachorro/Android/Sdk/platform-tools

Once the project is opened in an IDE of your choice, create a virtual environment and start it.

python3 -m venv .venv

source .venv/bin/activate

Run the following command to download the needed dependencies:

pip install -r requirements.txt

Start your Emulator by opening up the AVD in Android studio:

Open the terminal and paste this in to view the devices currently available and connected: adb devices

Note the 'emulator-5554' name, as this is important. This is the device udid

Now paste this to start the Appium server: appium --allow-insecure chromedriver_autodownload --base-path /wd/hub --use-plugins=gestures

Make sure your device udid obtained from the step above is included in the phones.json file

To run the tests marked as @calc, paste the following into the project terminal: pytest -m calc

That should get the project up and running for a single device!

How to automate with Appium

So far, everything listed above was to run the automation for the calculator app. In order to automate an app of your choosing, or to expand this one for practice, heres what you'll need to know.

To open your app youre going to want to use this fixture shown below and tweak it to open the app of your choosing

As you can see, the following lines are responsible for the driver opening up the calculator

    'appPackage': 'com.google.android.calculator',
    'appActivity': 'com.android.calculator2.Calculator'

In order to get the appPackage and appActivity of your desired app:

  1. open the emulator from the AVD or connect a physical device to your computer
  2. on the emulator or phone, open the desired app
  3. in the terminal type adb devices to verify phone is connected
  4. enter the following: adb shell dumpsys activity activities | grep -isw Hist

This was obtained by following the steps shown above for the calculator app

Once you replace the appPackage and appActivity fields in the fixture with your own, the app you selected will now open Just be sure to use the fixture if you decide to make your own

Automating mobile apps are similar to automating the web, we need locators to work off of.

Heres how to get locators for a mobile app.

For this, you will need a physical device, or a very good computer.

  • Connect a device(physical or emulator)
  • open the terminal
  • type adb devices and verify your device is shown
  • if you have not opened up Appium inspector previously, paste the following
    • chmod a+x Appium-Inspector-2024.3.4-linux-x86_64.AppImage
  • cd into the folder that contains appium inspector i.e cd Downloads
  • paste and enter to open Appium inspector
    • .//Appium-Inspector-2024.3.4-linux-x86_64.AppImage

It should look like this minus the details for the calculator

Once you click start session, you should be able to control the device using the inspector.

Here, you can click on anything to bring its information up on the right hand side

Here you can get the xpaths or ids for the items you would want to automate.

In our example, we have the ids of the numbers and buttons we want to automate under the calculator.py file

How to use the reporting feature

In order to view the reports after a run. Use the following commands: allure serve allure-results

In order to generate a copy of the report into a folder in your structure, use this command. allure generate --clean allure-results -o ./allure-reports

In some rare situations where you are running tests a lot, there will be reports with old data in them.

To fix this, run the following: rm -rf allure-results

This will delete old test data so that it does not appear on new reports

About

Mobile testing framework using Pytest-BDD, Appium and Allure reports. A small tutorial on android automation done on ubuntu

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors