???????resourceid??λ???
????UISelector?????λ???????????????????????????id???????????????????????????????汾?仯???????????????????????id???????????????????????????Ч?????á?
??????п????????ж???????
if (mDevice.hasObject(By.clazz(TextView.class).res(downloadRes))){
UiObject downloag = mDevice.findObject(new UiSelector().className(TextView.class).resourceId(downloadRes));
downloag.clickAndWaitForNewWindow(mOutTime/2);
if (mDevice.hasObject(By.text(lostApk))){
UiObject confirmBtn = mDevice.findObject(new UiSelector().resourceId(dialogbtnRes));
confirmBtn.click();
}
waitAndInstall();
}
?????????clickAndWaitForNewWindow
???????????????click?????clickAndWaitForNewWindow????????????Ч?????????????′???????????????????????????????????????????????????
????????????????????
?????е?????????????NAF=true?????????????no access flag?????????????????????????????????????????????????????????
?????????https://stuff.mit.edu/afs/sipb/project/android/docs/tools/testing/testing_ui.html
???????Щ????????????????
??????????????????????????????????????????????????????????д?????clickд?????????????д10???д???????д???????д????????????apk??????
protected void waitAndInstall() throws UiObjectNotFoundException{
if(mDevice.hasObject(By.clazz(Button.class).text("?????"))){
UiObject btn = mDevice.findObject(new UiSelector().text("?????").className(Button.class));
btn.click();
waitAndInstall();//????????????
}else if(mDevice.hasObject(By.clazz(Button.class).text("???"))){
UiObject btn = mDevice.findObject(new UiSelector().text("???").className(Button.class));
btn.click();
btn = mDevice.findObject(new UiSelector().text("???").className(TextView.class));
btn.waitForExists(30000);
btn.click();
}else{
mDevice.pressBack();//??????????????????????????????????????root???????????????????á????????????????й???
UiObject btn = mDevice.findObject(new UiSelector().text("?????").className(Button.class));
btn.waitForExists(mOutTime/2);
btn.click();
waitAndInstall();
}
}
???????UIWatcher????????????????????
????????????????????????赱????????????????UiAutomator??????UiWatcher???????????????д???????????????Щ?????
????UiWatcher????ü????????????????????????????????????????????????????塣
public interface UiWatcher {
/**
* Custom handler that is automatically called when the testing framework is unable to
* find a match using the {@link UiSelector}
*
* When the framework is in the process of matching a {@link UiSelector} and it
* is unable to match any widget based on the specified criteria in the selector??
* the framework will perform retries for a predetermined time?? waiting for the display
* to update and show the desired widget. While the framework is in this state?? it will call
* registered watchers' checkForCondition(). This gives the registered watchers a chance
* to take a look at the display and see if there is a recognized condition that can be
* handled and in doing so allowing the current test to continue.
*
* An example usage would be to look for dialogs popped due to other background
* processes requesting user attention and have nothing to do with the application
* currently under test.
*
* @return true to indicate a matched condition or false for nothing was matched
* @since API Level 16
*/
public boolean checkForCondition();
????}
???????????????????????????????watcher?????????selector???????????????Ui?????????watcher????????÷??????£????????????????????????????У??????apk??????Щapp?????????????????????????????????????????д?????watcher??????????????UI?????????????????????????????back????????????????????????????С?
public class MyWatcher implements UiWatcher {
private UiDevice mDevice;
public MyWatcher(UiDevice device){
mDevice = device;
}
@Override
public boolean checkForCondition() {
if(mDevice.hasObject(By.text("????????"))){
mDevice.pressBack();
return true;
}
return false;
}
}
?????????????????setUp??????????watcher????????????????????????watcher???
????myWatcher = new MyWatcher(mDevice);
????mDevice.registerWatcher("testwatcher"??myWatcher);
???????????????watcher????????????????????????UiObject?в??????????????????????????????????????device??runWatcher???????????????watcher??????????г????????????????????????watcher????????????????′β?????????
protected AccessibilityNodeInfo findAccessibilityNodeInfo(long timeout) {
AccessibilityNodeInfo node = null;
long startMills = SystemClock.uptimeMillis();
long currentMills = 0;
while (currentMills <= timeout) {
node = getQueryController().findAccessibilityNodeInfo(mUiSelector);
if (node != null) {
break;
} else {
// does nothing if we're reentering another runWatchers()
mDevice.runWatchers();
}
currentMills = SystemClock.uptimeMillis() - startMills;
if(timeout > 0) {
SystemClock.sleep(WAIT_FOR_SELECTOR_POLL);
}
}
return node;
}
??????????????У??????????device??findObject????hasObject???????????????????watcher??????watcher??????????????????д?????в????????????
????takeScreenShot????
??????????д??????????????????????device or resource is busy????????????RootExplorer????????????????????????????????RE?????????????????????????д?????????
????????????????takescreenshot??????????棬?????????????????ò??????Ч?????????д???????????д?????????
????????????????Щ??????????????????????????????????????????????????????????????????????