[英]UiAutomator getLastTraversedText()
我試圖使用Android UiAutomator測試Android Webview。 據我了解的文檔 ,滾動瀏覽WebvView會生成UI遍歷事件,這些事件應該通過getUiDevice().getLastTraversedText().
可讀getUiDevice().getLastTraversedText().
但是,當我使用getUiDevice().pressDPadDown()
滾動瀏覽Web視圖時, getUiDevice().getLastTraversedText()
始終返回null。
我錯過了什么?
如果有人接到了這個電話,我將不勝感激一個簡短的代碼示例。
壞消息:我已經花了幾個小時試圖弄清楚如何getUiDevice().getLastTraversedText()
工作,但是我還沒有得到null以外的任何東西來響應對getUiDevice().getLastTraversedText()
的調用。
僅供參考,以下是我嘗試並發現的內容:
adb shell
運行uiautomator events
應轉儲所有可訪問性事件。 它肯定會報告各種事件,但是當我手動滾動瀏覽WebView的內容時,它幾乎是靜默的。 如果滾動瀏覽WebView中的內容(例如,在向上或向下導航超出屏幕顯示的內容之后),則會收到以下類型的事件報告: 03-10 19:44:47.436 EventType: TYPE_VIEW_SCROLLED; EventTime: 911700; PackageName: com.example.simplewebview; MovementGranularity: 0; Action: 0 [ ClassName: android.webkit.WebView; Text: []; ContentDescription: null; ItemCount: -1; CurrentItemIndex: -1; IsEnabled: true; IsPassword: false; IsChecked: false; IsFullScreen: false; Scrollable: true; BeforeText: null; FromIndex: -1; ToIndex: -1; ScrollX: 0; ScrollY: 270; MaxScrollX: 0; MaxScrollY: 544; AddedCount: -1; RemovedCount: -1; ParcelableData: null ]; recordCount: 0
如果啟用了“按觸摸瀏覽”可訪問性設置,則無法在adb shell
使用uiautomator。 每次運行uiautomator命令時,都會得到Killed
的響應。 我當時正在使用“按觸摸瀏覽”說出顯示的文字。 雖然我無法在啟用“通過觸摸瀏覽”的情況下導航到所有鏈接(或使用Android Accessibility Suite中的令人討厭的Virtual D-Pad時),但確實為某些鏈接讀出了LinkText(例如,它跳過了我用於測試http://pickalize.info/的頁面上的日語字符)
如果我運行與您的腳本類似的腳本(針對我為此目的創建的簡單WebView應用程序),那么我可以看到WebView中的UI突出顯示了各種元素,在這種情況下,出於以下原因,我正在調查以下網頁的Web鏈接:時間http://pickalize.info/當前選定鏈接的背景變為淺藍色,但是對getUiDevice().getLastTraversedText()
的調用未返回任何文本
我最好的猜測是,我們要么試圖不適當地使用該方法。 也許我們應該請求並解析WebView的DOM(天知道怎么做),但是請參閱getLastTraversedText()文檔中的以下注釋
When the view control used can return a reference to is Document Object Model, it is recommended then to use the view's DOM instead.
順便說一句:我正在使用Android 4.2.2在物理設備上進行測試。
祝您調查順利。 我將不時地研究問題,以幫助我更多地了解Ui Automator的適用性和功能。
更新(2013年3月26日):我再次進行了測試,將設備的“輔助功能設置”菜單中的“增強網絡輔助功能”設置為“允許”。 我仍然得到由getLastTraversedText()
返回的null
這是我的測試代碼的膽量:
公共類WebViewNavigator擴展了UiAutomatorTestCase {
public void testNavigationDownGeneratesEventText() throws UiObjectNotFoundException {
getUiDevice().pressHome();
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();
String nameOfAppToLaunch = "SimpleWebView";
UiAutomatorHelpers.launchAppCalled(nameOfAppToLaunch);
UiObject simpleWebViewValidation = new UiObject(new UiSelector().packageName("com.example.simplewebview"));
assertTrue("The package name seems incorrect for the app we want to launch", simpleWebViewValidation.exists());
simpleWebViewValidation.clickAndWaitForNewWindow();
// Ok we should have started the app now... On to the test
Log.i("WebViewNavigatorTest", "About to start navigating down the contents of the webview");
int closeToInfinity = 10;
for (int i = 0; i < closeToInfinity; i++) {
boolean goneDown = getUiDevice().pressDPadDown();
if (!goneDown) {
break;
}
String lastTraversedText = getUiDevice().getLastTraversedText();
if (lastTraversedText != null) {
Log.i("WebViewNavigatorTest", lastTraversedText);
} else {
Log.w("WebViewNavigatorTest", "(null) returned for getLastTraversedText()");
}
}
}
}
這是在Android設備上執行此代碼時用來查看消息的adb命令: adb logcat WebViewNavigatorTest:I *:S
接下來的輸出:
I/WebViewNavigatorTest(29358): About to start navigating down the contents of the webview
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.