Getting started automation on Android with Appium

Appium is an open source, cross-platform test automation tool for native, hybrid and mobile web apps, tested on simulators (iOS, FirefoxOS), emulators (Android), and real devices (iOS, Android, FirefoxOS).
Appium is cross-platform. It allows you to write tests against multiple platforms (iOS and Android), using the same API. Appium enables iOS and Android automation using Selenium WebDriver.

Requirments to start

– Java IDE (Here we use Eclipse)
– Java JDK
– Maven Plugin for Eclipse
– Selenium WebDriver Dependencies
– Appium Java-Client Dependency
– Android SDK
– JUnit
– Appium Server

How start?

Create a new java Maven project in Eclipse
In the pom.xml add dependency:

<dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>2.2.0</version>
</dependency>

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
</dependency>
<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.45.0</version>
</dependency>

Above is an example of a test script.

In setup(), the funciont which is executed before run the test, the driver is configured.
Also, it is indicated where is the apk to test. So you should add the app.apk n src/test/resources, in this case.

Desire capabilities are setting up also.

Desired Capabilities are a set of keys and values sent to the Appium server to tell the server what kind of automation session we’re interested in starting up. There are also capabilities used to modify the behaviour of the server during automation. They are explained below.

Then the test.

In tearDown(), the funcion which is executed after the test is run, it is possible to remove the app, leave the enviroment as we find it…

@Before
public void setUp() throws Exception {
   //path to eclipse project
   File rootPath = new File(System.getProperty("user.dir"));
   //path to folder where the apk is going to be
   File appDir = new File(rootPath,"src/test/resources");
   //name of the apk
   File app = new File(appDir,"ownCloud.apk");

   DesiredCapabilities capabilities = new DesiredCapabilities();
   //which mobile OS to use: Android, iOS or FirefoxOS
   capabilities.setCapability("platformName", "Android");
   capabilities.setCapability("deviceName", "test");
   //the absolute local path to the APK
   capabilities.setCapability("app", app.getAbsolutePath());
   //Java package of the tested Android app
   capabilities.setCapability("appPackage", "com.owncloud.android");
   // 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",".activity_name");
   capabilities.setCapability("appWaitActivity",".activity_name");

   // constructor to initialize driver object
   driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
   driver.manage().timeouts().implicitlyWait(waitingTime,
   TimeUnit.SECONDS);
   wait = new WebDriverWait(driver, waitingTime, 50);
}

@Test
public void test1LoginPortrait () throws Exception {
     //here goes the test
}

@After
public void tearDown() throws Exception {
  driver.removeApp("com.owncloud.android");
  driver.quit();
}

If you want to use an example, try this:

git clone https://github.com/appium/tutorial.git

Import a maven project and you can see how it is:
http://appium.io/slate/en/tutorial/android.html?java#writing-your-first-…

How to Locate the elements?

How do you indicate the test to click in an specific element?
For this it is necessary to select the elements by name, by id, by description…

How do you know the value of this properties?
There are several ways of doing it, using appium or uiatomatoviewer, for instance.
Here we are going to explain it using uiatomatoviewer.

Go to sdk folder and access to tool folder.
Then, open uiatomatoviewer.
Once it is done, plugin a device or lauch a simulator.
In the device, open the app you want to broadcast.
In the uiatomatoviewer, click on Device Screenshot
Then, all the elements of the app are shown. It is possible to see their id, name, text, description…
Google recomend to use the content-description to look for the elements in the code.

How to launch test cases?

Go to the Appium folder you downloaded earlier and run Appium.exe. Then select Android and launch it.
It is also possible to do it by command line:

appium &

Now we can launch the test.

In the eclipse, select to run the test as junit.
If it also possible to do it by command line:

mvn clean test

if you only want to launch some files, it is possible to do it:

mvn clean -Dtest=RunInProgressTest test

Things to take into accout before starting automation:

  • leaving the app in the initial state, which assures that each test can be run independently.
  • validating that the tests finish as expected.

Remind to include assertions in your code to check that everything works as expected
The most important assertions for Selenium are assertTrue and assertEquals.

1 thought on “Getting started automation on Android with Appium”

  1. hi
    I’ve a script using appium, I start a mobile browser chrome. What if I want to take a screenshot.
    is there a way to take the entire screenshot of the page , and not just the visible part?

    Thanks

Leave a Comment

¿Necesitas una estimación?

Calcula ahora