2016-04-04 106 views

回答

0

看看我們的discussion thread at reddit,有一個解決方案。

此外,我正在開發Android Studio的插件,這將使這一切變得簡單。您可以subscribe for news about it,我會在準備就緒時通知您。

+0

非常感謝。這正是我正在尋找的 –

1

具有以下依賴性創建一個單獨的項目:

compile 'com.android.support.test.espresso:espresso-core:2.2.2' 
compile 'com.android.support.test:runner:0.5' 
compile 'junit:junit:4.12' 

我用這個清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest package="com.test.test"xmlns:android="http://schemas.android.com/apk/res/android"> 

    <instrumentation 
     android:name="android.support.test.runner.AndroidJUnitRunner" 
     android:targetPackage="com.imc.imc" > 
    <instrumentation> 

</manifest> 

我的src/main/JAVA/com.test.test名爲MainActivityTest創建一個新類

public class MainActivityTest { 


    @Rule 
    public ActivityTestRule<?> mActivityRule = newActivityTestRule("com.imc.imc.MainActivity"); 

    @NonNull 
    private ActivityTestRule newActivityTestRule(String className) { 
     return new ActivityTestRule(activityClass(className)); 
    } 

    private static Class<? extends Activity> activityClass(String className) { 
     try { 
      return (Class<? extends Activity>) Class.forName(className); 
     } catch (ClassNotFoundException e) { 
      throw new RuntimeException(e); 
     } 
    } 
private static int getId(String id) { 
    Context targetContext = InstrumentationRegistry.getTargetContext(); 
    String packageName = targetContext.getPackageName(); 
    return targetContext.getResources().getIdentifier(id, "id", packageName); 
} 

    @Test 
    public void mytest() { 
     onView(withId(getId("button"))).perform(click());; 
    } 


} 

,但它不工作!!!!!!

1

您可以使用UI Automator進行跨應用功能性UI測試,例如 ,與使用SettingsAPI創建的GPS對話框進行交互。 瞭解更多關於它here.

enter image description here 點擊這樣的OK按鈕: -

/** UI Automator Google GPS dialog box */ 
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 
UiObject uiObject = mDevice.findObject(new UiSelector().text("OK")); 
try { 
    uiObject.click(); 
}catch (UiObjectNotFoundException e) { 
    e.printStackTrace(); 
}