2013-02-27 45 views
8

我試圖使用Android UiAutomator測試一個Android網頁視圖。據我瞭解的documentation,通過WebvView滾動會產生UI穿越事件,而那些應通過getUiDevice().getLastTraversedText().UiAutomator getLastTraversedText()

然而,是可讀的,當我使用getUiDevice().pressDPadDown()通過Web視圖滾動,getUiDevice().getLastTraversedText()不斷返回null。

我錯過了什麼?

如果有人得到這個電話轉給工作,我將非常感謝短代碼示例。

回答

4

不好的消息:我花了好幾個小時試圖弄清楚如何得到這個工作,但是我還沒有得到任何其他的東西來響應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

  • 如果我啓用探索,通過觸摸輔助功能的設置,然後我不能使用uiautomator在adb shell。每次運行uiautomator命令時,我都會收到Killed的響應。我使用Explore-By-Touch來說出顯示的​​文字。雖然我沒能瀏覽到所有的環節探索]啓用觸摸(或使用真氣虛擬d-墊是Android輔助套件的一部分時),它並讀出連結文字的某些環節(例如,它跳過了我用於測試的頁面上的日文字符http://pickalize.info/

  • 如果我運行類似於您的腳本(對於爲此目的創建的簡單WebView應用程序),則可以看到WebView突出顯示各種元素,在我的案例中,來自以下網頁的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.

BTW的文檔以下注釋:我與Android 4.2測試.2在物理設備上。

祝您的調查。我會不時一直在研究這個問題,以幫助我更多地瞭解Ui Automator的適用性和功能。

更新(2013 3月26日):我與「增強網頁輔助功能」設置爲在設備的輔助功能設置菜單中的「允許」再次測試。我仍然可以返回null getLastTraversedText()

回到這裏是我的測試代碼的膽量:

公共類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()"); 
     } 
    } 
} 

}

而這裏的ADB命令我用看此代碼在Android設備上執行時的消息: 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() 
+0

謝謝你的回覆!我也沒有得到更進一步的。 TalkBack能夠讀取WebView的內容,我覺得特別令人費解。 我還會在此處添加其他調查結果。 – Micha 2013-03-20 11:30:07

+0

作爲一個想法,或許我們需要安裝反饋用於增強其從Web瀏覽器中讀取內容的能力的Web腳本? – JulianHarty 2013-03-22 08:13:14

+0

對於記錄,Android 4.3沒有改進。 – Micha 2013-08-23 15:40:54