Slide 1

Slide 1 text

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/

Slide 2

Slide 2 text

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  

Slide 3

Slide 3 text

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  

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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  

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

•  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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Application  Under  Test 14   Simple  text  box Launch  Settings  App

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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  !