[Android automated testing] robotium initial experience

Source: Internet
Author: User

Robotium performs Android automated testing for the initial experience. The content of this article is as follows:

I. Only APK, robotium testing without source code:

Ii. Source Code test of APK to be tested:

Robotium Web site: http://code.google.com/p/robotium/

 

 

I. Only APK, robotium testing without source code:

1) Make sure that the APK is a debug signature;

Download the re-sign.jar;

2) create a test project;

(1) create a project and select Android test project;

(2) Select "this project" for test target ":

(3) The package name can be the same as the package name of the APK for the test, or added with. Test;

For example, if the package name of the APK to be tested is com. myapk, the package name of the test project can be com. myapk or com. myapk. tset.

3) Load robotium-solo-2.1.jar;

Download robotium jar from

Http://code.google.com/p/robotium/downloads/list

4) Write test class;

As follows:

Package COM. trunk. ticket. act. test; import COM. jayway. android. robotium. solo. solo; import android. test. activityinstrumentationtestcase2; public class testapk extends activityinstrumentationtestcase2 {Private Static final string target_package_id = "com. trunk. ticket. act "; Private Static final string launcher_activity_full_classname =" com. trunk. ticket. act. mainactivity "; Private Static class launcheractivityclass; static {try {launcheractivityclass = Class. forname (expiration);} catch (classnotfoundexception e) {Throw new runtimeexception (e) ;}} public testapk () throws classnotfoundexception {super (target_package_id, launcheractivityclass);} private Solo; @ overrideprotected void setup () throws exception {solo = new solo (getinstrumentation (), getactivity ();}/** my test function */Public void testdisplayblackbox () {// enter any integer/decimal value for // first editfield, we are writing 10solo. entertext (0, "10"); // enter any interger/decimal value for first // editfield, we are writing 20solo. clickonbutton ("query"); // verify that resultant of 10x20 // asserttrue (solo. searchtext ("200") ;}@ overridepublic void teardown () throws exception {try {solo. finalize (); // robotify will finish all the activities that have been open} catch (throwable e) {e. printstacktrace ();} getactivity (). finish (); super. teardown ();}}

 

5) modify the androidmanifest. xml file

<Instrumentation Android: targetpackage = "com. myapk" Android: Name = "android. Test. instrumentationtestrunner"/>
Instrumentation Android: The targetpackage must be the package to be tested.

 

Install the APK to be tested on your phone and run as androidjunit test.

References: http://code.google.com/p/robotium/wiki/RobotiumForAPKFiles

Ii. Source Code test of APK to be tested:

1) create a test project. Same as above, select the project to be tested when selecting test target:

Other information will be written by default. Click "finish.

2) Write test class

As follows:

/** This is an example test project created in eclipse to test notepad which is a sample * project located in androidsdk/samples/Android-9/notepad * just click on file --> New --> project --> Android project --> Create project from existing source and * select notepad. ** then you can run these test cases either on the emulator or on device. you right click * The test project and select Run as --> Run as Android JUnit test ** @ author renas Reda, renas.reda@jayway.com **/package COM. mytest. app. test; import COM. mytest. app. test. mainactivity; import COM. jayway. android. robotium. solo. solo; import android. test. activityinstrumentationtestcase2; import android. test. suitebuilder. annotation. smoke; import android. util. log; public class mytest extends activityinstrumentationtestcase2 <mainactivity> {private Solo; Public mytest () {super ("com. mytest. APP ", mainactivity. class);} public void setup () throws exception {solo = new solo (getinstrumentation (), getactivity ());} /** my test function */@ smoke public void testaddnote () throws exception {solo. clickonmenuitem ("add note"); solo. entertext (0, "NOTE 2"); // in text field 0, add note 2 solo. gobacktoactivity ("noteslist"); // go back to first activity named "noteslist" Boolean expected = true; Boolean actual = solo. searchtext ("NOTE 1") & solo. searchtext ("NOTE 2"); assertequals ("NOTE 1 and/or note 2 are not found", expected, actual ); // assert that NOTE 1 & Note 2 are found} @ overridepublic void teardown () throws exception {try {solo. finalize (); // robotify will finish all the activities that have been open} catch (throwable e) {e. printstacktrace ();} getactivity (). finish (); super. teardown ();}}

3) The androidmanifest. xml file is automatically configured and runs directly without any modifications;

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.