Appium UiWatchers 監聽解決各種非期待彈窗,彈層,彈彈彈等問題


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() {

//廣告提示
UiObject ggPop = new UiObject(new UiSelector().resourceId("com.gift.android:id/close_view"));

//站點切換
UiObject addChgPop = new UiObject(new UiSelector().resourceId("com.gift.android:id/bt_cancel"));

//升級提示
UiObject upPop = new UiObject(new UiSelector().resourceId("com.gift.android:id/close"));

if (upPop.exists()) {
System.out.println("you have a updateApp popup window");
try {
upPop.clickAndWaitForNewWindow();
return true; // triggered
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "Exception", e);
}
}else if (ggPop.exists()) {
System.out.println("you have a popup window");
try {
ggPop.clickAndWaitForNewWindow();
return true; // triggered
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "Exception", e);
}
}else if (addChgPop.exists()) {
System.out.println("you have a change site popup window");
try {
addChgPop.clickAndWaitForNewWindow();
return true; // triggered
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "Exception", e);
}
}
return false; // no trigger
}
});

Log.i(LOG_TAG, "Registered myPopupPopup Watchers");
}
listenForever的方法中去調用一下,這樣我們也監聽Forever了。

寫完編譯成AppiumBootstrap.jar包,放在\Appium\node_modules\appium\build\android_bootstrap\下面。隨着appium服務啟動時,推送到手機中。
跑一下看效果。
app中彈窗中有如下resourceId的,統統被點掉了。
resourceId="com.gift.android:id/close_view";
resourceId="com.gift.android:id/bt_cancel";
resourceId="com.gift.android:id/close";
轉載:
https://testerhome.com/topics/12938



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM