Robotium at Android Only 2010-09-29
-
Upload
hugojosefson -
Category
Technology
-
view
5.115 -
download
1
description
Transcript of Robotium at Android Only 2010-09-29
![Page 1: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/1.jpg)
Robotium
Easy Black-box Testingfor Android
Renas Reda / Hugo Josefson
![Page 2: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/2.jpg)
Agenda
● Vanilla vs Robotium– Instrumentation testing– Write readable tests, with 10x less code [DEMO]– (Still) access all the powers of Instrumentation [DEMO]
● Darker than Black box [DEMO]● Robotium with Maven [DEMO]
● The future of testing on Android
![Page 3: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/3.jpg)
Black box testing?
• ...doesn't know how an application is designed at the code level...
– bughuntress.com/analytics/glossary.html
• ...done without reference to the source code...– www.sqablogs.com/jstrazzere/46/A+Glossary+of+Testing+Terms
.html
![Page 4: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/4.jpg)
Instrumentation testing
Vanillavs
Robotium
![Page 5: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/5.jpg)
Instrumentation Testing
(vanilla)
![Page 6: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/6.jpg)
What's instrumentation testing?
• Investigate interaction with UI
• Pressing buttons, navigating menus
• Blackbox method(?)
• Takes time
• Can use JUnit
• Good for acceptance or system tests
• Can be automated!
![Page 7: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/7.jpg)
Android built-in support
• Runs on device/emulator
• JUnit 3 compatible framework
• Allows direct control of user interface– Touches– Taps– Scrolling in lists
• Can be started from Eclipse or shell
![Page 8: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/8.jpg)
Android built-in support
• Tapping a view:TouchUtils.tapView(someView)
• Pressing key (physical button):getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU)
• Sending text:sendKeys(“some text”)
![Page 9: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/9.jpg)
Drawbacks and limitations
• Required to know implementation details
• You often have to manually add Thread.sleep(500) to make tests work
• Large apps can be very complex to test
• Tests run slowly– Like if it would be done manually– Not suitable for TDD
![Page 10: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/10.jpg)
Setting up instrumentation tests
• Runs in emulator/device using JUnit 3
• Separate test project as normal
![Page 11: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/11.jpg)
Writing an instrumentation test
![Page 12: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/12.jpg)
Testing a Calculator GUI
![Page 13: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/13.jpg)
Writing an instrumentation test// Find viewsEditText num1 = (EditText) getActivity().findViewById(com.calculator.R.id.num1);EditText num2 = (EditText) getActivity().findViewById(com.calculator.R.id.num2);Button addButton = (Button) getActivity().findViewById(com.calculator.R.id.add_button);
// Perform instrumentation commandsTouchUtils.tapView(this, num1);sendKeys(KeyEvent.KEYCODE_1,KeyEvent.KEYCODE_5);TouchUtils.tapView(this, num2);sendKeys(KeyEvent.KEYCODE_5);TouchUtils.tapView(this, addButton);
// Fetch result and compare it against expected valueString actual = num1.getText().toString();String expected = "20.0";assertEquals("Addition incorrect", expected,actual);
![Page 14: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/14.jpg)
Running instrumentation tests
• Started from Eclipse as “Android JUnit test”
• Can also be started from command line:
adb shell am instrument -w com.package/android.test.InstrumentationTestRunner
![Page 15: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/15.jpg)
Instrumentation testingwith
Robotium
![Page 16: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/16.jpg)
Robotium
• Facts– Instrumentation testing framework– Add-on jar– Open Source (Apache 2)
• Purpose– Simplify making tests
![Page 17: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/17.jpg)
• Benefits– Easy to write, shorter code– Automatic timing and delays– Automatically follows current Activity– Automatically finds Views– Automatically makes own decisions
– when to scroll etc.
– No modification to Android platform– Test execution is fast
• Current limitations– Tied to JUnit 3 Instrumentation on device– Tied to one app process– Needs initial Activity(?)
![Page 18: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/18.jpg)
Writing tests with Robotium
• You use only one class: Solo
• Test class like for normal instrumentation:
![Page 19: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/19.jpg)
Remember the Calculator GUI?
![Page 20: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/20.jpg)
Remember standard instrumentation test?
// Find viewsEditText num1 = (EditText) getActivity().findViewById(com.calculator.R.id.num1);EditText num2 = (EditText) getActivity().findViewById(com.calculator.R.id.num2);Button addButton = (Button) getActivity().findViewById(com.calculator.R.id.add_button);
// Perform instrumentation commandsTouchUtils.tapView(this, num1);sendKeys(KeyEvent.KEYCODE_1,KeyEvent.KEYCODE_5);TouchUtils.tapView(this, num2);sendKeys(KeyEvent.KEYCODE_5);TouchUtils.tapView(this, addButton);
// Fetch result and compare it against expected valueString actual = num1.getText().toString();String expected = "20.0";assertEquals("Addition incorrect", expected,actual);
![Page 21: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/21.jpg)
Test Calculator with Robotium
public void testAdd() { solo.enterText(0, "5"); solo.enterText(1,"3"); solo.clickOnButton("Add"); assertTrue(solo.searchEditText("8.0"));}
![Page 22: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/22.jpg)
Some Robotium commands
• clickOnButton(String regex)
• clickInList(int line)
• enterText(int index, String text)
• searchText(String regex)
• clickOnMenuItem(String regex)
• getCurrentActivity()
• goBack(), goBackToActivity(String name)
![Page 23: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/23.jpg)
Writing Tests with Robotium+ still access standard Instrumentation
[DEMO]
![Page 24: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/24.jpg)
Darker than Black Box
![Page 25: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/25.jpg)
Black box testing?
• ...doesn't know how an application is designed at the code level...
– bughuntress.com/analytics/glossary.html
• ...done without reference to the source code...– www.sqablogs.com/jstrazzere/46/A+Glossary+of+Testing+Terms
.html
![Page 26: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/26.jpg)
The usual “Black box” way
• Two projects in Eclipse– App project– Test project
• Write test without looking at app's internals
![Page 27: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/27.jpg)
The Darker than Black box way
• Any application's apk file
• One project in Eclipse– App project
– Test project
• No access to original project
• Write test without any access to app's internals
![Page 28: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/28.jpg)
Darker than Black BoxTest any APK w/o its source code
[DEMO]
![Page 29: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/29.jpg)
Robotium with Maven
![Page 30: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/30.jpg)
Maven
• Project management + Build tool
• Based on sane defaults– Can be overridden with configuration
– Only configure what's different
• Plugins add functionality– maven-android-plugin: build Android projects +
run Instrumentation tests
• Dependencies downloaded automatically– Declare need for Robotium
→ Maven downloads jar
![Page 31: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/31.jpg)
Declare need for Robotiumin test project
<dependency> <groupId>com.jayway.android.robotium</groupId> <artifactId>robotium-solo</artifactId> <version>1.8.0</version></dependency>
![Page 32: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/32.jpg)
Perform build and Run all Testsincluding Robotium tests
$ mvn install
![Page 33: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/33.jpg)
Maven with RobotiumAutomating test execution
[DEMO]
![Page 34: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/34.jpg)
The future of Testing
on Android
![Page 35: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/35.jpg)
Google's Roadmap
?
![Page 36: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/36.jpg)
Google's Roadmap
http://source.android.com/roadmap/
– Last entry “Beyond Q1 2009”:– Support for WVGA and QVGA...
– Not keen on disclosing what they will /
will not release.
![Page 37: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/37.jpg)
Google's Roadmap
http://source.android.com/roadmap/
– Last entry “Beyond Q1 2009”:– Support for WVGA and QVGA...– Page deleted
– Not keen on disclosing what they will /
will not release.
![Page 38: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/38.jpg)
Robotium's Roadmapfor Instrumentation Testing
• Features we want to implement next– Remote control
– Similar to Selenium RC
– Cucumber integration– Generate screenshot on failure– UI test coverage
• Features further down the line?– Multidevice support
![Page 39: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/39.jpg)
Robotium resources
Getting Started +Robotium Developers discussion group:
www.robotium.org
Questions?
![Page 40: Robotium at Android Only 2010-09-29](https://reader033.fdocuments.net/reader033/viewer/2022052906/5588f9d3d8b42af3678b469d/html5/thumbnails/40.jpg)