A practical tutorial on Robotium Automated Testing framework

Source: Internet
Author: User

First, Introduction

Robotium is a foreign Android Automation testing framework, mainly for Android platform applications for black box Automation testing, it provides simulation of various gesture operations (click, Long Press, swipe, etc.), find and assert the mechanism of the API, to be able to manipulate the various controls. Robotium combines Android's official testing framework to automate the testing of applications. In addition, the Robotium 4.0 version already supports the operation of the WebView. Robotium is supportive of Activity,dialog,toast,menu.

second, related download

SOURCE Access: http://code.google.com/p/robotium/

Source API Documentation: http://robotium.googlecode.com/svn/doc/index.html

robotium5.1 download and latest API documentation: Http://pan.baidu.com/s/1bnlO8MF

Official Example Notepad: http://pan.baidu.com/s/1qWLVL72

Re-signing Tool: Http://pan.baidu.com/s/1i3H0tKD

Iii. Common Grammar
1. Wait for timeout milliseconds an activity that is named Name starts: waitforactivity (String name, int timeout) instance: Asserttrue ("Startup class cannot be started", Solo.waitforactivity ("mainactivity", 30000)) 2.Robotium the number of milliseconds to set sleep: sleep (int time) instance: Solo.sleep (5000) 3. Empty the contents of the EditText: Clearedittext (Android.widget.EditText EditText) instance: Solo.clearedittext ((EditText) Solo.getview (" Edtinsertname ")) 4. Click the button according to the text on the button: Clickonbutton (String text) instance: Solo.clickonbutton (" ^ Green $ "); 5. Click the control according to the text: Clickontext ( String text) instance: Solo.clickontext (display text on control); 6. Input: Entertext (Android.widget.EditText EditText, string text) Solo.entertext (EditText) Solo.getview ("Edtinsertname"), "What's the good of saying?" 7. Return: GoBack () 8. Screenshot and Save as set name: Takescreenshot (String name) is saved by default:/SDCARD/ROBOTIUM-SCREENSHOTS/9. Unlock screen: Unlockscreen ()

  

Iv. Actual combat-testing for APK

The test project is DEMO1, the following is the actual practical steps

