2015-08-15 48 views
1

我正在嘗試使用UiAutomator爲Android應用程序(我擁有apk但不是源代碼)編寫UI自動「黑匣子」測試。 我在設置階段打開抽屜的應用程序時遇到問題。到目前爲止,我的代碼是UiAutomator採摘應用程序從應用程序抽屜中測試

@Override 
public void setUp() throws Exception { 
    super.setUp(); 
    mDevice = UiDevice.getInstance(getInstrumentation()); 
    mDevice.pressHome(); 
    //Wait for the app drawer icon to show up on the screen 
    mDevice.wait(Until.hasObject(By.desc("Apps")), 3000); 
    //Obtain reference to the app drawer button in order to click it 
    UiObject drawerIcon = mDevice.findObject(new UiSelector().description("Apps")); 
    drawerIcon.clickAndWaitForNewWindow(); 
    //Finding and Clicking on the Sunshine app 
    UiObject drawer = mDevice.findObject(new UiSelector().resourceId("com.sec.android.app.launcher:id/apps_grid")); 
    UiObject appToTest = mDevice.findObject(new UiSelector().description("app-to-test-description")); 
    while (!appToTest.exists()) { 
     drawer.swipeLeft(3); 
    } 
    appToTest.clickAndWaitForNewWindow(); 
} 

當我運行測試,應該打開應用程序(然後運行雜色山雀的測試方法,我還沒有寫。) 相反,它會打開抽屜,它掛起。我想有一種更好的方法來識別抽屜並滾動它直到找到正確的應用程序。 這是錯誤日誌。

運行測試
試運行開始
android.support.test.uiautomator.UiObjectNotFoundException:UiSelector [RESOURCE_ID = com.sec.android.app.launcher:ID/apps_grid] 在android.support.test .uiautomator.UiObject.getVisibleBounds(UiObject.java:891) at android.support.test.uiautomator.UiObject.swipeLeft(UiObject.java:315) at com.crisanti.roberto.uturistautomatedtest.UiAutomatorTest.setUp(UiAutomatorTest.java :29) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.j AVA:176) 在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) 在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1853)

回答

1

我已經算起來如何使它工作。在棒棒糖上,發射器由"com.google.android.googlequicksearchbox:id/apps_customize_pane_content"而不是由"com.sec.android.app.launcher:id/apps_grid"識別。這可能是一個問題,因爲測試依賴於平臺版本(在Kitkat上,發射器有另一種行爲,Marshmellow也會有另一種不同的行爲)。 我所做的另一個修改是在線 drawer.swipeLeft(3);我更改爲 drawer.swipeLeft(5);

總結代碼上棒棒堂與UiAutomator啓動一個應用程序是:

public void setUp() throws Exception { 
     super.setUp(); 
     mDevice = UiDevice.getInstance(getInstrumentation()); 
     mDevice.pressHome(); 
     //Obtain reference to the app drawer button in order to click it 
     UiObject allAppsButton = mDevice.findObject(new UiSelector().description("Apps")); 
     //The operation below expects the click will result a new window. 
     allAppsButton.clickAndWaitForNewWindow(); 
     // Find the application in the app launcher 
     UiObject appViews = mDevice.findObject(new UiSelector().resourceId("com.google.android.googlequicksearchbox:id/apps_customize_pane_content")); 
     UiObject navigationDrawerApp = mDevice.findObject(new UiSelector().text("app-to-test-name")); 
     while (!navigationDrawerApp.exists()){ 
      appViews.swipeLeft(5); 
     } 
     navigationDrawerApp.clickAndWaitForNewWindow(); 
} 
3

如果你真的想從你已經回答了你自己的問題菜單啓動它。但是請記住,在不同的設備中,應用程序抽屜可能會有所不同(三星和其他人通常會在Android上自己做)。

作爲替代你可以

Context context = InstrumentationRegistry.getInstrumentation().getContext(); //gets the context based on the instrumentation 
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageNameOfYourApp); //sets the intent to start your app 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); //clear out any previous task, i.e., make sure it starts on the initial screen 
context.startActivity(intent); //starts the app 

您可以通過使用UiAutomatorViewer(在SDK文件夾/工具/)有包名。

如果你想要的話,你可以等到應用程序真正開始做這件事(我假設你已經有一個UiDevice設備)。

device.wait(Until.hasObject(By.pkg(packageNameOfYourApp)), timeOut); //ofc you need to set timeout 

這適用於任何設備/仿真器配置。

+0

感謝您提到這個pro-tip:「您可以使用UiAutomatorViewer(在sdk-folder/tools /中)的軟件包名稱。」在應用程序包名稱末尾有一個「.debug」,但我使用的是原始包名稱(不帶後綴),因此返回的意圖始終爲空。這個「.debug」後綴可能來自構建類型。無論如何都讓我瘋狂! – Metallica

相關問題