Espresso和UIAutomator - 完美的结合

本篇文章翻译自Espresso & UIAutomator - the perfect tandem

Espresso是个功能强大、执行速度很快的Android自动化测试框架,但是它有一个重要的局限-你只可以在被测App的Context中操作。

意味着下列App自动化测试需求无法实现:

  • 应用的推送消息
  • 同步联系人
  • 从另一个应用程序进入被测App

因为你要处理移动设备的其他应用程序,比如通知栏、联系人等。

事实上,UIAutomator 2.0发布后可以实现上述功能。如Android Developers blog post所说“最重要的是,UIAutomator现在已经基于Android Instrumentation...”,因此,使用Instrumentation test runner既可以运行UIAutomator,也可以运行Espresso。

另外,我们可以将UIAutomator和Espresso测试结合起来,可以更加得心应手的处理被测应用和电话之间的交互。

下面的例子,我将解释如何使用这种方法来自动化测试App的联系人同步功能。

import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiSelector;

@RunWith(AndroidJUnit4.class)
public class TestContactsSync {

    @Rule
    public ActivityTestRule mActivityRule = new ActivityTestRule(LoginActivity.class);

    UiDevice mDevice;
    String PEOPLE_APP = "People";
    String MY_APP = "XING";
    String userContactName = "Android Tester";

    @Before
    public void setUp() throws Exception{
        super.setUp();
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    }
    @Test
    public void testContactsSync() throws Exception {
    
        // Espresso code: login the user, navigate to contact sync and enable clicking on toggle
        logInUser();
        onView(withText(R.string.sync_button)).perform(click());
        onView(withId(R.id.contacts_sync_enable_toggle)).perform(click());

        // UIAutomator code: navigate to People app 
        mDevice.pressHome();
        UiObject conutactsApp = mDevice.findObject(new UiSelector().text(PEOPLE_APP));
        conutactsApp.clickAndWaitForNewWindow();

        // UIAutomator code: check that contact is present in it after sync was triggered    
        UiObject contactName = mDevice.findObject(new UiSelector().text(userContactName));
        assertTrue(contactName.exists());

        // UIAutomator code: navigate back to my app under testm
        Device.pressHome();
        UiObject myApp = mDevice.findObject(new UiSelector().text(MY_APP));
        myApp.clickAndWaitForNewWindow();

        // Espresso code: turn off contact sync and logout
        onView(withId(R.id.contacts_sync_enable_toggle)).perform(click());
        onView(withId(R.id.logout)).perform(click());
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}

是不是很完美?UIAutomator需要Android 4.3(API Level 18)及更高版本。稍微修改下build.gradle文件即可-添加productFlavors和声明uiautomator依赖。

productFlavors {
    // The actual application flavor 
    production {
        minSdkVersion 14
    }
    // Test application flavor for uiautomatior tests
    test {
        minSdkVersion 18
    }
}

dependencies {
    // Instrumentation tests
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.1'
    androidTestCompile 'com.android.support.test:rules:0.2'
    // Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
}

祝你好运:)

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,221评论 25 709
  • 作者:Ringoyan,腾讯测试开发工程师。先后为植物大战僵尸Online,糖果传奇等游戏担任测试经理,其负责的“...
    饭盒阅读 7,729评论 2 41
  • 一:移动端自动化测试框架对比 概述 1、Monkey是Android SDK自带的测试工具,在测试过程中会向系统发...
    PeytonWu阅读 5,888评论 0 3
  • 语言的起源可以追溯到人类的起源,每一个民族都有自己的语言,无论你能听懂与否,其共通的就是说话者的情感、语气、表情,...
    Miss墨菲阅读 3,025评论 1 4
  • 時間線後面 十二三歲的頭哥登上破舊的校車 坐到我身邊 說肥仔 然後木棉花開 蘿蔔田跟我說一輩子第一次有人跟我說的話...
    康飄釀阅读 1,521评论 0 2