Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UI Automator Becomes Friendly with Espresso

TOYAMA Sumio
November 17, 2015

UI Automator Becomes Friendly with Espresso

Roppongi.aar #2 の発表資料です。
UI Automatorのバージョン2より、Espressoと同一テストメソッド内で混ぜて使えるようになりました。
このスライドでは、混ぜて使うことで便利になった点を、実例を挙げて説明しています。

サンプルコードのURL: https://github.com/sumio/UiAutomatorEspressoSample

TOYAMA Sumio

November 17, 2015
Tweet

More Decks by TOYAMA Sumio

Other Decks in Programming

Transcript

  1. UI  Automator  
    Becomes  Friendly  with  
    Espresso
    2015.11.17  
    @sumio_tym  (TOYAMA  Sumio)
    Roppongi.aar  #2
    Portions  of  this  presentation  are  reproduced  from  work  created  and  shared  by  the  Android  Open  Source  Project[1]  
    and  used  according  to  terms  described  in  the  Creative  Commons  2.5  Attribution  License[2].  
    [1]  http://code.google.com/policies.html  
    [2]  http://creativecommons.org/licenses/by/2.5/

    View Slide

  2. About  Me
    •  TOYAMA  Sumio  (֎ࢁ७ੜ)  
    @sumio_tym  (twitter),  @sumio  (github)  
    •  NTT  Software  Corporation  
    – Provide  technical  assistance  to  Android  Projects  
    •  Private  activities  
    – STAR  (Software  Test  Automation  Research  Group  Jp)  
    – Some  articles  in  @IT  related  to  uiautomator/Appium:  
    ʮεϚϗ޲͚ແྉγεςϜςετࣗಈԽπʔϧʯ  
    http://www.atmarkit.co.jp/ait/kw/smapho_testtool.html  
    2  

    View Slide

  3. Overview
    •  Architecture  of  UI  Automator  is  changed  into  
    Instrumentation  Test  as  of  ver.  2.x.  
    •  The  change  makes  you  
    write  a  test  method  in  mixture  of  Espresso  and  UI  
    Automator.  
    •  We  show  a  concrete  example  in  this  talk.  
    3  

    View Slide

  4. Review  of  
    Android  Testing  Frameworks
    4  
    •  Instrumentation  Test  
    •  Espresso  
    •  UI  Automator

    View Slide

  5. Instrumentation  Test  (Android)
    5  
    •  Both  application  
    under  test  (AUT)  and  
    test  package:    
    – run  on  the  same  
    process.  
    – must  be  signed  by  the  
    same  certificate.  
    •  The  test  package  can  
    examine  the  AUT  
    deeply.    
    (e.g.  fields  of  Activity)  
    Quoted  from  http://developer.android.com/intl/ja/tools/testing/testing_android.html

    View Slide

  6. Espresso
    6  
    •  Testing  framework  for  writing  UI  tests  
    – Single  app  testing  
    – White-­‐box  style  testing  
    (Can  access  the  interior  of  the  AUT)  
    •  Architecture:    Instrumentation  test  
    •  Supported  API  level:  ≥8  
    •  Easier  to  write  
    Quoted  from  http://developer.android.com/intl/ja/training/testing/ui-­‐testing/espresso-­‐testing.html

    View Slide

  7. UI  Automator
    7  
    •  Testing  framework  for  writing  UI  tests  
    – Cross-­‐app  testing  
    – Black-­‐box  style  testing  
    (Can  not  access  the  interior  of    the  AUT)  
    •  Architecture:  
    – Specialized  architecture  for  UI  Automator  (as  of  1.x)  
    – Instrumentation  test  (as  of  2.x)  
    •  Supported  API  level:  
    – ≥16  (as  of  1.x)  
    – ≥18  (as  of  2.x)  
    •  More  complicated  to  write  

    View Slide

  8. UI  Automator  (cont.)
    8  
    •  See  also  
    "Notable  Changes  in  UI  Automator  2.0"  
    (ABC  2015  Summer)  
    https://speakerdeck.com/sumio/notable-­‐changes-­‐in-­‐ui-­‐automator-­‐2-­‐dot-­‐0  
    Quoted  from  http://developer.android.com/intl/ja/training/testing/ui-­‐testing/uiautomator-­‐testing.html

    View Slide

  9. •  Now,  both  Espresso  and  UI  Automator  are    
    based  on  Instrumentation  Test.  
    •  So,  you  can  write  a  test  method  in  a  mixture  of  
    them.  
    •  That  widen  a  testable  area:  
    – Use  Espresso  for  AUT  (main  purpose)  
    – Use  UI  Automator  for  other  apps  (e.g.  launched  by  
    the  AUT)  
    9  
    UI  Automator  Becomes  Friendly  
    with  Espresso

    View Slide

  10. How  to  Setup
    10  
    Available  from  https://github.com/sumio/UiAutomatorEspressoSample

    View Slide

  11. build.gradle
    11  
    dependencies {
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile \
    'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile \
    'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    }
    for  Instrumentation  Test
    for  UI  Automator
    for  Espresso

    View Slide

  12. Test  Class
    12  
    @RunWith(AndroidJUnit4.class)
    public class MainActivityTest {
    @Rule
    public ActivityTestRule activityRule
    = new ActivityTestRule<>(MainActivity.class);
    private UiDevice uiDevice;
    @Before
    public void setUp() {
    uiDevice = UiDevice.
    getInstance(InstrumentationRegistry.getInstrumentation());
    }
    // YOUR TEST METHOD HERE
    }
    for  Instrumentation  Test
    for  UI  Automator

    View Slide

  13. Test  Example
    13  
    Available  from  https://github.com/sumio/UiAutomatorEspressoSample

    View Slide

  14. Application  Under  Test
    14  
    Simple  text  box
    Launch  Settings  App

    View Slide

  15. Scenario
    15  
    1.  (In  AUT)  
    Tap  "ઃఆΞϓϦΛදࣔ͢Δ"  
    2.  (In  Settings  App)  
    Tap  "೔෇ͱ࣌ࠁ"  
    3.  Turn  on/off  "24࣌ؒදࣔ"  
    4.  Press  the  back  key  twice  
    5.  (In  AUT)  
    Enter  "Hello  World!"  into  the  
    text  box  
    by  using  UI  Automator

    View Slide

  16. Test  Method  Implementation
    16  
    @Test
    public void espresso_uiautomator() throws Exception {
    // Tap "ઃఆΞϓϦΛදࣔ͢Δ"
    onView(withId(R.id.button_setting)).perform(click());
    // Tap "೔෇ͱ࣌ࠁ"
    UiScrollable settingList =
    new UiScrollable(new UiSelector().
    resourceId(SETTINGS_CONTAINER_ID));
    settingList.setAsVerticalList();
    UiObject dateTimeItem =
    settingList.getChildByText(new UiSelector().
    className(FrameLayout.class),
    "೔෇ͱ࣌ࠁ");
    dateTimeItem.clickAndWaitForNewWindow();
    // (continued on next slide)
    UI  Automator
    Espresso

    View Slide

  17. Test  Method  Implementation  (cont.)
    17  
    // Turn on/off "24࣌ؒදࣔ"
    UiCollection dateTimeList =
    new UiCollection(new UiSelector().
    className(ListView.class));
    UiObject use24HourItem =
    dateTimeList.getChildByText(new UiSelector().
    className(LinearLayout.class),
    "24࣌ؒදࣔ");
    use24HourItem.clickAndWaitForNewWindow();
    // go back to AUT.
    uiDevice.pressBack();
    uiDevice.waitForIdle();
    uiDevice.pressBack();
    uiDevice.waitForWindowUpdate(AUT_PACKAGE_NAME, 1500L);
    // Enter "Hello World!" into the text box
    onView(withId(R.id.editText)).
    perform(typeText("Hello World!"));
    }
     UI  Automator
    Espresso

    View Slide

  18. Wrap  Up
    18  
    •  UI  Automator  becomes  friendly  with  Espresso  
    – You  can  write  a  test  method  in  a  mixture  of  them.  
    •  Enables  you  to  write  a  cross-­‐app  test  more  easily    
    by  using  mainly  Espresso.  
    •  A  concrete  example  is  available  from  
    https://github.com/sumio/UiAutomatorEspressoSample  
    Thank  you  !

    View Slide