Table of Contents
Getting Started
Introduction
In this series, we will setup a mobile automation framework from scratch, to test against a native Android application.
The completed repository can be found at https://gitlab.com/testifyqa/java-android-appium-maven
We will use the following in our framework:
- Selenium WebDriver
- Selenium is a portable software-testing framework for web applications. Selenium provides a playback (formerly also recording) tool for authoring tests without the need to learn a test scripting language (Selenium IDE). It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. The tests can then run against most modern web browsers. Selenium deploys on Windows, Linux, and macOS platforms. It is open-source software, released under the Apache 2.0 license: web developers can download and use it without charge.
- Selenium is required by Appium
- Selenium is a portable software-testing framework for web applications. Selenium provides a playback (formerly also recording) tool for authoring tests without the need to learn a test scripting language (Selenium IDE). It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. The tests can then run against most modern web browsers. Selenium deploys on Windows, Linux, and macOS platforms. It is open-source software, released under the Apache 2.0 license: web developers can download and use it without charge.
- Cucumber
- Cucumber is a software tool used by computer programmers for testing other software. It runs automated acceptance tests written in a behaviour-driven development style. Central to the Cucumber BDD approach is its plain language parser called Gherkin.
- TestNG
- TestNG is a testing framework for the Java programming language, inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities. It is similar to JUnit but has extra capabilities
- Appium
- Appium is an open source automation tool for running scripts and testing native applications , mobile-web applications and hybrid applications on Android or iOS using a webdriver.
Setting Up Java
Java JDK
The Java Development Kit is an implementation of either one of the Java Platform, Standard Edition, Java Platform, Enterprise Edition, or Java Platform, Micro Edition platforms released by Oracle.
- Download the Java JDK appropriate for your Operating System / Architecture from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
- Follow the wizard and complete the installation of the Java JDK
Setting Up Java Environment Variables
An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. Because every developer on a team will have a slightly different Development environment on there local machine, using environment variables is good practice to help easily find and use needed processes and/or paths in an environment.
MacOS:
- Open up Terminal and type the following (exclude typing the $’s)…
$ nano ~/.bash_profile
- Add the following lines to your .bash_profile file within Terminal to set the $JAVA_HOME environment variable…
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
- Save .profile and exit nano editor by pressing (in order, one at a time)…
Ctrl + O
[Return]
Ctrl + X
- Completely quit out of Terminal and restart it to launch a fresh Terminal window with your changes applied
- Confirm you have correctly set the $JAVA_HOME environment variable by typing in Terminal…
$ java -version
Windows:
- Click ‘Start’ and search for “System”
- Go to ‘Advanced System Settings’ –> ‘Advanced’ –> ‘Environment Variables’
- Under the ‘System Variables’ section, click ‘New’ and then add and set the following new System Variable…
- Variable Name:
JAVA_HOME
- Variable Value:
C:\Program Files\jdk1.#.#_###
(replace with directory path of where JDK was installed)
- Variable Name:
- Double-click on ‘Path’ in the System Variables pane
- Set and append the following PATH environment variable…
C:\Program Files\jdk1.#.#_###\bin;
({jdk-root-directory-path}\bin)
- Click ‘OK’ where applicable to apply your changes
- Confirm you have correctly set the $JAVA_HOME environment variable by opening CMD Prompt and entering…
$ java -version
(exclude typing the $)
Setting Up Maven
Maven is a build automation tool used primarily for Java projects, to manage its dependencies and any external libraries and packages the Java project may need. In Yiddish, the word maven means “accumulator of knowledge”.
Download Maven Binaries
- Download the Maven binaries from https://maven.apache.org/download.cgi
- If on Windows, download the zip file
- If on MacOS, download the tar.gz file
- Create a root directory for all your Framework related downloads (e.g. ‘C:\FrameworkSetup’ or ‘~/Users/username/FrameworkSetup’)
- Extract the downloaded Maven binaries and place the extracted Binaries directory inside the root directory you just created
Setting Up Maven Environment Variables
MacOS:
- Open up Terminal and type the following (exclude typing the $’s)…
$ nano ~/.bash_profile
- Add the following lines to your .bash_profile file within Terminal to set the $M2_HOME (Maven) environment variable…
export M2_HOME=/Users/username/FrameworkSetup/apache-maven-#.#.#
(replace directory with where you extracted Maven binaries to on your machine)
export PATH=$JAVA_HOME/bin:$PATH
- Save .profile and exit nano editor by pressing (in order, one at a time)…
Ctrl + O
[Return]
Ctrl + X
- Completely quit out of Terminal and restart it to launch a fresh Terminal window with your changes applied
- Confirm you have successfully added and set the environment variable for Maven by entering…
$ source ~/.bash_profile
(exclude the $)
$ echo $M2_HOME
(exclude the $)
Windows:
- Click ‘Start’ and search for “System”
- Go to ‘Advanced System Settings’ –> ‘Advanced’ –> ‘Environment Variables’
- Under the ‘System Variables’ section, click ‘New’ and then add and set the following new System Variables…
- Variable Name:
M2_HOME
- Variable Value:
C:\FrameworkSetup\apache-maven-#.#.#
(replace directory with where you extracted Maven binaries to on your machine)
- Variable Value:
- Variable Name:
MAVEN_HOME
- Variable Value:
C:\FrameworkSetup\apache-maven-#.#.#
(replace directory with where you extracted Maven binaries to on your machine)
- Variable Value:
- Variable Name:
- Double-click on ‘Path’ in the System Variables pane
- Set and append the following PATH environment variable…
%M2_HOME%\bin;
- Click ‘OK’ where applicable to apply your changes
- Confirm you have correctly set the Maven environment variables by opening CMD Prompt and entering…
$ mvn --version
(exclude typing the $)
Setting Up IntelliJ IDEA
IntelliJ IDEA is a Java IDE for developing computer software. It is developed by JetBrains, and is available as a free community edition, or as a paid commercial edition. For the purposes of this blog, the free, community edition is fine 🙂
- Download IntelliJ IDEA from https://www.jetbrains.com/idea/download
- Complete installation of IntelliJ IDEA
- Launch IntelliJ IDEA and choose to not import any settings (unless you have appropriate ones already)
- Select your UI theme and click ‘Next’
- Select your keymap scheme and click ‘Next’
- Keep clicking ‘Next’ without changing anything on the following screens in the wizard
- Click ‘Start using IntelliJ IDEA’
Setting Up Appium
Appium works by sending commands from our Appium client (added to our codebase in an IDE) to an Appium server, via JSON Wire Protocol. Appium Server then creates a mobile automation session for the client and checks against any Desired Capabilities that we have specified to then connect to our respective vendor-provided framework (e.g. Selendroid/UIAutomator for Android or XCUITest for iOS) and run our tests in a session against the appropriate mobile device/platform defined in our Desired Capabilities. Don’t worry if you don’t understand all of this now, as hopefully it will make more sense as we go through everything.
In order to set up our Appium environment, there are several things we need to do, as outlined below…
- Download and install Android Studio
- Check Android installation path on Mac machine
- Set $ANDROID_HOME environment variables path to Android SDK location and include bin folder paths in PATH variable
- Open Android Studio and configure virtual devices/emulators
- Open emulator and ensure it works
- Install Xcode
- Validate iOS simulators
- Download Node.js
- Set $NODE_HOME environment variables path
- Set NPM environment variables path
- Download Appium Server from NPM (Node Package Manager)
- Download Appium client (Selenium which is also required, has already been added in the Java Web Selenium framework)
Let’s begin 🙂 …
Android
Download and Install Android Studio
Mac
- Go to https://developer.android.com/studio/ and click ‘Download Android Studio’
- Agree to the terms and conditions and click ‘Download Android Studio for Mac’
- Launch the downloaded Android Studio DMG file
- Drag and drop Android Studio into the /Applications folder, then launch Android Studio
- Select whether you want to import previous Android Studio settings, then click ‘OK’
- Follow the install wizard, selecting ‘Standard’ and complete the installation of Android Studio and SDK
Windows
- Go to https://developer.android.com/studio/ and click ‘Download Android Studio’
- Agree to the terms and conditions and click ‘Download Android Studio for Windows’
- Launch the downloaded Android Studio installer file
- Follow any install wizard and install using default settings/options
- Select whether you want to import previous Android Studio settings and click ‘OK’
- Follow the install wizard, selecting ‘Standard’ and complete the installation of Android Studio and SDK
Set ANDROID_HOME environment variable
Mac
- Open up Terminal and type the following (excluding typing the $)…
$ nano ~/.bash_profile
- Add the following lines to your .bash_profile within Terminal, to set the $ANDROID_HOME environment variable as well as including the bin directory paths (e.g. for platform-tools and emulators etc.)…
export ANDROID_HOME=/Users/username/Library/Android/sdk
(change to your Android SDK path)
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_SDK/emulator:$ANDROID_SDK/tools:$PATH
Windows
- Right-click on the Start button and select ‘System’
- Select ‘System Info’ under the ‘Related settings’ section
- Click ‘Advanced System Settings’ and select the ‘Advanced’ tab
- Click ‘Environment Variables…’
- Under the ‘User Variable’ table, click ‘New’ to open ‘New User Variable’ dialog
- Put ANDROID_HOME as Variable Name
- Provide the path of the Android SDK folder for Variable Value (e.g. ‘C:\Android’ )
- Choose a path without whitespace like the example given
Path Variables (Windows)
- Go to the Android SDK folder in explorer
- Inside the SDK folder look for ‘tools’ and ‘platform-tools’ folder
- Go into Environment Variables again
- Go to User Variables table again and locate the Path variable.
- Select ‘Path’ in the User Variables table and click ‘Edit’
- Ensure the following are listed in Path…
- %ANDROID_HOME%
- %ANDROID_HOME%\tools\
- %ANDROID_HOME%\tools\bin
- %ANDROID_HOME%\platform-tools\
- Lets’s now check that we added the Android SDK environment and path variables correctly…
- Open up a CMD Prompt
- Enter in
adb
- If you see output like below then that means the Android SDK path variable has been configured successfully…
C:\Users\Username>adb Android Debug Bridge version 1.0.39 Version 0.0.1-4500957 Installed as C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe
- If you see output like below then that means the Android SDK path variable has been configured successfully…
Configure Android Virtual Devices
We will now configure some Android emulators that we can later use with Appium to run our tests on
- Launch Android Studio
- Select ‘Start a new Android Studio Project’
- Leave all the options as they are by default in the Create New Project wizard and keep clicking ‘Next’
- Click ‘Finish’ on the last screen of the wizard
- In Android Studio, advance to ‘Tools’ –> ‘AVD Manager’
- Click ‘Create Virtual Device’
- Select a suitable device definition (e.g. Phone – Nexus 5X) and click ‘Next’
- Select a suitable system image / Android OS (e.g. Oreo – API Level 27) and click ‘Next’ (usually latest is the best to choose, make sure it is compatible with Appium and download the image if necessary)
- Give the Android Virtual Device (AVD) a suitable name (e.g. Nexus5xOreo) and click ‘Finish’
- You can now click the ‘Play’ icon and open the virtual device if you want and play around with it (e.g. try opening Chrome browser in it or another app)
- From now on, you can also open up the AVD via Terminal. Open up Terminal and type in the following commands…
cd $ANDROID_HOME/platform-tools
emulator -avd yourAvdName
(replace ‘yourAvdName’ with name of the AVD you made)
Node.js & NPM (Node Package Manager)
Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser.
NPM is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry.
Mac
Setting up Homebrew
Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s macOS operating system. Originally written by Max Howell, the package manager has gained popularity in the Ruby on Rails community and earned praise for its extensibility.
- Open up Terminal and paste the following command to download and install Homebrew (if you run into any issues, consult https://docs.brew.sh/Common-Issues)…
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Once Homebrew is installed, let’s make sure Homebrew is up to date by running the following command…
brew update
- It is also recommended to run Brew Doctor and follow any instructions to resolve any errors/warnings that the brew doctor returns…
brew doctor
Add Homebrew’s location to $PATH
- Open up Terminal and type the following (excluding typing the $)…
$ nano ~/.bash_profile
- Add the following line to your .bash_profile within Terminal, to add Homebrew’s location to $PATH…
export PATH="/usr/local/bin:$PATH"
Windows
Setting up Chocolatey
Chocolatey is a package manager for Windows. It is a single, unified interface designed to easily work with all aspects of managing Windows software (installers, zip archives, runtime binaries, internal and 3rd party software) using a packaging framework that understands both versioning and dependency requirements. Chocolatey packages encapsulate everything required to manage a particular piece of software into one deployment artefact by wrapping installers, executables, zips, and scripts into a compiled package file. Chocolatey packages can be used independently, but also integrate with configuration managers like SCCM, Puppet, and Chef.
- Search for CMD Prompt
- Right-click on Command Prompt and select ‘Run as administrator’
- Run the following command…
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
- Let’s now check that Chocolatey was installed successfully by checking the version number (exit and restart command prompt first if necessary)…
chocolatey -v
Setting up Node.js and NPM
Mac
- Open up Terminal and use Homebrew to install Node.js (NPM will be installed with Node)…
brew install node
- You can check that Node.js and NPM were installed successfully by checking their appropriate version numbers in Terminal…
node -v
npm -v
Windows
- Open up an elevated Command Prompt (run as Administrator again) and use Chocolatey to install Node.js (NPM will be installed with Node)…
choco install nodejs.install
- Let’s then check that Node.js and NPM have been installed successfully by checking their version numbers. Enter each of the following in the elevated Command Prompt…
node -v
npm -v
In the next section, we will add create our project, add our dependencies and install Appium Server as well as Appium Java Client 😀