1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-04-13 14:20:50 +01:00

Add more convenience methods to BaseUiAutomation.

These additions make it easier to get UiObjects and perform UiDevice based gestures when writing UiAutomation workloads
This commit is contained in:
James Hartley 2016-04-26 17:54:49 +01:00
parent 73c8ca86a3
commit 16ba611789

View File

@ -20,6 +20,7 @@ import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
@ -31,8 +32,9 @@ import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class BaseUiAutomation extends UiAutomatorTestCase {
public class BaseUiAutomation extends UiAutomatorTestCase {
public long waitTimeout = TimeUnit.SECONDS.toMillis(4);
public void sleep(int second) {
super.sleep(second * 1000);
@ -40,7 +42,7 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
public boolean takeScreenshot(String name) {
Bundle params = getParams();
String png_dir = params.getString("workdir");
String png_dir = params.getString("workdir");
try {
return getUiDevice().takeScreenshot(new File(png_dir, name + ".png"));
@ -87,7 +89,7 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
long currentTime = System.currentTimeMillis();
boolean found = false;
while ((currentTime - startTime) < timeout){
while ((currentTime - startTime) < timeout){
sleep(2); // poll every two seconds
while((line=reader.readLine())!=null) {
@ -109,5 +111,94 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
throw new TimeoutException("Timed out waiting for Logcat text \"%s\"".format(searchText));
}
}
}
public UiObject getUiObjectByResourceId(String resourceId, String className) throws Exception {
UiObject object = new UiObject(new UiSelector().resourceId(resourceId)
.className(className));
if (!object.waitForExists(waitTimeout)) {
throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"",
resourceId, className));
};
return object;
}
public UiObject getUiObjectByDescription(String description, String className) throws Exception {
UiObject object = new UiObject(new UiSelector().descriptionContains(description)
.className(className));
if (!object.waitForExists(waitTimeout)) {
throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"",
description, className));
};
return object;
}
public UiObject getUiObjectByText(String text, String className) throws Exception {
UiObject object = new UiObject(new UiSelector().textContains(text)
.className(className));
if (!object.waitForExists(waitTimeout)) {
throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"",
text, className));
};
return object;
}
public int getDisplayHeight () {
return getUiDevice().getInstance().getDisplayHeight();
}
public int getDisplayWidth () {
return getUiDevice().getInstance().getDisplayWidth();
}
public int getDisplayCentreWidth () {
return getDisplayWidth() / 2;
}
public int getDisplayCentreHeight () {
return getDisplayHeight() / 2;
}
public void tapDisplayCentre () {
tapDisplay(getDisplayCentreWidth(), getDisplayCentreHeight());
}
public void tapDisplay (int x, int y) {
getUiDevice().getInstance().click(x, y);
}
public void uiDeviceSwipeUp (int steps) {
getUiDevice().getInstance().swipe(
getDisplayCentreWidth(),
(getDisplayCentreHeight() / 2),
getDisplayCentreWidth(),
(getDisplayCentreHeight() + (getDisplayCentreHeight() / 2)),
steps);
}
public void uiDeviceSwipeDown (int steps) {
getUiDevice().getInstance().swipe(
getDisplayCentreWidth(),
(getDisplayCentreHeight() + (getDisplayCentreHeight() / 2)),
getDisplayCentreWidth(),
(getDisplayCentreHeight() / 2),
steps);
}
public void uiDeviceSwipeLeft (int steps) {
getUiDevice().getInstance().swipe(
(getDisplayCentreWidth() + (getDisplayCentreWidth() / 2)),
getDisplayCentreHeight(),
(getDisplayCentreWidth() / 2),
getDisplayCentreHeight(),
steps);
}
public void uiDeviceSwipeRight (int steps) {
getUiDevice().getInstance().swipe(
(getDisplayCentreWidth() / 2),
getDisplayCentreHeight(),
(getDisplayCentreWidth() + (getDisplayCentreWidth() / 2)),
getDisplayCentreHeight(),
steps);
}
}