使用Robotium编写测试程序,按照下面的测试步骤:
 
1.获得目标应用程序的package 名称和主Activity
     如 Msm.apk, package 名称是“com.android.mms”, 主Activity是“com.android.mms.ui.ConversationList”,可以打开logcat, 同时在手机上打开应用程序,应用的包名和package即可在logcat里面显示出来。
 
2.修改目标应用程序的签名和测试程序一致。
     可以修改测试程序的签名,也可以修改目标应用程序的签名。后一种的实现比较简单,使用re-sign.jar包。运行 “java -jar re-sign.jar”命令打开修改签名的程序,然后将目标apk拖至re-sign 应用。如果目标应用程序是系统应用,按照下面的步骤
     a). 运行adb pull /system/app/Mms.apk 获取Mms.apk
     b). 采用上面的方法修改Mms.apk的签名,将改过签名的应用程序的Mms.apk push回手机
     c). 运行 adb pull /data/system/packages.xml ,将要测试的应用程序的标签删除,然后push回手机

 

3.编写robotium的case
      在编写测试case之前,将robotium-solo.jar的导入应用程序。需要在测试工程目录下新建libs文件夹,将robotium-solo.jar拷入libs文件夹,设置buildpath将库导入工程。否则运行测试程序,会报出Error, 
  1. java.lang.NoClassDefFoundError: com.jayway.android.robotium.solo.Solo 
  2. at com.example.RobotiumTest.TestMsm.setUp(TestMsm.java:41
  3. at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190
  4. at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175
  5. at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555
  6. at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584

 

4.Sample code
AndroidManifest.xml
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.example.RobotiumTest" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <uses-sdk android:minSdkVersion="10" /> 
  7.     <instrumentation android:targetPackage="com.android.mms" android:name="android.test.InstrumentationTestRunner" /> 
  8.     <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> 
  9.  
  10.     <uses-library android:name="android.test.runner" /> 
  11.     </application> 
  12. </manifest> 

 

Test class: TestMsm
  1. package com.example.RobotiumTest; 
  2. import android.test.ActivityInstrumentationTestCase2; 
  3. import com.jayway.android.robotium.solo.Solo; 
  4.  
  5. public class TestMsm extends ActivityInstrumentationTestCase2{ 
  6.  
  7.      private Solo solo; 
  8.      private static final String TARGET_PACKAGE_ID = "com.android.mms"
  9.      private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.android.mms.ui.ConversationList"
  10.  
  11.      private static Class<?> launcherActivityClass; 
  12.       
  13.      static
  14.          try
  15.              launcherActivityClass= Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);  
  16.          }catch(ClassNotFoundException e){ 
  17.             throw new RuntimeException(e);  
  18.          } 
  19.      } 
  20.       
  21.      
  22.     public TestMsm() throws ClassNotFoundException{ 
  23.          super(TARGET_PACKAGE_ID,launcherActivityClass); 
  24.      } 
  25.       
  26.       
  27.       public void setUp()throws Exception { 
  28.         solo=new Solo(getInstrumentation(), getActivity()); 
  29.            } 
  30.             
  31.     public void tearDown()throws Exception{ 
  32.          try
  33.              solo.finalize(); 
  34.          }catch(Throwable e){ 
  35.              e.printStackTrace(); 
  36.          } 
  37.          getActivity().finish(); 
  38.          super.tearDown(); 
  39.      } 
  40.       
  41.      public void testSendSMS(){ 
  42.         try
  43.              
  44.             solo.clickOnScreen(50,770 ); 
  45.             solo.enterText(0"10010"); 
  46.             solo.enterText(1"test"); 
  47.              
  48.             solo.clickOnScreen(430,770 ); 
  49.             solo.sleep(2000); 
  50.              
  51.             solo.goBack(); 
  52.             solo.waitForDialogToClose(1000); 
  53.              
  54.         }catch(Exception e){ 
  55.             System.out.println("Exception captured!"); 
  56.             e.printStackTrace(); 
  57.         }         
  58.      } 

参考:http://code.google.com/p/robotium/