Introduction
Espresso and Robotium are both Android UI Automation test frameworks and are open source libraries. Robotium, the most widely used testing framework in the early Android world, has expanded a number of effective testing methods for the Android UI based on JUnit.
Espresso is a new tool that, relatively speaking, is more accurate and helps developers write more concise UI test code for the app. Espresso's birth, the biggest advantage lies in "fast". In Robotium's test code, there is usually a lot of sleep,waitfor to wait for the control to load, otherwise it is very likely to fail. The espresso, however, does not have synchronization headaches and therefore greatly improves the test speed.
For most Android instrumentation-based test frameworks (Robotium), the test thread is independent of the UI thread, and espresso is different, and the runtime is automatically synchronized with the UI thread, so Espresso's actions and assertions operations run fast.
Below, let us together to feel "Fly general feeling"!
Test your phone: VIVO X520L--CPU: four cores; Memory: 3GB
Test object: Notepad--robotium sample test application with source code
Test content:
- New text note 1, save;
- New Text note 2, save;
- Check for note 1 and note 2 new success
- Click to enter note 1 text, use menu to delete;
- Long press note 2 to delete.
- Check Note 1 and note 2 Delete success
Test code
1. robotium test Code
Public classNotepadrbotextendsActivityinstrumentationtestcase2<noteslist>{ PrivateSolo solo; PublicNotepadrbot () {Super(Noteslist.class); } Public voidTestaddnote ()throwsException {//New Note 1 and note 2, and determine if successfulSolo.clickonmenuitem ("Add Note"); Solo.entertext (0, "Note 1"); Solo.clickonmenuitem ("Save"); Solo.clickonmenuitem ("Add Note"); Solo.entertext (0, "Note 2"); Solo.clickonmenuitem ("Save"); Booleanexpected =true; Booleanactual = Solo.searchtext ("Note 1") && Solo.searchtext ("Note 2")); Assertequals ("Note 1 and/or note 2 is not found", expected, actual); //Delete Note 1 and note 2 and determine if the successSolo.clickontext ("Note 1"); Solo.clickonmenuitem ("Delete"); BooleanExpected2 =false; Solo.clicklongontext ("Note 2"); Solo.clickontext ("Delete"); BooleanActual2 = Solo.searchtext ("Note 1") | | Solo.searchtext ("Note 2"); Assertequals ("Note 1 and/or note 2 is found", Expected2, Actual2); }}
Robotium test Results
2. Espresso test Code
Public classNotepadespextendsActivityinstrumentationtestcase2<noteslist>{ PublicNotepadesp () {Super(Noteslist.class); } Public voidTestclickbutton ()throwsinterruptedexception {//Add Note1Onview (IsRoot ()). Perform (Viewactions.pressmenukey ()); Onview (Viewmatchers.withtext ("Add Note") . Perform (Viewactions.click ()); Onview (Viewmatchers.withid (R.id.note)). Perform (Viewactions.typetext ("Note 1"), Closesoftkeyboard ()); Onview (IsRoot ()). Perform (Viewactions.pressmenukey ()); Onview (Viewmatchers.withtext ("Save") . Perform (Viewactions.click ()); //Add Note2Onview (IsRoot ()). Perform (Viewactions.pressmenukey ()); Onview (Viewmatchers.withtext ("Add Note") . Perform (Viewactions.click ()); Onview (Viewmatchers.withid (R.id.note)). Perform (Viewactions.typetext ("Note 2")); Onview (IsRoot ()). Perform (Viewactions.pressmenukey ()); Onview (Viewmatchers.withtext ("Save") . Perform (Viewactions.click ()); //Menu Delete Note1Onview (Viewmatchers.withtext ("Note 1")) . Perform (Viewactions.click ()); Onview (IsRoot ()). Perform (Viewactions.pressmenukey ()); Onview (Viewmatchers.withtext ("Delete") . Perform (Viewactions.click ()); Onview (Viewmatchers.withtext ("Note 2") . Perform (Viewactions.longclick ()); Onview (Viewmatchers.withtext ("Delete") . Perform (Viewactions.click ()); }}
Espresso Test Results
Summary
Robotium |
Espresso |
32.682 s |
5.694 s |
From the table, we can see clearly that executing the same test case, the espresso speed is 5.7 times times the robotium. Unlike the sleep/poll mechanism of robotium, the espresso is fully event-driven and the test thread synchronizes with the UI thread, with significant speed advantages.
Robotium and Espresso Big pk--speed Chapter