(4.5.5.9)Espresso之UiAutomator2与Espresso的结合

来源:互联网 发布:免费音乐制作软件 知乎 编辑:程序博客网 时间:2024/06/06 03:40

我们都已经知道Espresso是基于Android instrumentation进行的自动化,而有了解的朋友们应该也是知道的 UiAutomator2.0也是基于 instrumentation的,那这两个是否能够一起使用呢。

我们的答案是可以的。

我们先拿网上的例子来说下简单的应用

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();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

是不是很完美?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'}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

最近在测试的过程中又遇到另外一个需要UiAutoamtor解决的问题,在进行修改某个学生头像时,无法判断图片是否已经被修改了。这个貌似Espresso是没办法做到的 所以就需要依赖到Espresso了。

下来是测试的代码

 public void editStudentAvatar() {        for (int i=0;i<12;i++) {            clickAva();            Commons.clickRecyclerViewElementByPos(R.id.fragment_avatars_recyclerView, i);            screenStudentAva("/sdcard/1.jpg");            save();            Commons.clickRecyclerViewElementByText(R.id.add_student_recyclerView, studentList.get(0));            screenStudentAva("/sdcard/2.jpg");            Assert.assertTrue(ImageTestUtils.sameAs("/sdcard/1.jpg", "/sdcard/2.jpg"));        }  } public void screenStudentAva(String storePath) {        ImageTestUtils.screenShot(uiDevice);        UiObject2 studentAvatar = uiDevice.wait(Until.findObject(By.res("com.seewo.easicare.demo", "edit_student_avatar_imageView")), 500);        ImageTestUtils.imageCrop(studentAvatar.getVisibleBounds(), storePath);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

以上的作用主要是获取到对应头像的矩形边框,截取修改前后的图片做比较来判断头像是否修改成功。

参考文章

Espresso和UIAutomator - 完美的结合

0 0