app自動化時,各種不期待的彈層彈窗,升級廣告等時有飛出,由於彈窗具有不定時,不定頁面等很多不確定性。有的彈窗很不友好,不×掉,很難進行下一步操作,造成 測試用例失敗。而判斷是否有彈窗,彈層很麻煩。
研究一下 appium和手機通信的原理就不難發現,運行appium時推送手機AppiumBootstrap.jar的中,有這么一段代碼再listenForev/* * The Bootstrap class runs the socket server.
*
*/
public class Bootstrap extends UiAutomatorTestCase {
public void testRunServer() {
Find.params = getParams();
final boolean disableAndroidWatchers = Boolean.parseBoolean(getParams()
.getString("disableAndroidWatchers"));
final boolean acceptSSLCerts = Boolean.parseBoolean(getParams().getString(
"acceptSslCerts"));
SocketServer server;
try {
server = new SocketServer(4724);
server.listenForever(disableAndroidWatchers, acceptSSLCerts);
} catch (final SocketServerException e) {
Logger.error(e.getError());
System.exit(1);
}
}
}
那么我們可以利用這個長監聽,干點點掉彈窗的事兒會不會很爽,彈窗一彈出,就把他點掉,點掉,點掉。
其實在UiWatchers.java中,作者已經寫了很多UI監聽了,貼一小段代碼。
public class UiWatchers {
private static final String LOG_TAG = UiWatchers.class.getSimpleName();
private final List<String> mErrors = new ArrayList<String>();
/**
* We can use the UiDevice registerWatcher to register a small script to be
* executed when the framework is waiting for a control to appear. Waiting may
* be the cause of an unexpected dialog on the screen and it is the time when
* the framework runs the registered watchers. This is a sample watcher
* looking for ANR and crashes. it closes it and moves on. You should create
* your own watchers and handle error logging properly for your type of tests.
*/
public void registerAnrAndCrashWatchers() {
UiDevice.getInstance().registerWatcher("ANR", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector()
.className("com.android.server.am.AppNotRespondingDialog"));
String errorText = null;
if (window.exists()) {
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onAnrDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
在這個類中我們加入自己的彈窗監聽
public void registerMyPopupWatcher() {
UiDevice.getInstance().registerWatcher("myPopup", new UiWatcher() {
@Override
public boolean checkForCondition() 