Work Flow of Appium
Appium test script written in IDE will interact with the Appium Server which is nothing but the node server with the specified IP address and port number. Node server again passes the request to mobile devices or emulator using the uiautomator as a JSON format. All the UI elements associated with the mobile application can be controlled by using the appium client which is nothing but the derived one of selenium.
The following information is going to explain about the Android application automation using Appium + Junit + Core Java. In the forthcoming posting, I will be sharing the information about the integration of Apache Maven and the continuous integration tool of Jenkins.
How to setup Appium in Windows x86 or x64 PC?
The following pre-requisites make it available at your end before start setting up Appium;
- Latest JDK (Should be installed not copy & paste)
- Android SDK with API 16 and above (Jellybean 4.1 and above)
- .NET Framework 4.5
- Appium.exe for windows
- Latest Eclipse IDE (I’ve tried with both Juno and Mars)
- APK file (Android mobile app)
Once the above pre-requisite is ready, start writing the test scripts in the newly created class file under java project by mapping the external dependencies of JAR files (Selenium & Appium client)
Now, we can see the above pre-requisite information in an elaborate way;
Pre-requisite 1:
After installing the JDK and completed the environment variable setup of both the ‘JAVA_HOME’ and ‘Path’ based on the below screenshot;
Ensure the Java installation in the command prompt based on the below screenshot;
Pre-requisite 2:
After downloading the Android SDK from https://developer.android.com/sdk and installing the Android SDK in your windows PC;
You have to setup the Android Environment variable in the ‘ANDROID_HOME’ and ‘Path’ as like JDK;
You have to add the android sdk installed location path based on the below screeshot;
D:\SDK\Android_Sdk_and_eclipse\android-sdk
You have to add both the platform-tools and tools location in the path variable;
D:\SDK\Android_Sdk_and_eclipse\android-sdk\tools
D:\SDK\Android_Sdk_and_eclipse\android-sdk\platform-tools
Ensure the SDK manager is ready at your end based on the below screenshot; (click the SDK manager.exe available in the Android SDK location)
Pre-requisite 3:
Download the .NET framework from https://www.microsoft.com/en-in/download/details.aspx?id=30653 and install it in your PC and ensure the installation in your installed programs in control panel based on the below screenshot;
Pre-requisite 4:
Download the Appium Installer from https://bitbucket.org/appium/appium.app/downloads/ and I’ve downloaded ‘AppiumForWindows_1_3_7_2.zip’ from the above link. Unzip the installer file and click the ‘appium-installer.exe’ to install Appium in your windows PC;
Ensure Appium is successfully installed in your PC based on the below screenshot;
Pre-requisite 5:
Download the Eclipse IDE from http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/marsr and run the ‘eclipse.exe’ available in your windows PC;
Pre-requisite 6:
I am going to take Yammer Android Mobile Application for this test run. You can download the Yammer apk from http://apk-dl.com/ by giving the google play URL https://play.google.com/store/apps/details?id=com.yammer.v1&hl=en
Install the apk file to emulator using the below command and please note I’ve used Android API 19 (Kitkat 4.4.2) for installing the above apk. (You have to create an emulator using AVD manager.exe in the Android SDK installed location or you can connect the real device in your windows PC)
Even directly you can give the apk location path in Appium server IDE to install the apk by skipping the above section.
All the pre-requisites are ready at our end and now coming to the script preparation for automating an android mobile application;
- Create a java project in Eclipse IDE as AppiumDemo
2. Create a new package as Appiumdemo(your own name) and class as appiumTest(your own name) based on the below screenshot;
3. Now add the script in the appiumTest.java file in Eclipse IDE;
Before entering in to the script preparation, I would like to highlight the importance of ui automator available under the tools folder of android sdk;
It will help us to trace the mobile elements, which is used for triggering the user actions like click, swipe, scroll, enter text etc.
Just go to the tools folder under the installed android sdk location and click the ‘uiautomatorviewer.bat’ file and open the mobile app in the emulator, click the screenshot button in the ui automator IDE based on the below screenshot;
Now you could visualize the mobile element information for your automation need;
Go to Eclipse IDE –> Project –> Properties –> Select ‘Java Build Path’ in left pane and select the ‘Libraries’ tab and select the ‘Add External Jars…’ to map all the dependencies jar as mentioned below;
Selenium supporting jars from http://www.seleniumhq.org/download/ (Java)
Appium client libraries jars from http://appium.io/downloads.html (Java)
Add the selenium-java-2.45.0.jar and all jar files available in the libs folder.
Junit and hamcrest jars from https://github.com/junit-team/junit/wiki/Download-and-Install
Add the below script and run the test script in the Eclipse IDE;
package Appiumdemo;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class appiumTest
{
private WebDriver driver = null;
@Before
public void setUp() throws Exception
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, “”);
capabilities.setCapability(“deviceName”, “Motorola”);
capabilities.setCapability(“platformVersion”, “4.4.2”);
capabilities.setCapability(“platformName”, “Android”);
capabilities.setCapability(“appPackage”, “com.yammer.v1”);
capabilities.setCapability(“appActivity”, “com.yammer.droid.ui.LauncherActivity”);
try
{
driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub “),capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
Thread.sleep(10000);
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
}
@Test
public void Login() throws Exception
{
driver.findElement(By.name(“Log In”)).click();
List<WebElement> textFieldsList = driver.findElements(By.className(“android.widget.EditText”));
textFieldsList.get(0).sendKeys(“gopikannan@xyz.com”); //Give your account username
textFieldsList.get(1).sendKeys(“asdfghjkl”); //Give your account password
driver.findElement(By.id(“com.yammer.v1:id/login_button”)).click();
Thread.sleep(10000);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //Taking screenshot
FileUtils.copyFile(scrFile, new File(“c:\\Temp\\screenshotGK.png”));
}
@After
public void tearDown()
{
driver.quit();
}
}
Make the Appium Server UP with the default configuration; Ensure the server address and port number as mentioned in the test script such as 127.0.0.1 with 4723.
Make the Emulator UP with the Installed APK file;
Go to Eclipse and right click the mouse button in script window by selecting the ‘Run as’ option and again select the JUnit Test’ as mentioned in the below screenshot;
That’s it!!! You script will be executed in the mobile application installed in the emulator. Next posting will be explaining about the integration of Appium with TestNG instead of JUnit.
Very good information… Its very easy to understand about Appium tool..Thanks for sharing….
LikeLike
Looks very good and so much useful….we all need to implement this and start from there to reach our destination….
LikeLike
@Test
I think, loading a test web page url statement is missing.
For ex:
driver.get(testurl);
driver.navigate.to(testurl);
correct me if I am wrong.
LikeLike
testurl is applicable for browser related mobile web application automation.
It’s for mobile application right? so we have to specify the application package name and activity(screen) name as follows;
capabilities.setCapability(“appPackage”, “com.yammer.v1”);
capabilities.setCapability(“appActivity”, “com.yammer.droid.ui.LauncherActivity”);
LikeLike
FileUtils.copyFile(scrFile, new File(“c:\\Temp\\Screen.png”));
not working [syntax error].
LikeLike