As the automation testing is more and more present in the development of web and desktop applications it is also taking more place in the mobile world. There are various tools and frameworks helping the testers out the job.
First of all it is necessary to get the Android SDK and those API levels which are running on the target devices. Check this link for Android SDK: http://developer.android.com/sdk/index.html. The recommended IDE for android development is the Android Studio, however the example in this guide is written in Eclipse. To add the android plugin to Eclipse follow this guide: http://developer.android.com/sdk/installing/installing-adt.html#Download.

It is possible to run the test cases both on real and virtual devices. In the case if the target device is a virtual one there are two options. The first and more traditional way is using the android emulator which comes with the SDK or the other one is using Genymotion.

Genymotion is an emulator offers the same functionality as the traditional one and even more, beside that the boot of these devices is much faster. It can definitely improve the performance of the tests. You can download here: https://www.genymotion.com/#!/store. It is recommend to add the Genymotion plugin to Eclipse to make the job easier. Find the details on this page: https://www.genymotion.com/#!/download#genymotion-eclipse.

Genymotion has its own repository with a tons of predefined android devices and any of them could be installed easily. After opening the Genymotion application click to Add button and select the appropriate VM then complete a next few steps after the click on the Next button. The following example will use a
“Google Galaxy Nexus – 4.2.2 – API 17 – 720×1280” virtual device.

There is a need for a framework allowing the communication between the code and the mobile devices like Appium does. Appium is a test automation framework for native, hybrid and mobile web applications. It allows to write the tests with Webdriver with little modifications but requires the NodeJS framework to fulfill its job. These frameworks can be used in various language such as Java, Python, JavaScript, Ruby etc. but the following example was written in Java.

As mentioned before the Webdriver is used in these kind of tests so the Selenium libraries are necessary for the job. Beside that the Appium client package is also needed. These jars are available here:
Selenium: http://www.seleniumhq.org/download/
Appium: http://appium.io/downloads.html

This is the sample code:

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

public class AndroidSettingsTests {

	private AndroidDriver<WebElement> driver = null;
	private final String settingsAppPackageName = "com.android.settings";
	private final String settingsAppActivityName = "com.android.settings.Settings";

	@Before
	public void setUp() throws MalformedURLException {
		DesiredCapabilities capabilities = DesiredCapabilities.android();
		capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
		capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "192.168.56.101:5555");
		capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, settingsAppPackageName);
		capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, settingsAppActivityName);
		driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	}

	@After
	public void tearDown() {
		driver.quit();
	}

	@Test
	public void testApp() throws InterruptedException, MalformedURLException {
		//Switch OFF WIFI
		driver.findElement(By.xpath("//*[@class = 'android.widget.Switch']")).click();
		//Validate results
		WebElement toggle = driver.findElement(By.xpath("//*[@class = 'android.widget.Switch']"));
		Assert.assertEquals("Incorrect state", "OFF", toggle.getText());
	}
}

This test case describes how to start the Setting app on the virtual device, turn off the WiFi and also verifies the result of the action.

The first step is to create a Java project in Eclipse and copy this code into a class file with name “AndroidSettingsTests”. After that configure the build path including the appropriate jar files. The next step is to start the android virtual device through the Genymotion plugin. Click to the Genymotion icon on the toolbar then select and start the proper device. Meanwhile the VM is booting start the Appium application and lunch the Appium node server. When everything is ready, run the class in Eclipse as JUnit test.

The code will communicate with the node server on the localhost through port 4723 as it is defined in the constructor of the AndroidDriver. Beside that a DesiredCapabilities object is also instantiated and passed like a parameter of the constructor. It contains further details about the connection. The DEVICE_NAME capability set the path to the target device, in our case this is the Google Galaxy Nexus. Alternatively the actual name of the device is also valid as identifier so the IP can be changed to the name. The settingsAppPackageName and settingsAppActivityName define the application and the activity which will be started in the test run. Most of the applications could be started with an identification like that. It is also possible to test standalone applications with an .apk file. After all the necessary properties are set up in the before method the actual test can be started. The selected app will be lunched on the device and the search for the elements begins. The method is the same as in any other Webdriver tests. Locate the element with a find method and do all the tasks. To search for locators use the Dump View Hierarchy for UI Automator tool in DDMS perspective. The code turns off the WiFi with a switch then verifies the status of it by getting the textual content from the same element. After the test run the application will be closed by calling the quit in the after method.

These steps could be useful in the beginning of the job and can help to avoid the initial difficulties in the early stages.

Follow us and you will able to read about IOS automation soon.

Posted By Árpád Farkas

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>