34
An Intertech Course Robotium Tutorial Mobile March March 21, 2013 By Jim White, Intertech, Inc..

Introduction to Robotium

Embed Size (px)

Citation preview

Page 1: Introduction to Robotium

An Intertech Course

Robotium Tutorial

Mobile March

March 21, 2013

By Jim White, Intertech, Inc..

Page 2: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 2

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/blog

Page 3: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 3

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&A

Page 4: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 4

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!

Page 5: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 5

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 drinker

Page 6: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 6

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 framework

Page 7: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 7

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/list

Page 8: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 8

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 IDE

Page 9: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 9

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.

Page 10: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 10

Robotium Project Setup

• Add Robotium JAR to the Java Build Path

• Put Robotium in the build path order.

Page 11: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 11

Android JUnit Project Setup

Demo

Page 12: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 12

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.

Page 13: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 13

Example JUnit Test (Continued) public class TestDataCollection extends ActivityInstrumentationTestCase2<DataCollectionActivity> {

DataCollectionActivity activity;

public TestDataCollection() {

super(DataCollectionActivity.class);

}

@Override

public void setUp() throws Exception {

super.setUp();

activity = getActivity();

}

@Override

protected void tearDown() throws Exception {

activity.finish();

super.tearDown();

}

Extends AndroidTestCase –

provides functionality for testing a

single Activity. Need to associate it

to an Activity type (like

DataCollectionActivity)

Test case initialization method (just

like JUnit 3).

Test case tear down method (just

like JUnit 3).

Page 14: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 14

Example 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 by

their Android ID.

Test methods must

begin with “test”.

UI adjustments/ work must

be done on UI thread

TestCase,

TouchUtils

provide limited UI

maneuvering, but

again requires

deep knowledge

of the UI details

Normal assert methods to

check results.

Page 15: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 15

Page 16: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 16

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.

Page 17: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 17

Example Robotium

• Use Robotium tests in JUnit test class

• Same code as in TestDataCollectionActivity above…

• With a few additions/changes.

private Solo solo; @Override public void setUp() throws Exception { super.setUp(); activity = getActivity(); solo= new Solo(getInstrumentation(), getActivity()); }

Add a Solo member

variable and initialize it

during setUp( ).

Page 18: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 18

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.

Page 20: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 20

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, …

Page 21: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 21

Android Robotium Demo

Demo

Page 22: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 22

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.

Page 23: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 23

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(); }

Page 24: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 24

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)

Page 25: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 25

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 notifications

Page 26: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 26

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.

Page 27: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 27

Android Robotium Black Box Demo

Black Box

Demo

Page 28: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 28

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.

Page 29: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 29

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/RemoteControl

Page 30: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 30

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.html

Page 31: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 31

Q&A

• Questions – you got’em, I want’em

Page 32: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 32

Award-Winning Training and Consulting.

Visit www.Intertech.com for complete details.

Page 33: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 33

Intertech offers

Mobile Training On:

• Android

• HTML5

• iOS

• Java ME

• jQuery

• Windows Phone

Visit ww.Intertech.com for complete course schedule.

Page 34: Introduction to Robotium

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 34

Stop by Intertech’s booth for a chance to win FREE Training.

Or go to bit.ly.com/intertech-login