This will guide you through the steps to write your first uiautomator test using gradle as it build system.
What is gradle?
“Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build.”
For more visit gradle page.
What is uiautomator?
“The uiautomator testing framework lets you test your user interface (UI) efficiently by creating automated functional UI testcases that can be run against your app on one or more devices.”
For more visit uiautomator page.
Java source code
Create a directory named bluetooth:
$ mkdir bluetooth
Create a java project layout:
$ cd bluetooth
$ mkdir -p src/main/java/com/github/wiliamsouza/bluetooth
Create a java file BluetoothTest.java with the following content:
package com.github.wiliamsouza.bluetooth;
import java.io.IOException;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class BluetoothTest extends UiAutomatorTestCase {
public void setUp() throws UiObjectNotFoundException, IOException {
getUiDevice().pressHome();
String packageName = "com.android.settings";
String component = packageName + "/.Settings";
String action = "am start -a android.intent.action.MAIN -n ";
// start settings application
Runtime.getRuntime().exec(action + component);
UiObject settingsUi = new UiObject(new UiSelector().packageName(packageName));
assertTrue("Application settings not started", settingsUi.waitForExists(5000));
}
public void testBluetooth() throws UiObjectNotFoundException {
UiScrollable scroll = new UiScrollable(new UiSelector().scrollable(true));
UiObject layout = scroll.getChildByText(new UiSelector().className(android.widget.LinearLayout.class.getName()),"Bluetooth", true);
UiObject switchBluetooth = layout.getChild(new UiSelector().className(android.widget.Switch.class.getName()));
assertTrue("Unable to find bluetooth switch object", switchBluetooth.exists());
switchTo(switchBluetooth, "Bluetooth");
}
private void switchTo(UiObject switchObject, String name) throws UiObjectNotFoundException {
// Before start test ensure switch is off
if (switchObject.isChecked()) {
switchObject.click();
sleep(3000);
}
switchObject.click();
sleep(3000);
assertTrue("Unable to turn " + name + " on", switchObject.isChecked());
switchObject.click();
sleep(3000);
assertFalse("Unable to turn " + name + " off", switchObject