Cybersecurity, Mobile Integration Testing
Cybersecurity Mobile Integration Testing
Introduction
- The purpose of this document is to help development teams associated with the University of Illinois fulfill their responsibility to comply with Illinois Cybersecurity standards as described at https://cam.illinois.edu/policies/fo-36/
- Mobile Integration Testing provides an automated means to check iterations of an application for flow-breaking changes
- This article currently provides a overview of Mobile Integration Testing with RobotFramework for Android Applications
Install Android Studio
- Directions are found at https://developer.android.com/studio
- On Windows, accept the defaults
Android Studio Phone Emulator
Setup Emulator
If the AVD Manager is already present in the Tools Menu, this section is not necessary.
- Start New Project
- Empty new project with no steps, and accept the default everything
- Go to Settings and search for "AVD" to help ensure it shows up in the proper menus
- Select it
- Accept the changes
- Go To Tools --> AVD Manager
- Create Virtual Device
- Phone type as Pixel 2 (or another if desired)
- Choose Pie API (or another if desired)
- Accept all the things
- Download it before proceeding
- Downloading and installing may take some time
- Click Finish
- Verify Config (Can rename to something meaningful like "name01")
- Finish the setup
- Now it is available!
- Push the Play Button in the Virtual Device Description if it is not already running
- Open the phone
Test an APK
Any local APK can be utilized for testing, such as the Calculator App on Android.
It is also possible to test with a custom APK not available in the App Store.
To test with a custom APK:
- Procure the APK with which to test
- Start the Emulator Phone (directions to create are provided above in Setup Emulator)
- Drag and Drop the APK onto the phone
- Will install to the phone
- Windows Save location will be similar to
C:\Users\username\.android\avd\name01.avd
- Open the Application just installed
- Left Click "Home" and "Swipe Up" to find it
- Click the Application
- Can add to home screen by dragging it there
Integration Testing with Robot Framework
Robot Framework Installation
***
Note: Install these items as Administrator to avoid future issues ***
- Install Python with pip: https://www.python.org/downloads/windows/
- Install Wheel:
pip install wheel
(see https://wheel.readthedocs.io/en/stable/installing.html) - Install Node.js if necessary (may take a bit of time): https://nodejs.org/en/
- Select to include Chocolatey when prompted -OR-
- Install Chocolatey: https://chocolatey.org/install
- Install Robot Framework:
pip install robotframework
- Install Robot Framework Appium Library:
pip install robotframework-appiumlibrary
- Install Appium Command Line Tools:
- See http://appium.io/docs/en/about-appium/getting-started/ for details
- Appium:
npm install -g appium
- Set these Environment Variables:
ANDROID_HOME: C:\Users\username\AppData\Local\Android\Sdk
JAVA_HOME: C:\Program Files (x86)\Java\jre1.8.0_201
PATH: ADD %JAVA_HOME%\bin
- Appium Doctor:
npm install -g appium-doctor
- Verify Appium setup for Android usage:
appium-doctor --android
- Install Appium Desktop: https://github.com/appium/appium-desktop
- Information about UIAutomator with Appium: http://appium.io/docs/en/drivers/android-uiautomator2/index.html#the-uiautomator2-driver-for-android
Use Appium to Inspect APK Elements
- Start Emulated Device and ensure that Developer Settings are Turned On
- This can vary by Android Version
- Directions for various version can be found on the Android Studio Website: https://developer.android.com/studio/debug/dev-options
- Verify Devices:
C:\Users\username\AppData\Local\Android\Sdk\platform-tools>adb devices
List of devices attached
device-name device
- Start Appium Desktop and Start a Server with all the Defaults
- Need to get the App info to inspect the App:
- Open the App you want on the Emulated phone (This example uses Calculator)
- On CMD (see https://www.inviul.com/android-app-package-activity/ for details):
C:\Users\username\AppData\Local\Android\Sdk\platform-tools>adb shell
generic_x86_arm:/ $
generic_x86_arm:/ $ dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
mCurrentFocus=Window{abc123 u0 com.android.calculator2/com.android.calculator2.Calculator}
mFocusedApp=AppWindowToken{def456 token=Token{ghi789 ActivityRecord{jkl012 u0 com.android.calculator2/.Calculator t16}}}
generic_x86_arm:/ $
- Use all of this info to help fill out this section in the Appium Search
{
"platformName": "Android",
"platformVersion": "9.0",
"deviceName": "device-name",
"automationName": "UiAutomator2",
"appPackage": "com.android.calculator2",
"appActivity": "com.android.calculator2.Calculator"
}
- Should now be able to inspect the App in the interface to acquire id, Xpath, etc. data for elements in the App
- These are used to create the Robot Framework tests
- About Xpath: https://www.w3schools.com/xml/xpath_intro.asp
Run a Robot File
After inspecting an App, a Robot file can be created.
Robot Files are named as filename.robot
.
Robot Files are run as robot filename.robot
.
This is a sample Robot File for the Calculator App (named testcalc.robot
):
*** Settings ***
Documentation Test Cases for evaluation of a proper setup with
... RobotFramework, AppiumLibrary, and Emulated Android Phone
...
... This will solve the equation 2+4 and present 6 as the answer
...
... Various Click styles are presented for examples
...
... A screenshot will be saved to the working directory before closing
Library
AppiumLibrary
*** Variables ***
${LOCAL_APPIUM_SERVER} http://localhost:4723/wd/hub
*** Keywords ***
Open the Application
Open Application ${LOCAL_APPIUM_SERVER} platformName=android platformVersion=9.0
... deviceName=emulator-5554 automationName=uiautomator2
... appPackage=com.android.calculator2 appActivity=com.android.calculator2.Calculator
Enter the Equation
Click Element id=com.android.calculator2:id/digit_2
Click Element accessibility_id=plus
Click Element xpath=//android.widget.LinearLayout[@content-desc="Numbers and basic operations"]/android.view.ViewGroup[1]/android.widget.Button[4]
Solve the Equation
Click Element At Coordinates 600 1600
Close the Application
Capture Page Screenshot
Close Application
*** Test Cases ***
Valid Open and Close the Calculator
Open the Application
Enter the Equation
Solve the Equation
Close the Application
This can be run using robot testcalc.robot
. In the directory where this file is present, the following files will be created:
appium-screenshot-1.png
log.html
output.xml
report.html
Use a YAML File for Variables
Variables can be provided by an external file ( https://github.com/robotframework/robotframework/blob/master/doc/userguide/src/CreatingTestData/ResourceAndVariableFiles.rst ).
For example, a YAML file can be utilized to provide variables. Per the instructions at https://github.com/robotframework/robotframework/blob/master/doc/userguide/src/CreatingTestData/ResourceAndVariableFiles.rst#variable-file-as-yaml , installing PyYAML is necessary (instructions provided therein) for Robot to utilize the file correctly.
This is an example YAML file with example variables called testvals.yaml :
variable01: username
variable02: password
The Robot file will see/use the variables as:
YAML File Variable | Robot File Variable |
---|---|
variable01 | VARIABLE01 |
variable02 | VARIABLE02 |
This is an example Robot file segment using the YAML file to provide the variables:
*** Settings ***
Documentation This is a non-working script with example text
Variables testvals.yaml
*** Keywords ***
Enter Credentials
Input Text accessibility_id=user ${VARIABLE01}
Input Password accessibility_id=pswd ${VARIABLE02}
By using an external file, it is easier to share the test code with less risk of compromising test account data, etc.
More Resources
- Robot Framework
- General Information: https://robotframework.org/
- Libraries: https://robotframework.org/#libraries
- Appium: http://appium.io/docs/en/about-appium/getting-started/?lang=en
- Android AppiumLibrary
- General: https://github.com/serhatbolsu/robotframework-appiumlibrary
- Detailed Information about Tags, etc.: http://serhatbolsu.github.io/robotframework-appiumlibrary/AppiumLibrary.html
- UI Automator: https://developer.android.com/training/testing/ui-automator
- Gherkin Syntax: https://cucumber.io/docs/gherkin/reference/
- Training, etc.
- Useful training on LinkedIn Learning:
- Robot Framework Test Automation: Level 1 ( https://www.linkedin.com/learning/robot-framework-test-automation-level-1-selenium )
- Robot Framework Test Automation: Level 2 ( https://www.linkedin.com/learning/robot-framework-test-automation-level-2 )
- This website has some nice getting started information: https://www.pentalog.com/blog/mobile-automation-with-robot-framework-and-appium
- Testing with Forms: https://softwareengineering.stackexchange.com/questions/334618/acceptance-testing-form-fields-with-robot-framework
- Useful training on LinkedIn Learning: