【Android】UI测试:UIAutomator & Espresso

UIAutomator

允许以全局的视角操作设备和OS,可用于跨进程跨应用做黑盒测试。
支持 API 18 或以上,不支持 WebView。

提供5个基本工具类,用于操作测试流程:

com.android.uiautomator.core.UiCollection;
com.android.uiautomator.core.UiDevice;
com.android.uiautomator.core.UiObject;
com.android.uiautomator.core.UiScrollable;
com.android.uiautomator.core.UiSelector

Example代码:

// Public void for the operation
public void testSignInAndTweet() throws Exception {
    // Starting application:
    getUiDevice().wakeUp();
    // Press Home button to ensure we're on homescreen
    getUiDevice().pressHome(); 
    // Select 'Apps' and click button
    new UiObject(new UiSelector().description("Apps")).click(); 
    // Select 'Twitter' and click
    new UiObject(new UiSelector().text("Twitter")).click(); 
    // Locate and select 'Sign in'
    UiSelector signIn = new UiSelector().text("Sign In"); 
    // If button is available, click
    UiObject signInButton = new UiObject(signIn);
    if (signInButton.exists()) {
    signInButton.click(); 
    new UiObject(new UiSelector().className("android.widget.Button").
    text("Sign In").instance(0)).click(); 
    // Wait Sign in progress window
    getUiDevice().waitForWindowUpdate(null, 2000);
    new UiObject(new UiSelector().description("New tweet")).click(); 
    // Typing text for a tweet
    new UiObject(new UiSelector().className("android.widget.LinearLayout").instance(8)).
    setText("Awesome #Testdroid!"); 
    // Tweeting!
    new UiObject(new UiSelector().text("Tweet")).click();

Espresso

基于 Android instrumentation framework 构建的快速UI测试框架,拥有简单易用的API及与UI线程同步的响应性能。用于 App 单应用测试,可通过动作录制方式自动生成测试代码。
支持 API 8及以上,不支持 WebView。

Example 代码:

public void testEspresso() {
    // Check if view with the text 'Hello.' is shown
    onView(withText("Hello.")).check(matches(isDisplayed()));
    // R class ID identifier for 'Sign in' - and click it
    onView(withId(getInstrumentation().getTargetContext().getResources()
    .getIdentifier("com.twitter.android:id/sign_in", null, null))).perform(click());
    // R class ID identifier for entering username
    onView(withId(getInstrumentation().getTargetContext().getResources()
    .getIdentifier("com.twitter.android:id/login_username", null, null))).perform((typeText("username")));
    // R class ID identifier for entering password
    onView(withId(getInstrumentation().getTargetContext().getResources()
    .getIdentifier("com.twitter.android:id/login_password", null, null))).perform((typeText("password")));
    // R class ID identifier for clicking log in
    onView(withId(getInstrumentation().getTargetContext().getResources()
    .getIdentifier("com.twitter.android:id/login_login", null, null))).perform(click());
    // Activate the text field to compose a tweet
    onView(withId(getInstrumentation().getTargetContext().getResources()
    .getIdentifier("com.twitter.android:id/menu_compose_tweet", null, null))).perform(click());
    // Type the tweet
    onView(withId(getInstrumentation().getTargetContext().getResources()
    .getIdentifier("com.twitter.android:id/edit", null, null))).perform((typeText(”#Testdroid")));
    // Tweeting!
    onView(withId(getInstrumentation().getTargetContext().getResources()
    .getIdentifier("com.twitter.android:id/composer_post", null, null))).perform(click());
}

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,500评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,641评论 2 45
  • 为什么要用Markdown Markdown十分的简洁,跨平台性更是不用说,一个VIM或记事本就可以完成的编写,我...
    BigJelly阅读 375评论 0 1
  • 第一次见到他的时候,我还在感叹着家里的她真是风情万种,引来无数追求者。现在看来他们仿佛是一对喜欢吵架的恋人未满,天...
    小九jdjd阅读 557评论 0 0
  • 发现效率最高的时候是一个人的时候 某种意义上social会严重影响我的效率 但是不能放弃social 因为存在的意...
    littleairplane阅读 218评论 0 1