Hi folks. Hope you're all in good spirits. For those of you into automation, desktop or mobile: Open source tools rule. And they rule because they are, well ... Open source. Open source tools are freely available for use by all. And what's more, if you are well acquainted with these tools, you'll find your career graph shoot up exponentially. Unfortunately, since they are open source there's not much of documentation and support available for them online. Most of these tools are updated and documented by programmers passionate about these tools and software in general. Hats off to them.
There are numerous open source automation frameworks available these days for automating Mobile and Web apps. Appium is one such popular tool. It can be used to automate native, hybrid and web based apps and pages on iOS, Android and FireFox OS operating systems.
In this blog, I will outline the steps required to install and setup Appium for automating Android application using JAVA on your Windows machine. This blog will cover the following topics:
Appium has the following disadvantages:
Appium is actually a server written in Node JS. Clients initiate a session with the Appium server by ending a POST request with instructions (containing the code for tests to run). Settings required to run the test are sent to the server through the 'Desired Capabilities' object. After receiving the request, Appium sets up a session on the server with a unique session ID. User can now send multiple test requests for execution after the server is setup. For Android, Appium uses the UIAutomator framework and sends a HTTP response to the client.
Installation and proper setup of the Appium test framework is the most crucial and complicated step for running your Appium Tests. Without proper setup, you will not be able to run your Appium tests. What makes the setup process complicated is the dependency the Appium tool has on other third party and open source applications and software. Apart from the actual server application (Appium.exe) itself, you will require installation of various other third party tools for proper execution. The entire installation and setup process is mentioned below:
//Mobile OS version – in this case 4.4.2
since my device is running Android 4.4.2
Initiate your test by adding the following:
This is the default port number from which the Appium Server will be initialized.
Elements can be detected by using the UI Automator application. This application is found in the 'tools' sub folder of your Android SDK folder. To detect the element, open UI Automator and take a screenshot of the page on which the script is to be run.
The right hand side of the UI Automator application shows all the elements of the page grouped by classes. Click on the desired element in the screenshot. The bottom right hand side window will populate with all the details of the selected element. For example, click on the text window under the text "Enter First Number". The element gets highlighted by a red rectangular colour. You will see the element details populate on the bottom right side table. Since we are using Selenium Web Driver libraries and classes, we can use the 'findelement' method to detect and select elements using various parameters such as 'Class', 'Name' or 'Id'.
To detect and select the element, first create a WebElement object and assign the relevant method to it:
For example:
Similarly, create an object for the multiply button, select it and click on it by using the following code:
This should click the 'Multiply' button and display the result of the product of '56' and '65'. After your test is done, use the following command below to exit the script cleanly and end the test.
Since the actual test runs very fast, I have added a few thread.sleep methods to pause the test in between, so that you can get a better idea of how the test runs and to give the app some breathing space (Phew!)
There are numerous open source automation frameworks available these days for automating Mobile and Web apps. Appium is one such popular tool. It can be used to automate native, hybrid and web based apps and pages on iOS, Android and FireFox OS operating systems.
In this blog, I will outline the steps required to install and setup Appium for automating Android application using JAVA on your Windows machine. This blog will cover the following topics:
- A brief introduction to Appium and it's features
- Pros and Cons of using Appium
- Tools required to run Appium
- Installation and setup of Appium (for running tests via JAVA)
Why Appium?
Pros
Appium has the following advantages:
- Appium is open source and free. All supporting tools required for setting up the Appium Test Framework are open source and can be downloaded from the net. There are no licensing and fees involved.
- Appium can automate a wide variety of apps supported on major mobile Operating Systems such as Android, iOS and FireFox OS.
- Appium supports the Selenium Web Driver framework, so users can script their tests in various Web Driver Compatible languages such as JAVA, Python etc and also use Selenium WebDriver Libraries.
- Appium scripts are cross-platform compatible. A Script created for Android can also be used for iOS as well(with a few tweaks of course).
- User does not require the source code for running tests.
- User does not need to recompile the app again for running Appium tests. This means that your app is tested as is in real world conditions.
- Appium is supported by proprietary cloud based test frameworks such as Saucelabs which can run your tests on Simulators close to actual device requirements
- There is no need to Jailbreak or root the device for running Appium Scripts
- Appium provides a wide range of versatile functionalities for running tests.
Cons:
Appium has the following disadvantages:
- Appium setup is a bit complicated and time-consuming, especially for first time learners.
- Appium cannot support OS below 4.2
- Appium does not have a record and playback tool for Windows
- Appium is not very stable at high loads.
Appium Architecture
Appium is actually a server written in Node JS. Clients initiate a session with the Appium server by ending a POST request with instructions (containing the code for tests to run). Settings required to run the test are sent to the server through the 'Desired Capabilities' object. After receiving the request, Appium sets up a session on the server with a unique session ID. User can now send multiple test requests for execution after the server is setup. For Android, Appium uses the UIAutomator framework and sends a HTTP response to the client.
Installing and setting up Appium on your windows machine for Android Tests
Installation and proper setup of the Appium test framework is the most crucial and complicated step for running your Appium Tests. Without proper setup, you will not be able to run your Appium tests. What makes the setup process complicated is the dependency the Appium tool has on other third party and open source applications and software. Apart from the actual server application (Appium.exe) itself, you will require installation of various other third party tools for proper execution. The entire installation and setup process is mentioned below:
Tools required for setting up Appium
Appium.exe
This is the windows Appium server application. It can be downloaded via the Official Appium WebSite.
For downloading the latest Appium Server app, follow the instructions below:
The latest version of Appium has Node JS included which automatically installs along with the Appium server installation. If you are using an older version of Appium then you need to install Node JS on your machine. It can be downloaded and installed from the official Node JS website.
The latest version of the JAVA SE Development Kit (JDK) needs to be downloaded from the Oracle Website.
Download the latest Eclipse tool for scripting and executing your JAVA code. Download from the Eclipse Website.
After installing JAVA and eclipse, you need to set the proper path for JAVA via the Environment variables window. Click on the Windows Start button. In the Start menu right click on 'My Computer' and select 'Properties'.
This will open the 'System Properties' page. Click on the 'Advanced System Settings' link seen in the left hand side of the page. This will open the 'System Properties' window. Click on the Environment Variables button at the bottom.
Here you will see two types of variables, user variables and system variables. User variables are settings for the specific user only. System variables are variables for all users accessing the machine. Choose any type of variable as per your needs.
Create new System variable 'JAVA_HOME' and set value to your JDK installation directory.
In the PATH system variable, add the following parameter:
<Path to your JDK folder>\bin
Eg: C:\Program Files\ Java\ jdk1.8.0_40\bin
Go to the relevant Android web site and download the latest Android Software Development Kit for your machine. SDK zip gets downloaded. Extract the same on your machine and make a note of the path.
Go to My Computer > Properties > Advanced System Settings > Environment Variables
Create a new system variable ANDROID_HOME and set the value to the path where the SDK is extracted on your machine.
ANDROID_HOME: <path to your SDK folder>\sdk
Ex: ANDROID_HOME: D:\Work\android_sdks
The edit the 'PATH' system variable and add the following values to the same:
In case developer options are not enabled on your emulator or android device, enable the same by going to Device Settings. Tap on the 'About Phone' link 6 times. Developer options will appear. If your phone is connected to PC, your device will be prompted to accept an RSA key for linking between device and PC. Accept the same. This will ensure that your device /emulator is properly linked to your PC / Laptop.
MS.net is essential for Appium to function. Go to the .NET download page and download version 4.5 or later.
Go to the Selenium Website, download and extract the following files to any safe location on your PC / Laptop:
a) Selenium Standalone Server file (Jar file)
b) Selenium Client and WebDriver Binding files for JAVA
Last but not the least, we have to download the Appium JAVA binding file for JAVA. It's a JAR file and can be found at the following location:
b) The latest Selenium JAVA Web Driver JAR file:
c) All the jar files in the /libs folder
d) The selenium server standalone JAR file
10. Connect your device to your PC/Laptop via USB. Ensure that USB debugging is ON.
For downloading the latest Appium Server app, follow the instructions below:
- Open the Appium Website.
- Click on the 'Download Appium' link seen on the website. This will automatically start the download of the latest version of the Appium application
Node JS
The latest version of Appium has Node JS included which automatically installs along with the Appium server installation. If you are using an older version of Appium then you need to install Node JS on your machine. It can be downloaded and installed from the official Node JS website.
JAVA - JDK & JRE
The latest version of the JAVA SE Development Kit (JDK) needs to be downloaded from the Oracle Website.
- Open the Website http://www.oracle.com/technetwork/java/javase/downloads/index.html
- Download the latest version of the JDK and JRE for windows
Eclipse
Download the latest Eclipse tool for scripting and executing your JAVA code. Download from the Eclipse Website.
Setting up Environment and System variables for JAVA
After installing JAVA and eclipse, you need to set the proper path for JAVA via the Environment variables window. Click on the Windows Start button. In the Start menu right click on 'My Computer' and select 'Properties'.
This will open the 'System Properties' page. Click on the 'Advanced System Settings' link seen in the left hand side of the page. This will open the 'System Properties' window. Click on the Environment Variables button at the bottom.
Here you will see two types of variables, user variables and system variables. User variables are settings for the specific user only. System variables are variables for all users accessing the machine. Choose any type of variable as per your needs.
Create new System variable 'JAVA_HOME' and set value to your JDK installation directory.
In the PATH system variable, add the following parameter:
<Path to your JDK folder>\bin
Eg: C:\Program Files\ Java\ jdk1.8.0_40\bin
Android SDK
Go to the relevant Android web site and download the latest Android Software Development Kit for your machine. SDK zip gets downloaded. Extract the same on your machine and make a note of the path.
Setting up Environment and System variables for ANDROID
Go to My Computer > Properties > Advanced System Settings > Environment Variables
Create a new system variable ANDROID_HOME and set the value to the path where the SDK is extracted on your machine.
ANDROID_HOME: <path to your SDK folder>\sdk
Ex: ANDROID_HOME: D:\Work\android_sdks
The edit the 'PATH' system variable and add the following values to the same:
- %ANDROID_HOME%/tools
- %ANDROID_HOME%/platform-tools
Enable developer 'Options' on your device or Emulator
In case developer options are not enabled on your emulator or android device, enable the same by going to Device Settings. Tap on the 'About Phone' link 6 times. Developer options will appear. If your phone is connected to PC, your device will be prompted to accept an RSA key for linking between device and PC. Accept the same. This will ensure that your device /emulator is properly linked to your PC / Laptop.
Download and Install PdaNET software (optional)
Go to the PDA website. Download and install the latest version of the Software PdaNet+. This will install the latest drivers for your phone. This will ensure that your phone is detected in the Android Device Monitor. Use this option if your laptop does not detect and install the mobile device USB drivers automatically.Microsoft .NET framework version 4.5 or above
MS.net is essential for Appium to function. Go to the .NET download page and download version 4.5 or later.
Selenium WebDriver Files
Go to the Selenium Website, download and extract the following files to any safe location on your PC / Laptop:
a) Selenium Standalone Server file (Jar file)
b) Selenium Client and WebDriver Binding files for JAVA
Appium language bindings for JAVA
Last but not the least, we have to download the Appium JAVA binding file for JAVA. It's a JAR file and can be found at the following location:
Now you have all the files you require for setup. Now let's look into the installation of the Appium server for running our automation Scripts.
Setting up your Appium Test project in Eclipse
- Download the APK to be tested to any safe location on your PC / Laptop. For this example, we are going to automate the simple Android calculator for this sample project test.
- Extract and download the Selenium Standalone and WebDriver files to any location on your PC. Note the location.
- Open Eclipse. Start a new JAVA project. Click on New > JAVA Project
4. Create a new JAVA project for ex: "NewAppiumTest"
5. Create a new package ex: 'AppiumTest'
6. Create a new class file ex: 'AppiumTest' (or any other name as per your choice).
7. Right click on project folder and Select Build path > Configure Build path.
8. In the JAVA build path window, switch to 'Libraries' tab.
9. Click on the 'Add External JAR's' button and add the following JAR files:
a) The Latest Appium JAVA binding file
b) The latest Selenium JAVA Web Driver JAR file:
c) All the jar files in the /libs folder
d) The selenium server standalone JAR file
10. Connect your device to your PC/Laptop via USB. Ensure that USB debugging is ON.
Executing your First Appium Script
Congratulations. The Appium setup part is done. You are now ready to execute your first Appium Script. For this demo we are going to use the simple Android Calculator application. Place the file at any desired location on your PC / Laptop and remember it (Ex: D:\Radio\AndroidCalculator-V1_0.apk)
Import the Selenium WebDriver and Appium Driver libraries to your Appium Script:
import io.appium.java_client.AppiumDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
Importing these libraries will call Web Driver interfaces and classes for running tests.
Note: Not all the libraries imported will be used in your test script. But it is better you import all of them to avoid any run-time compilation errors.
Create the methods for scripting your tests:
public static void main(String[] args) throws Exception {
// TODO Auto-generated
method stub
// creating a class
object
AppiumTest testcases = new AppiumTest();
// call launch app method
testcases.launchCalc();
// call add name method
testcases.addName();
}
Setup the Android APK file path:
public void launchCalc() throws
InterruptedException, MalformedURLException
{
File app=new File("D:\\Radio\\AndroidCalculator-V1_0.apk");
Initialize the test method. Remember the path to the Android APK you want to test and setup the path accordingly.
Set the Desired Capabilities:
Desired capabilities are a set of values sent to the Appium server which define the parameters for the test to be executed. They are setup in the following manner:
DesiredCapabilities capabilities = new
DesiredCapabilities();
//Name of mobile web browser to automate. Should be an empty
string if automating an app instead.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
//which mobile OS to use: Android, iOS or
FirefoxOS
capabilities.setCapability("platformName", "Android");
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
//device name – since this is an actual device name is
found using ADB devices command in CMD
capabilities.setCapability("deviceName", "92268ee10561a040");
//the absolute local path to the APK
capabilities.setCapability("app", app.getAbsolutePath());
//Java package of the tested Android
app
capabilities.setCapability("appPackage", "com.calculator");
// activity name for the Android activity
you want to run from your .
This need to be preceded by a . (example: .MainActivity)
capabilities.setCapability("appActivity", ".Main");
Note:
- Device name can be detected by entering the command 'adb devices' in your command prompt window.
- Your Android App package name and Main Activity name can be detected by using the following command:
aapt list -a "path to apk"> file.txt
Alternatively, loading the APK in Android settings of the Appium tool gives the package and
the main activity info also.
Starting your test
Initiate your test by adding the following:
// constructor to initialize driver
object
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
This is the default port number from which the Appium Server will be initialized.
Detecting Elements on your Android App
Elements can be detected by using the UI Automator application. This application is found in the 'tools' sub folder of your Android SDK folder. To detect the element, open UI Automator and take a screenshot of the page on which the script is to be run.
The right hand side of the UI Automator application shows all the elements of the page grouped by classes. Click on the desired element in the screenshot. The bottom right hand side window will populate with all the details of the selected element. For example, click on the text window under the text "Enter First Number". The element gets highlighted by a red rectangular colour. You will see the element details populate on the bottom right side table. Since we are using Selenium Web Driver libraries and classes, we can use the 'findelement' method to detect and select elements using various parameters such as 'Class', 'Name' or 'Id'.
To detect and select the element, first create a WebElement object and assign the relevant method to it:
For example:
//locating the First text box
WebElement FirstNo = driver.findElementById("EditText01");
Here, EditText01 is the element id if the first text box. You can click / activate the text box and enter a value in it by using the following commands:
//Clicking the First Text Box and
entering the value
FirstNo.click();
FirstNo.sendKeys("56");
This will activate the First text box and enter the numeric value '56' in it.
You can create a similar script for locating the second text box and entering a value it in:
//locating the second text box
WebElement SecondNo = driver.findElementById("EditText02");
//Clicking the Second text box and
entering the value
SecondNo.click();
SecondNo.sendKeys("65");Similarly, create an object for the multiply button, select it and click on it by using the following code:
//Select and click the multiply
button
WebElement Multiply = driver.findElementByName("Multiply");
Multiply.click(); This should click the 'Multiply' button and display the result of the product of '56' and '65'. After your test is done, use the following command below to exit the script cleanly and end the test.
driver.quit();
Optiona: You can import TestNG assertions and add an assert to make sure that the result is as per specification.
Your complete Appium Script should look something like this:
package <Your Package name>;
import io.appium.java_client.AppiumDriver;
import java.io.File;
import
java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.remote.CapabilityType;
import
org.openqa.selenium.remote.DesiredCapabilities;
import org.junit.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.AppiumDriver;
import
io.appium.java_client.android.AndroidDriver;
import org.testng.annotations.Test;
public class AppiumTest {
AndroidDriver
driver;
public static void main(String[] args) throws Exception {
// TODO Auto-generated
method stub
// creating a class
object
AppiumTest testcases = new AppiumTest();
// call launch app method
testcases.launchCalc();
//Start GlobalMenu
//testcases.launchGMenu();
// call add name method
testcases.addName();
}
public void launchCalc() throws
InterruptedException, MalformedURLException
{
File app=new File("D:\\Radio\\AndroidCalculator-V1_0.apk");
DesiredCapabilities capabilities = new
DesiredCapabilities();
//Name of mobile web browser to automate.
Should be an empty string if automating an app instead.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
//which mobile OS to use: Android, iOS or
FirefoxOS
capabilities.setCapability("platformName", "Android");
//Mobile OS version – in this case 4.4
since my device is running Android 4.4.2
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
//device name – since this is an actual
device name is found using ADB
capabilities.setCapability("deviceName", "92268ee10561a040");
//the absolute local path to the APK
capabilities.setCapability("app", app.getAbsolutePath());
//Java package of the tested Android app
capabilities.setCapability("appPackage", "com.calculator");
// activity name for the Android activity
you want to run from your package. This need to be preceded by a . (example:
.MainActivity)
capabilities.setCapability("appActivity", ".Main");
// constructor to initialize driver object
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(4000);
System.out.println("App
Launched");
//locating the First text box
WebElement FirstNo = driver.findElementById("EditText01");
//Clicking the First Text Box and entering
the value
FirstNo.click();
FirstNo.sendKeys("56");
Thread.sleep(2000);
//locating the second text box
WebElement SecondNo = driver.findElementById("EditText02");
//Clicking the Second text box and entering
the value
SecondNo.click();
SecondNo.sendKeys("65");
//Select and click the multiply button
WebElement Multiply = driver.findElementByName("Multiply");
Multiply.click();
Thread.sleep(10000);
driver.quit();
}
public void addName() throws InterruptedException
{
System.out.println("Test
success");
// write all your tests here
}
}
Since the actual test runs very fast, I have added a few thread.sleep methods to pause the test in between, so that you can get a better idea of how the test runs and to give the app some breathing space (Phew!)
Starting the Appium Server
This step is crucial to running your tests. Without this, your tests will not execute. Start the Appium.exe application on windows. Click on the 'Android' button to set the Android settings.
Click on the 'Choose' button, browse and select the APK to be tested. App package and Main Activity details will be displayed automatically. Enter device name as derived from the command 'adb devices' in CMD.
Click on the general settings icon and check the server number and port number from which the Appium server will be launched.
Now click on the 'Play' button on the top right corner of the Appium window. This will start the Appium server. The Appium server will look like below:
Now run the JAVA test you have in Eclipse and watch the result on your device.
Here we have reached the end of this blog. Hopefully, you have learned something useful from this blog and hope this helps you install and run Appium Scripts smoothly.
There is more to this. In my next blog, I will take up other commands and reporting via Test NG etc. Stay tuned !!
Thank you for taking the trouble to read this blog. Comment and criticism are always welcome.
Enjoy !!
No comments:
Post a Comment