Android monkey test 脚本的编写


Instrumentation 是google开发的Android测试框架(http://developer.android.com/reference/android/test/InstrumentationTestRunner.html)

主要分为下列项目:

  • ActivityInstrumentationTestCase2

  • ActivityUnitTestCase

  • AndroidTestCase

  • ApplicationTestCase

  • InstrumentationTestCase

  • ProviderTestCase

  • ServiceTestCase

  • SingleLaunchActivityTestCase


AndroidTestCase 主要来测试相关非交互性API,比如数据库,内容提供者等,其优点是可以通过getContext获取上下文

publicclassTestAudioextendsAndroidTestCase{privateAudioManagermAudioManager;privatebooleanmUseFixedVolume;privatefinalstaticlongTIME_TO_PLAY=2000;privatefinalstaticintMP3_TO_PLAY=R.raw.testmp3;privateContextmContext;@OverrideprotectedvoidsetUp()throwsException{//TODOAuto-generatedmethodstubsuper.setUp();mContext=getContext();}publicvoidtestmp3(){MediaPlayermp=MediaPlayer.create(mContext,MP3_TO_PLAY);mp.setAudioStreamType(STREAM_MUSIC);mp.setLooping(true);mp.start();try{Thread.sleep(20*1000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}


ActivityInstrumentationTestCase2,SingleLaunchActivityTestCase,ActivityUnitTestCase主要来测试Activity相关API,方便之处是可以直接获取Activity

publicclassAdminMainTestextendsActivityInstrumentationTestCase2<MainActivity>{privateMainActivitymActivity;privateInstrumentationmInstrumentation;privateButtonlogin;privateEditTextaccount;privateEditTextpassword;privateRadioGroupradioGroup;//privateRadioButtonbutton;privateRadioButtonbutton1;privateRadioButtonbutton2;privateRadioButtonbutton3;privateRadioButtonbutton4;privateContextmContext;privateViewbuttonView;publicAdminMainTest(){super(MainActivity.class);//目标Activity}@BeforeprotectedvoidsetUp()throwsException{super.setUp();setActivityInitialTouchMode(false);mInstrumentation=getInstrumentation();mContext=mInstrumentation.getContext();mActivity=getActivity();login=(Button)mActivity.findViewById(com.example.example.R.id.landed);account=(EditText)mActivity.findViewById(com.example.example.R.id.landed_account);password=(EditText)mActivity.findViewById(com.example.example.R.id.landed_password);radioGroup=(RadioGroup)mActivity.findViewById(R.id.landed_user_type);button1=(RadioButton)mActivity.findViewById(R.id.landed_user_type_admin);button2=(RadioButton)mActivity.findViewById(R.id.landed_user_type_publisher);button3=(RadioButton)mActivity.findViewById(R.id.landed_user_type_common);button4=(RadioButton)mActivity.findViewById(R.id.landed_user_type_visitor);}@AfterprotectedvoidtearDown()throwsException{mActivity.finish();super.tearDown();}publicvoidtestPreConditions(){assertNotNull(mActivity);assertNotNull(login);assertNotNull(account);assertNotNull(password);assertNotNull(radioGroup);assertNotNull(button1);assertNotNull(button2);assertNotNull(button3);assertNotNull(button4);}publicvoidinput(){mActivity.runOnUiThread(newRunnable(){@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubSystemClock.sleep(1500);account.requestFocus();SystemClock.sleep(1500);account.performClick();//SystemClock.sleep(3000);}});mInstrumentation.waitForIdleSync();sendKeys(KeyEvent.KEYCODE_S,KeyEvent.KEYCODE_O,KeyEvent.KEYCODE_N,KeyEvent.KEYCODE_G);mActivity.runOnUiThread(newRunnable(){@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubSystemClock.sleep(1500);password.requestFocus();SystemClock.sleep(1500);password.performClick();}});mInstrumentation.waitForIdleSync();sendKeys(KeyEvent.KEYCODE_S,KeyEvent.KEYCODE_O,KeyEvent.KEYCODE_N,KeyEvent.KEYCODE_G);mInstrumentation.waitForIdleSync();}publicvoidtestFirstRadioButton(){assertTrue("TheAdminbuttonischecked",button1.isChecked());//assertEquals("商品发布者",button.getText().toString());assertEquals(R.id.landed_user_type_admin,radioGroup.getCheckedRadioButtonId());}publicvoidtestSecondRadioButton(){//assertTrue("ThePublisherbuttonischecked",button2.isChecked());//assertEquals(R.id.landed_user_type_publisher,radioGroup.getCheckedRadioButtonId());assertEquals("商品发布者",button2.getText().toString());}publicvoidtestThirdRadioButton(){//assertTrue("Thecommonuserischecked",button3.isChecked());//assertEquals(R.id.landed_user_type_common,radioGroup.getCheckedRadioButtonId());assertEquals("普通用户",button3.getText().toString());}publicvoidtestFourthRadioButton(){//assertTrue("Theguestischecked",button4.isChecked());//assertEquals(R.id.landed_user_type_visitor,radioGroup.getCheckedRadioButtonId());assertEquals("访客",button4.getText().toString());}publicvoidtestButton2Selection(){testFirstRadioButton();TouchUtils.clickView(this,button2);//assertFalse("Theadminradiobuttonshouldnotbechecked",button1.isChecked());assertTrue("Thepublisherradiobuttonshouldbechecked",button2.isChecked());assertEquals("Thepublisherbuttonshouldbechecked",R.id.landed_user_type_publisher,radioGroup.getCheckedRadioButtonId());}publicvoidtestButton3Selection(){testFirstRadioButton();TouchUtils.clickView(this,button3);assertTrue("Thecommonuserischecked",button3.isChecked());assertEquals(R.id.landed_user_type_common,radioGroup.getCheckedRadioButtonId());}publicvoidtestButton4Selection(){testFirstRadioButton();TouchUtils.clickView(this,button4);assertTrue("Theguestischecked",button4.isChecked());assertEquals(R.id.landed_user_type_visitor,radioGroup.getCheckedRadioButtonId());}/**publicvoidtestRadioButtonChange(){testFirstRadioButton();TouchUtils.clickView(this,button);assertFalse("Theadminradiobuttonshouldnotbechecked",button1.isChecked());assertTrue("Thepublisherbuttonshouldbechecked",button.isChecked());assertEquals("Thepublisherbuttonshouldbechecked",R.id.landed_user_type_publisher,radioGroup.getCheckedRadioButtonId());}**/publicvoidtestInput(){input();assertEquals("song",account.getText().toString());assertEquals("song",password.getText().toString());}@TestpublicvoidtestLogin(){input();//testRadioButtonChange();mInstrumentation.runOnMainSync(newRunnable(){@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubSystemClock.sleep(1500);login.requestFocus();SystemClock.sleep(1500);login.performClick();}});}}

ServiceTestCase专门用来测试Service服务

publicclassMyServiceTestextendsServiceTestCase<MyService>{privateStringTAG="myservicetest";privateContextmContext;/***构造方法*/publicMyServiceTest(){super(MyService.class);}/***重写setUp方法,第一句调用super.setUp*/protectedvoidsetUp()throwsException{super.setUp();mContext=getContext();}//publicvoidtestAndroidTestCaseSetupProperly(){//super.testAndroidTestCaseSetupProperly();//}protectedvoidtearDown()throwsException{mContext=null;super.tearDown();}/***测试Service正确地启动*/publicvoidtestStart(){Log.i(TAG,"starttestStart");Intentintent=newIntent();startService(intent);MyServiceServ=getService();assertNotNull(Serv);Log.i(TAG,"endtestStart");}}/***测试Service正确的终止*/publicvoidteststop(){Log.i(TAG,"startteststopService");Intentintent=newIntent();startService(intent);MyServiceservice=getService();service.stopService(intent);}}

InstrumentationTestCase 相对于Activity,Service等测试,相对而言,比较灵活,其他测试很多都是继承自这个

publicclassTestHelloActiviryextendsInstrumentationTestCase{finalStringTAG="TestHelloAppTestHelloApp";ButtonmHelloTestButton;EditTextmHelloEditText;HelloActivitymHelloTestActivity;InstrumentationmInstrumentation;publicvoidtestHelloActivity(){Log.i(TAG,"calltestHelloActivity()");mHelloTestButton=(Button)mHelloTestActivity.findViewById(R.id.Button1);mHelloEditText=(EditText)mHelloTestActivity.findViewById(R.id.EditText1);for(inti=0;i<3;i++){//设置事件在主线程中执行mInstrumentation.runOnMainSync(newClick(mHelloTestButton,mHelloEditText,Integer.toString(i)));SystemClock.sleep(3000);}}publicvoidtestHelloActivity2(){}privateclassClickimplementsRunnable{Buttonbutton;EditTexteditText;Stringstr;Click(Buttonb,EditTexte,Strings){button=b;editText=e;str=s;}@Overridepublicvoidrun(){editText.setText(str);button.performClick();}}//负责testcase开始前的初始化工作@OverrideprotectedvoidsetUp()throwsException{super.setUp();Log.i(TAG,"callsetUp()");mInstrumentation=getInstrumentation();Intentintent=newIntent();intent.setClassName("com.example.hello","com.example.hello.HelloActivity");intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//通过intent触发activitymHelloTestActivity=(HelloActivity)mInstrumentation.startActivitySync(intent);}@OverrideprotectedvoidtearDown()throwsException{super.tearDown();Log.i(TAG,"tearDown()");}}


参考

Android自动化测试初探(二): Hierarchyviewer 捕获Element的实现原理


更多相关文章

  1. 2010.10.31———Android(安卓)04
  2. Get the Android(安卓)SDK---获取Android(安卓)SDK
  3. 2010.10.31———Android(安卓)04
  4. android ksoap2 访问https javax.net.ssl.SSLHandshakeException
  5. android4.2上获取应用程序大小的变更点
  6. Android(安卓)从uri中获取路径
  7. android4.2上获取应用程序大小的变更点
  8. android 通过 button 弹出 option menu 和 context menu
  9. Fragment总结

随机推荐

  1. 待续
  2. [原]Android应用程序发送广播(sendBroadca
  3. Android动态设置Margin的方法
  4. [转]Android Recovery模式
  5. Android SDK Manager 更新时的“https://
  6. Android 反汇编Smali语言中插入log打印
  7. android-下载 gradle很慢的解决办法
  8. 一些记号贴(不完整仅入入门)
  9. android 控件颜色随焦点变化实例
  10. 使用maven构建基于spring、springMVC的简