Your SlideShare is downloading. ×
0
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Introduction to Robotium
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Introduction to Robotium

2,336

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,336
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
89
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. An Intertech CourseRobotium TutorialMobile MarchMarch 21, 2013By Jim White, Intertech, Inc..
  • 2. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 2Stop by Intertech’sbooth for a chance towin FREE Training.Or go to bit.ly.com/intertech-loginSlides & demo code available at intertech.com/blog
  • 3. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 3Session 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&A
  • 4. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 4Purpose• 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!
  • 5. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 5Jim White Intro• Intertech Partner,• Dir. of Training,• Instructor,• Consultant• Co-author, J2ME, Java in SmallThings (Manning)• Device developer since beforephones were “smart”• Java developer when “spring”and “struts” described yourstride• Occasional beer drinker
  • 6. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 6Robotium – 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 internalstructures or workings of an application• Black box testing – testing software functionality without knowledge ofan application (perhaps where the source code is not even available)• Tests can be executed on an Android Virtual Device (AVD) or realdevice• Built on Java (and Android) and JUnit (the Android Test Framework)• In fact, it may be more appropriate to call Robotium an extension to theAndroid test framework
  • 7. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 7Robotium 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/list
  • 8. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 8Background - 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 IDE
  • 9. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 9Android Test Architecture• Architecturally, the unit testing project and app project run on thesame JVM (i.e. DVM).Test case classes,instrumentation, JUnit,mock objects, etc.
  • 10. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 10Robotium Project Setup• Add Robotium JAR to the JavaBuild Path• Put Robotium in the build pathorder.
  • 11. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 11Android JUnit Project SetupDemo
  • 12. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 12The “App” Used To Demo Today• Want to make sure data isentered.• Want to make sure data isvalid.• Age is less than 122• Zip has 5 characters• Make sure a role is clicked.• Make sure clear does clear thefields.• Etc.
  • 13. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 13Example JUnit Test (Continued)public class TestDataCollection extends ActivityInstrumentationTestCase2<DataCollectionActivity> {DataCollectionActivity activity;public TestDataCollection() {super(DataCollectionActivity.class);}@Overridepublic void setUp() throws Exception {super.setUp();activity = getActivity();}@Overrideprotected void tearDown() throws Exception {activity.finish();super.tearDown();}Extends AndroidTestCase –provides functionality for testing asingle Activity. Need to associate itto an Activity type (likeDataCollectionActivity)Test case initialization method (justlike JUnit 3).Test case tear down method (justlike JUnit 3).
  • 14. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 14Example JUnit Test (Continued)public void testCheckNameClear() {final EditText name = (EditText) activity.findViewById(R.id.nameEdit);activity.runOnUiThread(new Runnable() {public void run() {name.requestFocus();}});sendKeys("J I M");Button button = (Button) activity.findViewById(R.id.clearButton);TouchUtils.clickView(this, button);assertTrue("First name field is not empty.", name.getText().toString().equals(""));}}Grab widgets bytheir Android ID.Test methods mustbegin with “test”.UI adjustments/ work mustbe done on UI threadTestCase,TouchUtilsprovide limited UImaneuvering, butagain requiresdeep knowledgeof the UI detailsNormal assert methods tocheck results.
  • 15. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 15
  • 16. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 16Why 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 interactionsbetween an application and the system are made possible.• Use of runOnUIThread to execute UI work that isn’t covered by TouchUtils orTestCase class.
  • 17. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 17Example Robotium• Use Robotium tests in JUnit test class• Same code as in TestDataCollectionActivity above…• With a few additions/changes.private Solo solo;@Overridepublic void setUp() throws Exception {super.setUp();activity = getActivity();solo= new Solo(getInstrumentation(), getActivity());}Add a Solo membervariable and initialize itduring setUp( ).
  • 18. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 18Example Robotium (Continued)• The new test method – greatly simplified via Robotium!public void testCheckNameClear() {solo.enterText(0, "Jim"); // 0 is the index of the EditText fieldsolo.clickOnButton("Clear");assertTrue("First name field is not empty.",solo.getEditText(0).getText().toString().equals(""));}Solo methods allowwidgets to be selectedand interacted with.
  • 19. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 19
  • 20. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 20Robotium 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, …
  • 21. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 21Android Robotium DemoDemo
  • 22. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 22Tips & Tricks• Robotium (and all JUnit tests) operate in the same process (DVM) asthe 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 activitywork 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 oranything.
  • 23. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 23Tips & Tricks (Continued)• Use waitFor methods liberally.• Especially if new screen opens or changes to what is displayed areoccurring.• The waitFor methods tell Robotium to wait for a condition to happenbefore 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();}
  • 24. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 24Tips & 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 layoutchanges• Consider clickOnButton(“Clear”) vs.clickOnButton(6)
  • 25. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 25Tips & Tricks (Continued)• Resources in Android are at a premium (especially when test casesand 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 notifications
  • 26. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 26Tips & Tricks – Black Box Testing• Black Box Testing (when all you have is the APK file) is a little moretricky.• 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 testproject.• Probably have to delete the signature and then resign the APK with theAndroid debug key signature.• It’s easier than it sounds. See referenced document for help.
  • 27. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 27Android Robotium Black Box DemoBlack BoxDemo
  • 28. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 28Robotium Additional Features• Robotium can automatically take screenshots• solo.takeScreenshot( )• Robotium can be run from the command line (using adb shell)• adb shell am instrument -wcom.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.
  • 29. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 29Complimentary 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/RemoteControl
  • 30. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 30Resources• 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.html
  • 31. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 31Q&A• Questions – you got’em, I want’em
  • 32. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 32Award-Winning Training and Consulting.Visit www.Intertech.com for complete details.
  • 33. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 33Intertech offersMobile Training On:• Android• HTML5• iOS• Java ME• jQuery• Windows PhoneVisit ww.Intertech.com for complete course schedule.
  • 34. Course NameCopyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 34Stop by Intertech’sbooth for a chance towin FREE Training.Or go to bit.ly.com/intertech-login

×