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'
}

祝你好运:)

    原文作者:TestDevTalk
    原文地址: https://www.jianshu.com/p/ad8b3514b852
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