Robotium Tutorial

21,367 views

Published on

From the 3/21 presentation at Mobile March 2013

Published in: Technology

Robotium Tutorial

  1. 1. Robotium Tutorial Mobile March March 21, 2013 An Intertech Course By Jim White, Intertech, Inc..
  2. 2. Course Name Stop by Intertech’s booth for a chance to win FREE Training. Or go to bit.ly.com/intertech-login Slides & demo code available at intertech.com/blogCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 2
  3. 3. Course Name Session Agenda • Robotium… • What is it? • Where to get it and how to set it up • “Normal” Android unit testing background • Why Robotium is needed • Using Robotium • Robotium Tips/Tricks/Issues • Complimentary tools • Further Resources • Q&ACopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 3
  4. 4. Course Name Purpose • The main point/purpose to my talk… • There are wonderful test/QA tools available for Android! • There are no excuses for skipping unit testing in Android!Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 4
  5. 5. Course Name Jim White Intro • Intertech Partner, • Dir. of Training, • Instructor, • Consultant • Co-author, J2ME, Java in Small Things (Manning) • Device developer since before phones were “smart” • Java developer when “spring” and “struts” described your stride • Occasional beer drinkerCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 5
  6. 6. Course Name Robotium – what is it? • An open source test framework • Used to write black or white box tests (emphasis is on black box) • White box testing – testing software that knows and tests the internal structures or workings of an application • Black box testing – testing software functionality without knowledge of an application (perhaps where the source code is not even available) • Tests can be executed on an Android Virtual Device (AVD) or real device • Built on Java (and Android) and JUnit (the Android Test Framework) • In fact, it may be more appropriate to call Robotium an extension to the Android test frameworkCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 6
  7. 7. Course Name Robotium Project Setup • Prerequisites • Install and setup JDK • Install and setup Eclipse (optional) • Install and setup Android Standard Development Kit (SDK) • Supports Android 1.6 (API level 4) and above • Install and setup Android Development Tools (ADT) for Eclipse (optional) • Create Android AVD or attach device by USB • Create an Android Test Project • Download Robotium JAR and add to project classpath • robotium-solo-X.X.jar (version 3.6 the latest as of this writing) • From code.google.com/p/robotium/downloads/listCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 7
  8. 8. Course Name Background - Android JUnit Testing • Android testing is based on JUnit • You create test suites, classes (test cases), methods • Organize tests into a Android Test project • Android API supports JUnit 3 code style – not JUnit 4! • No annotations • Old JUnit naming conventions • Test case classes can extend good-old-fashion JUnit3 TestCase • To call Android APIs, base class must extend AndroidTestCase • Use JUnit Assert class to check/display test results • Execute tests using an SDK provided InstrumentationTestRunner • android.test.InstrumentationTestRunner • Usually handled automatically via IDECopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 8
  9. 9. Course Name Android Test Architecture • Architecturally, the unit testing project and app project run on the same JVM (i.e. DVM). Test case classes, instrumentation, JUnit, mock objects, etc.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 9
  10. 10. Course Name Robotium Project Setup • Add Robotium JAR to the Java • Put Robotium in the build path Build Path order.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 10
  11. 11. Course Name Android JUnit Project Setup DemoCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 11
  12. 12. Course Name The “App” Used To Demo Today • Want to make sure data is entered. • Want to make sure data is valid. • Age is less than 122 • Zip has 5 characters • Make sure a role is clicked. • Make sure clear does clear the fields. • Etc.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 12
  13. 13. Course Name Example JUnit Test (Continued) public class TestDataCollection extends ActivityInstrumentationTestCase2<DataCollectionActivity> { DataCollectionActivity activity; public TestDataCollection() { Extends AndroidTestCase – super(DataCollectionActivity.class); provides functionality for testing a single Activity. Need to associate it } to an Activity type (like DataCollectionActivity) @Override public void setUp() throws Exception { super.setUp(); Test case initialization method (just activity = getActivity(); like JUnit 3). } @Override protected void tearDown() throws Exception { activity.finish(); Test case tear down method (just super.tearDown(); like JUnit 3).Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 13 }
  14. 14. Course Name Example JUnit Test (Continued) Test methods must begin with “test”. public void testCheckNameClear() { final EditText name = (EditText) activity.findViewById(R.id.nameEdit); activity.runOnUiThread(new Runnable() { Grab widgets by public void run() { their Android ID. name.requestFocus(); UI adjustments/ work must } be done on UI thread }); sendKeys("J I M"); TestCase, TouchUtils Button button = (Button) activity.findViewById(R.id.clearButton); provide limited UI TouchUtils.clickView(this, button); maneuvering, but assertTrue("First name field is not empty.", name.getText().toString().equals("")); again requires } deep knowledge } of the UI details Normal assert methods to check results.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 14
  15. 15. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 15
  16. 16. Course Name Why Android JUnit Isn’t Enough • Requires deep knowledge of widgets • Widget IDs • Widget Properties • What has focus • Order of widgets • Etc. • Often requires deep knowledge of Android internals • Especially around menus, dialogs, etc. • Makes for brittle unit tests • As the UI changes, the test often must change dramatically. • Poor instrumentation • Instrumentation is a feature in which specific monitoring of the interactions between an application and the system are made possible. • Use of runOnUIThread to execute UI work that isn’t covered by TouchUtils or TestCase class.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 16
  17. 17. Course Name Example Robotium • Use Robotium tests in JUnit test class • Same code as in TestDataCollectionActivity above… • With a few additions/changes. private Solo solo; @Override Add a Solo member public void setUp() throws Exception { variable and initialize it super.setUp(); during setUp( ). activity = getActivity(); solo= new Solo(getInstrumentation(), getActivity()); }Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 17
  18. 18. Course Name Example Robotium (Continued) • The new test method – greatly simplified via Robotium! public void testCheckNameClear() { solo.enterText(0, "Jim"); // 0 is the index of the EditText field solo.clickOnButton("Clear"); assertTrue("First name field is not empty.",solo.getEditText(0). getText().toString().equals("")); } Solo methods allow widgets to be selected and interacted with.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 18
  19. 19. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 19
  20. 20. Course Name Robotium Solo API • Robotium is all baked into one class - Solo – with many methods: • clickX methods: clickOnButton, clickOnImage, clickOnText,… • clickLongX methods: clickLongInList, clickLongOnScreen, clickLongOnText,… • enterText • drag • getX methods: getButton, getCurrentActivity, getImage, getEditText, … • goBack • isX methods: isCheckBoxChecked, isRadioButtonChecked, isSpinnerTextSelected, isTextChecked,… • pressX methods: pressMenuItem, pressMenuItem, pressSpinnerItem, … • scrollX methods: scrollToTop, scrollToBottom, … • searchX methods: searchButton, searchEditText, searchText, … • waitForX methods: waitForActivity, waitForText, …Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 20
  21. 21. Course Name Android Robotium Demo DemoCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 21
  22. 22. Course Name Tips & Tricks • Robotium (and all JUnit tests) operate in the same process (DVM) as the original app • Robotium only works with the activities and views within the defined app • For example: Can’t use intent to launch another app and test activity work from that app • The popup keyboard is accomplished with a bitmap in Android • Robotium (or any unit test software) doesn’t see the “keys” as buttons or anything.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 22
  23. 23. Course Name Tips & Tricks (Continued) • Use waitFor methods liberally. • Especially if new screen opens or changes to what is displayed are occurring. • The waitFor methods tell Robotium to wait for a condition to happen before the execution continues. public void testGoodLogin() { solo.enterText(0, “username"); solo.enterText(1, “password"); String label = res.getString(R.string.login_button_label); solo.clickOnButton(label); String title = res.getString(R.string.title_activity_systemv); solo.waitForText(title); solo.assertCurrentActivity("systemv", SystemVActivity.class); solo.getCurrentActivity().finish(); }Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 23
  24. 24. Course Name Tips & Tricks (Continued) • RadioButtons are Buttons, EditText are Text, etc… • Getting the proper widget by index can be more difficult • Use of index also makes the test case more brittle due to potential layout changes • Consider clickOnButton(“Clear”) vs. clickOnButton(6)Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 24
  25. 25. Course Name Tips & Tricks (Continued) • Resources in Android are at a premium (especially when test cases and App code are running in same DVM). • Use solo.finishOpenedActivities() in your tearDown method. • It closes all the opened activities. • Frees resources for the next tests • Robotium has some difficulty with animations • Robotium doesn’t work with status bar notificationsCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 25
  26. 26. Course Name Tips & Tricks – Black Box Testing • Black Box Testing (when all you have is the APK file) is a little more tricky. • Recall in the demo, the test application wants the main activity name? public TestDataCollectionActivity() { super(DataCollectionActivity.class); } • You may not know this for a 3rd party/black box app. • You can get the activity name by loading the APK to an AVD or device, running it, and watching the logcat. • The APK file has to have the same certificate signature as the test project. • Probably have to delete the signature and then resign the APK with the Android debug key signature. • It’s easier than it sounds. See referenced document for help.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 26
  27. 27. Course Name Android Robotium Black Box Demo Black Box DemoCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 27
  28. 28. Course Name Robotium Additional Features • Robotium can automatically take screenshots • solo.takeScreenshot( ) • Robotium can be run from the command line (using adb shell) • adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner • Robotium can test with localized strings • solo.getString(localized_resource_string_id) • Code coverage is a bit lackluster at this time • Can be done with special ant task and command line tools • Robotium does not work on Flash or Web apps.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 28
  29. 29. Course Name Complimentary Tools • Robotium Testroid Recorder • Record actions to generate Android JUnit/Robotium test cases • Run tests on 180 devices “in the cloud” • Testdroid.com • Commercial product (50 runs free, $99/month or ¢99/run) • Robotium Remote Control • Allows Robotium test cases to be executed from the JVM (on a PC) • This allows Robotium to work with JUnit 4. • code.google.com/p/robotium/wiki/RemoteControlCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 29
  30. 30. Course Name Resources • These slides and demo code: intertech.com/blog • Google Robotium site • code.google.com/p/robotium • code.google.com/p/robotium/wiki/RobotiumTutorials • Tutorial Articles/Blog Posts • devblog.xing.com/qa/robotium-atxing/ • www.netmagazine.com/tutorials/automate-your-android-app-testing • robotiumsolo.blogspot.com/2012/12/what-is-robotium.html • www.vogella.com/articles/AndroidTesting/article.html • robotium.googlecode.com/files/RobotiumForBeginners.pdf • excellent article for black box testing when all you have is the APK • Fundamentals of Android Unit Testing • developer.android.com/tools/testing/testing_android.htmlCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 30
  31. 31. Course Name Q&A • Questions – you got’em, I want’emCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 31
  32. 32. Course Name Award-Winning Training and Consulting. Visit www.Intertech.com for complete details.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 32
  33. 33. Course Name Intertech offers Mobile Training On: • Android • HTML5 • iOS • Java ME • jQuery • Windows Phone Visit ww.Intertech.com for complete course schedule.Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 33
  34. 34. Course Name Stop by Intertech’s booth for a chance to win FREE Training. Or go to bit.ly.com/intertech-loginCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 34

×