Transferred from: https://university.utest.com/how-to-set-up-your-first-android-automation-project-with-appium/
Appium is a open-source tool for automating native, mobile web, and hybrid applications on IOS and Android platforms. Native apps is those written using the IOS or Android SDKs. Mobile Web Apps is web apps accessed using a Mobile browser (Appium supports Safari on IOS and Chrome or the built-in ' Browser ' app on Android). Hybrid Apps has a wrapper around a "webview"-a native control that enables interaction with web content. Projects Likephonegap, make it easy-to-build apps using web technologies that is then bundled into a native wrapper, Crea Ting a hybrid app.
Importantly, Appium is cross-platform. It allows to write tests against multiple platforms (IOS and Android), using the same API. This enables code reuse between IOS and Android test suites.
For this example, I'll use a contact Manager app provided by Appium as a sample app and a Samsung Galaxy S4 with Android 4.4.2. Get contact Manager at GitHub.
Prerequisites
- Eclipse is installed
- Android SDK and APIs for recent versions of Android
- Selenium Webdriver Knowledge
- Java knowledge
SETUP
- Download Selenium Java Zip archive and extract all the files in a folder called Selenium.
- Download Appium for Windows zip archive and extract all files in a folder called Appium.
- Download Appium Java client jar and add it to Apium folder. This contains abstract class Appiumdriver which inherits from Selenium Java client. Iosdriver and Androiddriver both extend appiumdriver and add specific methods for Android and IOS automation. Basically this is a adaptation of Selenium Java Client,but adapted to mobile automation.
- Create A new Java project in Eclipse and add Selenium and Appium Java client Jar files to the project (right-click PROJEC ADD External JARs, Java build path---Properties.
- ADD a package and a new class to the project.
IMPORT REQUIRED LIBRARIES
1. The first step in writing a new test class was to import the needed libraries:
Used to verify if URL is malformed
Import java.net.MalformedURLException;
Library used to create URL for the Appium server
Import Java.net.URL;
Library used to create the path to APK
Import Java.io.File;
Library used to find elements (by ID, class, XPath etc)
Import Org.openqa.selenium.By;
Library for Web Element
Import org.openqa.selenium.WebElement;
Libraries for configuring desired capabilities
Import Org.openqa.selenium.remote.CapabilityType;
Import org.openqa.selenium.remote.DesiredCapabilities;
Library for test methods
Import org.junit.*;
Library for Appium drivers
Import Io.appium.java_client. Appiumdriver;
Import Io.appium.java_client.android.AndroidDriver;
CREATING the PATH to APK FILE
Since the APK is stored in the computer and was not already installed on the device we need to create a file object which r Epresents the actual apk file on the disk. I placed the folder Contactmanager which contains the APK file inside the Eclipse project.
File Classpathroot = new file (System.getproperty ("User.dir")); Path to Eclipse project
File Appdir = new file (classpathroot, "/contactmanager"); Path to <project folder>/contact Manager
File app = new file (appdir, "contactmanager.apk"); Path to <project folder>/contact manager/contactmanager.apk
Desired capabilities
To is able to test the app on a actual device desired capabilities need to BES set. Desired capabilities is a set of keys and values sent to the Appium server to tell the server what kind of automation ses Sion we ' re interested in starting up. There is also capabilities used to modify the behaviour of the server during automation.
Desiredcapabilities capabilities = new Desiredcapabilities ();
Name of mobile web browser to automate. Should be a 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 4.4 Since my device is running Android 4.4.2
Capabilities.setcapability (Capabilitytype.version, "4.4");
Device Name–since This is a actual device name is found using ADB
Capabilities.setcapability ("DeviceName", "111bd508″");
The absolute local path to the APK
Capabilities.setcapability ("App", App.getabsolutepath ());
Java package of the tested Android app
Capabilities.setcapability ("Apppackage", "Com.example.android.contactmanager");
Activity name for the Android activity want to run from the your package. This need to is preceded by a. (Example:. mainactivity)
Capabilities.setcapability ("Appactivity", ". Contactmanager ");
constructor to initialize driver object
Driver = new Androiddriver (New URL ("Http://127.0.0.1:4723/wd/hub"), capabilities);
How to FIND devicename
Since We is trying to run the test on a actual device we need to use ADB to find the device name. This was required when creating desired capabilities to specify on what device you want your tests to be run. You can use the this course to the "ADB devices" to find your device ID. Once You run ADB devices command, the only thing, left is to copy the device ID in the code.
How to FIND Apppackage and appactivity for APK FILE
- Download and install SDK.
- Inside SDK Manager download Tools (Android SDK Build-tools) and APIs for recent versions of Android.
- Open SDK folder and go to Build-tools folder.
- Open any folder (example:21.1.2).
- Open a command Prompt window in this folder (shift+ Right click–open command window).
- Run command "AAPT list-a <path_to_apk_we_need_to_test> >manifest.txt" (This command would write the app manifest In Manifest.txt file inside the same folder).
- Open the Manifest.txt txt file.
- At the beginning of the file there should is details about the package including name (Example:com.example.android.conta Ctmanager).
- At the end of the file there should is details about activity including name (Example:contactmanager).
How to RUN APPIUM SERVER
- Go to the Appium folder downloaded earlier and run Appium.exe.
- Click on General Settings button (second button) and verify URL for you defined in Eclipse matches the server address and port From the Appium app.
- Click on Launch Appium Node Server.
How to FIND ELEMENTS in A NATIVE APP
- Go to SDK folder and open the Tools folder.
- Open Uiautomatorviewer.
- On the actual device, open the app to the page want to automate.
- In the UI Automator Viewer, click on Device screenshot (second button).
- Click any element on the page and look in the Node Detail window (there you'll find details about the selected element: ID, class, name, etc.)
- Use the info found (ID, Class) in Eclipse to click Buttons, fill input fields.
SELENIUM CODE
Since Appium Server is running and all the settings for testing on the actual phone be set we are ready to write test met Hods:
@Test
public void Addcontact () throws exception{
Locate ADD Contact button and click it
Webelement Addcontactbutton = driver.findelement (By.name ("Add contact");
Addcontactbutton.click ();
Locate input fields and type name and email to a new contact and save it
list<webelement> textfieldslist = Driver.findelementsbyclassname ("Android.widget.EditText");
Textfieldslist.get (0). SendKeys ("Some Name");
Textfieldslist.get (2). SendKeys ("[email protected]");
Driver.findelementbyname ("Save"). Click ();
Insert assertions here
}
Since This was the first basic test to see how and if the automation is working there be no assertions in the code. You can modify the code and add assertions to see if contact is added successfully. Learn more on assertions in this utest University course.
The entire code:
Package tests;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.io.File;
Import Org.openqa.selenium.By;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.remote.CapabilityType;
Import org.openqa.selenium.remote.DesiredCapabilities;
Import org.junit.*;
Import Io.appium.java_client. Appiumdriver;
Import Io.appium.java_client.android.AndroidDriver;
public class Testone {
Private Androiddriver driver;
@Before
public void SetUp () throws malformedurlexception{
File Classpathroot = new file (System.getproperty ("User.dir"));
File Appdir = new file (classpathroot, "/contactmanager");
File app = new file (appdir, "contactmanager.apk");
desiredcapabilities capabilities = new Desiredcapabilities ();
capabilities.setcapability (Capabilitytype.browser_name, ""); Name of mobile web browser to automate. Should be a empty string if automating an app instead.
capabilities.setcapability ("PlatformName", "Android");
capabilities.setcapability (capabilitytype.version, "4.4");
capabilities.setcapability ("DeviceName", "111bd508″");
capabilities.setcapability ("app", App.getabsolutepath ());
capabilities.setcapability ("Apppackage", "Com.example.android.contactmanager");
capabilities.setcapability ("Appactivity", ". Contactmanager ");
Driver = new Androiddriver (New URL ("Http://127.0.0.1:4723/wd/hub"), capabilities);
}
@Test
Public void Addcontact () throws Exception {
webelement Addcontactbutton = driver.findelement (By.name ("Add contact");
Addcontactbutton.click ();
list<webelement> textfieldslist = Driver.findelementsbyclassname ("Android.widget.EditText");
textfieldslist.get (0). SendKeys ("Some Name");
Textfieldslist.get (2). SendKeys ("[email protected]");
driver.findelementbyname ("Save"). Click ();
//insert assertions here
}
Appium First Android Automation project