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. Android:Manifest merger failed with multiple errors, see log
  2. android中真正destroy掉activity的方法
  3. Android旋转屏幕不销毁数据的方法
  4. Android程序设置成横屏方法
  5. android 开发中将十六进制 颜色代码 转换为int类型数值 方法 :
  6. 详解Android读取本地图片和网络图片的方法
  7. android onSaveInstanceState的使用方法
  8. android获取sd卡路径方法:
  9. 对Android应用进行单元测试

随机推荐

  1. 用TextView自定义按钮
  2. Ubuntu-18.04 LTS配置android系统源码编
  3. Could not find com.android.tools.build
  4. Android(安卓)Fresco图片处理库用法API英
  5. [Android]设置Activity为全屏显示的两种
  6. Android(安卓)- 折线图
  7. Android井字棋
  8. js检测手机类型(android,ios,blackberry,w
  9. Android(安卓)屏幕滑动事件
  10. 用android:clipChildren来实现红心变大特