1. ConfigurationAndroid_homE is the Android SDK for Android directory, for example:D:\ANDROID-SDK2. Add these two under path:%android_home%\tools;%android_home%\platform-tools;3. Need to re-sign the APK, because Robotium requires the test application and testing code to have a consistent signature, so we need to download to the APK, throughRe-sign.jarTo generate the Debug key apk, the regenerated apk will match the test project signature by 4. After downloading, you need to configureAndroid_home, which is the location of the Android SDK, and then pull the apk to the icon, it will automatically generate a debug key apk, if you can not directly click Re-sign.jar Run, you need to switch to the directory where the jar file is placed,cmdPerformJava-jar Re-sign.jarThe process of generating a new apk will pop up a message box, remember to cut, because there are two of information we will need to use the code 5. Install the resulting apk. Then open the emulator (the emulator must be open to install successfully), and then open the command line adb install mitalk_debug.apk (the name of the new APK), or double-click the apk file can also install the installation successfully can see the app icon in the emulator6. Open Eclipse, click File->new an android Test ProjectTestDemo1,then click Next to select this project (because we are testing apk) and then choose which Android version to test on7. Under the project, create a package, com.example.demo1.test, under the package to create the testdemo1apk class, as follows
Package Com.example.demo1.test;import Com.robotium.solo.solo;import Android.test.ActivityInstrumentationTestCase2 , Import Android.widget.EditText, @SuppressWarnings ("Rawtypes") public class testdemo1apk extends ActivityInstrumentationTestCase2 {private static final String Launcher_activity_full_classname = "Com.example.demo        1.MainActivity ";//Startup class private static class<?> Launcheractivityclass; static{try {launcheractivityclass = Class.forName (launcher_activity_full_classname                );                } catch (ClassNotFoundException e) {throw new RuntimeException (e);                }} @SuppressWarnings ("Unchecked") public testdemo1apk () throws ClassNotFoundException {        Super (Launcheractivityclass);                } private solo solo; @Override protected void SetUp () throws Exception {solo = new solo (getinstrumentation (), GETactivity ()); } public void testcase001 () throws Exception {//wait for Activity "mainactivity" to start Asserttrue ("Startup class cannot be started", SOLO.WAITF            Oractivity ("mainactivity", 30000));          Solo.sleep (5000); Input text: "131243" Solo.entertext ((EditText) Solo.getview ("Edtinsertname"), "What's the good of saying?"            ");                        Solo.sleep (2000);                                    Empty the contents of the Input Box Solo.clearedittext ((EditText) Solo.getview ("Edtinsertname"));            Press the button "green" Solo.clickonbutton ("^ Green $");            Solo.sleep (2000);            Press the button "yellow" Solo.clickonbutton ("^ Yellow $");            Solo.sleep (2000);            Press the button "blue" Solo.clickonbutton ("^ Blue $");            Solo.sleep (2000);            Press TextView "See me change Change ~ ~ ~" Solo.clickontext ("^ See Me change Change ~~~$");                      Solo.sleep (5000);  } @Override public void TearDown () throws Exception {solo.finishopenedactivities (); }}

 

8. Right click on the project, select property and select Java build path, select Add JARs, select the Robotium.jaradd Library below, click JUnit, select JUNIT4

9. Before running the test case, you also need to modify the android:targetpackage of the next Androidmanifest.xml file as the package name of the root of the application being tested
    <instrumentation        android:name= "Android.test.InstrumentationTestRunner"        android:targetpackage= " Com.example.demo1 "/>
10. Last: Run as Android JUnit test, it can be tested

Source Download : Http://pan.baidu.com/s/1ybPUI

v. Actual combat-for projects under the working directory

The test project is DEMO1, the following is the actual practical steps

1. Create a new test project named Demo1test

Eclipse-file-project-android Test Project

Test project named Demo1test, test target project selection Demo1

   

2. Add the necessary class libraries, robotium and JUnit

(1) Create a new Lib directory in the project and pull the Robotium-solo-5.1.jar and Robotium-solo-5.1-javadoc.jar into the directory.

(2) Add JUNIT4

3. Add Robotium

4. Create Test Cases

Right click on package-new-junit Test case

The most basic test case Framework class

Package Com.example.demo1.test;import com.example.demo1.mainactivity;//Import the startup class of the target project Android.test.activityinstrumentationtestcase2;import Com.robotium.solo.solo;public class TestDemo1 extends activityinstrumentationtestcase2<mainactivity>{//inheriting the target project's startup class private solo solo;//Initializes a solo object public TestDemo1 ( {//At the constructor indicate the startup class super (Mainactivity.class) that inherits from the target project;} @Overridepublic void SetUp () throws Exception {//This method is called before the test starts, here to create a solo object solo = new solo (Getinstrumentation (), Getactivity ());} @Overridepublic void TearDown () throws Exception {//This method is called when a test case ends solo.finishopenedactivities ();// This method will end all activity that was opened during the Test execution}}

Add the relevant test method to the base test class, which is the test case that is actually executing

Package Com.example.demo1.test;import com.example.demo1.mainactivity;//Import the startup class of the target project Android.test.activityinstrumentationtestcase2;import Android.widget.edittext;import Com.robotium.solo.Solo; public class TestDemo1 extends activityinstrumentationtestcase2<mainactivity>{//inherit the startup class of the target project private Solo solo;// Initializes a solo object public TestDemo1 () {//Identifies the Startup class super (Mainactivity.class) that inherits from the target project at the constructor;} @Overridepublic void SetUp () throws Exception {//This method is called before the test starts, here to create a solo object solo = new solo (Getinstrumentation (), Getactivity ());} public void testcase001 () throws Exception {//waits for Activity "mainactivity" to start Asserttrue ("Startup class cannot be started", Solo.waitforactivity ("        Mainactivity ", 30000));      Solo.sleep (5000); Input text: "131243" Solo.entertext ((EditText) Solo.getview ("Edtinsertname"), "What's the good of saying?"        ");                Solo.sleep (2000);                        Empty the contents of the Input Box Solo.clearedittext ((EditText) Solo.getview ("Edtinsertname"));        Press the button "green" Solo.clickonbutton ("^ Green $"); Solo.sleep (2000);        Press the button "yellow" Solo.clickonbutton ("^ Yellow $");        Solo.sleep (2000);        Press the button "blue" Solo.clickonbutton ("^ Blue $");        Solo.sleep (2000);        Press TextView "See me change Change ~ ~ ~" Solo.clickontext ("^ See Me change Change ~~~$");              Solo.sleep (5000); } @Overridepublic void TearDown () throws Exception {//This method is called when a test case ends solo.finishopenedactivities ();// This method will end all activity that was opened during the Test execution}}

source sharing : Http://pan.baidu.com/s/1mgKcgju

Vi. Description of some special settings1. Add to the library you need to create a new Lib directory, and then put the robotium-solo-5.2.1.jar inside, so that the mobile project will not find 2. Test Project Androidmanifest.xml inside &LT;USES-SDK android: minsdkversion= "Ten"/> need to be above 8, and be as large as the project being tested 3. Tick required

4. The most detailed thing is that the construction method must be parameterless, and the new test case is usually parameterized:

Public testhelloworldcase () {
Super (Herlloactivity.class);
}

5. When testing the APK, it needs to be re-signed and then installed in order to test 6. How to configure Robotium help tips

Right-click on Project-build path-configure Build Path

7.robotium different versions of different methods

Robotium's Getcurrentlistviews
1. Expressions for version 3.6 and version 4.1

Version 3.6:arraylist<listview> LW = Solo.getcurrentlistviews ();

4.1 versions above:arraylist<listview> LW = Solo.getcurrentviews (Listview.class);

It also resembles:

Arraylist<imageview> imagelist=solo.getcurrentviews (Imageview.class); ImageView is what you get.
Arraylist<imageview> imagelist=solo.getcurrentviews (Imageview.class,parentview);

8. How to get control id-two ways

(1) Android Utility Hierarchy Viewer Combat

    • is a tool released with ANDROIDSDK, located under the Tools folder, named Hierarchyviewer.bat
    • You need to run a test project to detect the emulator in a debug environment

(2) run the command line record log, then click on the corresponding activity, then you can see in the Logcat

Robotium Automated Testing Framework Practical tutorial (figure)

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.