下面列出了android.app.UiAutomation.OnAccessibilityEventListener#com.android.uiautomator.core.UiObjectNotFoundException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testDemo() throws UiObjectNotFoundException {
// Press on the HOME button.
getUiDevice().pressHome();
// Launch the "Google" apps via the All Apps screen.
UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
allAppsButton.clickAndWaitForNewWindow();
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
appsTab.click();
UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
appViews.setAsHorizontalList();
UiObject testApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()),
"Google");
testApp.clickAndWaitForNewWindow();
// Get the google search text box
UiObject searchBox = new UiObject(
new UiSelector().className("com.google.android.search.shared.ui.SimpleSearchText"));
// do Japanese Input!
searchBox.setText(Utf7ImeHelper.e("こんにちは!UiAutomatorで入力しています。"));
}
private Result executeWithStopwatch(Command command, Action action) throws UiObjectNotFoundException {
Stopwatch timer = new Stopwatch().start();
Result result = action.execute(command.getArguments());
timer.stop();
Log.i(TAG, String.format("action '%s' took %d ms to execute on the tablet",
command.getAction(), timer.elapsed(MILLISECONDS)));
return result;
}
@Override
public final Result execute(Map<String, Object> arguments) throws UiObjectNotFoundException {
Log.d(TAG, "Entering " + TAG);
String parentDescription = (String) arguments.get("parent_description");
String childDescription = (String) arguments.get("child_description");
int expectedCount = ((Double) arguments.get("child_count")).intValue();
UiCollection parentElement = new UiCollection(new UiSelector().description(parentDescription));
int actualCount = parentElement.getChildCount(new UiSelector().description(childDescription));
Log.d(TAG, "Actual count was: " + actualCount + " when " + expectedCount + " was expected");
return new Result(isTrue(expectedCount, actualCount), "Actual count was: " + actualCount + " when " + expectedCount + " was expected");
}
private void start_target_app() throws UiObjectNotFoundException {
// 0. Start fromIndex HOME
dev.pressHome();
// 1. Find and click "Apps" button
// TODO: this is ad hoc fix for CM-10.2 nightly snapshot only
UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
// int cnt = 0;
// boolean end = false;
// while (!end) {
// UiObject obj = new UiObject(new UiSelector().packageName("com.cyanogenmod.trebuchet").className("android.widget.TextView").instance(cnt));
// cnt++;
//
// try {
// obj.getBounds();
// } catch (UiObjectNotFoundException exception) {
// end = true;
// }
// }
// Util.log("HOME: " + (cnt - 1) + " TextViews");
//
// if ((cnt - 1) < 5) {
// Util.err("ERR: NOT_AT_HOME");
// System.exit(-1);
// }
//
// // Now there are (cnt-1) TextViews, "Apps" should be the (cnt-4)-th.
// UiObject allAppsButton = new UiObject(new UiSelector().packageName("com.cyanogenmod.trebuchet").className("android.widget.TextView").instance(cnt - 4));
allAppsButton.clickAndWaitForNewWindow();
// 2. Find and click “Apps” tab
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
launcherPackName = appsTab.getPackageName(); // remember the launcher package
appsTab.click();
// 3. Select scrollable "container view"
UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
// Set the swiping mode to horizontal (the default is vertical)
appViews.setAsHorizontalList();
// 4. This API does not work properly in 4.2.2
appViews.scrollTextIntoView(appName);
// 5. Click target app
UiObject targetApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), appName, true);
boolean done = targetApp.clickAndWaitForNewWindow();
// Util.log("clickAndWaitForNewWindow: " + done);
waitForNetworkUpdate();
// AccessibilityEventProcessor.waitForLastEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED, WINDOW_CONTENT_UPDATE_TIMEOUT);
}
@Override
public Result execute(Map<String, Object> arguments) throws UiObjectNotFoundException {
return isUiObjectGone(getUiObject(arguments), arguments) ? Result.OK : Result.FAILURE;
}
@Override
public Result execute(Map<String, Object> arguments) throws UiObjectNotFoundException {
return isUiObjectAvailable(getUiObject(arguments), arguments) ? Result.OK : Result.FAILURE;
}
public void testDemo() throws UiObjectNotFoundException {
// Simulate a short press on the HOME button.
getUiDevice().pressHome();
// We’re now in the home screen. Next, we want to simulate
// a user bringing up the All Apps screen.
// If you use the uiautomatorviewer tool to capture a snapshot
// of the Home screen, notice that the All Apps button’s
// content-description property has the value “Apps”. We can
// use this property to create a UiSelector to find the button.
UiObject allAppsButton = new UiObject(
new UiSelector().description("Apps"));
// Simulate a click to bring up the All Apps screen.
allAppsButton.clickAndWaitForNewWindow();
// In the All Apps screen, the Settings app is located in
// the Apps tab. To simulate the user bringing up the Apps tab,
// we create a UiSelector to find a tab with the text
// label “Apps”.
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
// Simulate a click to enter the Apps tab.
appsTab.click();
// Next, in the apps tabs, we can simulate a user swiping until
// they come to the Settings app icon. Since the container view
// is scrollable, we can use a UiScrollable object.
UiScrollable appViews = new UiScrollable(
new UiSelector().scrollable(true));
// Set the swiping mode to horizontal (the default is vertical)
appViews.setAsHorizontalList();
// Create a UiSelector to find the Settings app and simulate
// a user click to launch the app.
UiObject settingsApp = appViews
.getChildByText(new UiSelector()
.className(android.widget.TextView.class.getName()),
"Settings");
settingsApp.clickAndWaitForNewWindow();
// Validate that the package name is the expected one
UiObject settingsValidation = new UiObject(
new UiSelector().packageName("com.android.settings"));
assertTrue("Unable to detect Settings", settingsValidation.exists());
}
public void testDemo() throws UiObjectNotFoundException {
// Simulate a short press on the HOME button.
getUiDevice().pressHome();
// We’re now in the home screen. Next, we want to simulate
// a user bringing up the All Apps screen.
// If you use the uiautomatorviewer tool to capture a snapshot
// of the Home screen, notice that the All Apps button’s
// content-description property has the value “Apps”. We can
// use this property to create a UiSelector to find the button.
UiObject allAppsButton = new UiObject(
new UiSelector().description("Apps"));
// Simulate a click to bring up the All Apps screen.
allAppsButton.clickAndWaitForNewWindow();
// In the All Apps screen, the Settings app is located in
// the Apps tab. To simulate the user bringing up the Apps tab,
// we create a UiSelector to find a tab with the text
// label “Apps”.
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
// Simulate a click to enter the Apps tab.
appsTab.click();
// Next, in the apps tabs, we can simulate a user swiping until
// they come to the Settings app icon. Since the container view
// is scrollable, we can use a UiScrollable object.
UiScrollable appViews = new UiScrollable(
new UiSelector().scrollable(true));
// Set the swiping mode to horizontal (the default is vertical)
appViews.setAsHorizontalList();
// Create a UiSelector to find the Settings app and simulate
// a user click to launch the app.
UiObject settingsApp = appViews
.getChildByText(new UiSelector()
.className(android.widget.TextView.class.getName()),
"Settings");
settingsApp.clickAndWaitForNewWindow();
// Validate that the package name is the expected one
UiObject settingsValidation = new UiObject(
new UiSelector().packageName("com.android.settings"));
assertTrue("Unable to detect Settings", settingsValidation.exists());
}