Android自动化大讲堂33--Instrumentation工具反思

先上完整自动化测试项目代码,如代码清单4-45所示。


代码清单4-45     完整自动化测试项目

package com.xuben.hellobugben.test;


import com.xuben.hellobugben.ChangeActivity;

import com.xuben.hellobugben.HelloBugbenActivity;

import com.xuben.hellobugben.R;


import android.app.Instrumentation.ActivityMonitor;

import android.content.Intent;

import android.os.SystemClock;

import android.test.ActivityInstrumentationTestCase2;

import android.text.TextPaint;

import android.util.Log;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.TextView;


public class HelloBugbenTestBase extends ActivityInstrumentationTestCase2<ChangeActivity> {

public HelloBugbenTestBase() {

super(ChangeActivity.class);

// TODO Auto-generated constructor stub

}

ChangeActivity changeAutoTest;

HelloBugbenActivity helloBugbenAutoTest;

private EditText txt1;

private EditText txt2;

private RadioButton bold;

private RadioButton notbold;

private RadioButton small;

private RadioButton big;

private Button subButton;

private TextView textview1;

private TextView textview2;

// xuben:输入值

String bugben_txt1 = “巴哥奔”;

String bugben_txt2 = “小简洁”;

Boolean bugben_bold = true;

Boolean bugben_notbold = false;

Float bugben_small_size = (float) 20.0;

Float bugben_big_size = (float) 60.0;

  @Override

public void setUp() throws Exception{

super.setUp();


// xuben:启动ChangeActivity

Intent intent = new Intent();

intent.setClassName(“com.xuben.hellobugben”, ChangeActivity.class.getName());

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

changeAutoTest =(ChangeActivity)getInstrumentation().startActivitySync(intent);


// xuben:通过changeAutoTest的findViewById获取ChangeActivity界面控

txt1 = (EditText)changeAutoTest.findViewById(R.id.txt1);

txt2 = (EditText)changeAutoTest.findViewById(R.id.txt2);

bold = (RadioButton)changeAutoTest.findViewById(R.id.bold);

notbold = (RadioButton)changeAutoTest.findViewById(R.id.notbold);

small = (RadioButton)changeAutoTest.findViewById(R.id.small);

big = (RadioButton)changeAutoTest.findViewById(R.id.big);

subButton = (Button)changeAutoTest.findViewById(R.id.myButton01);

}


@Override

public void tearDown() throws Exception{

super.tearDown();

}

 

// xuben:基本提交测试

public void testSubmit() throws Throwable {

// xuben:通过log表示运行的是哪一个自动化用例

Log.v(“testSubmit”, “test normal submit.”);


// xuben:添加一个监视器,监视HelloBugbenActivity的启动

ActivityMonitor bugbenMonitor = getInstrumentation()

.addMonitor(HelloBugbenActivity.class.getName(), null, false);


// xuben:要操作待测程序的UI必须在runTestOnUiThread()中执行

runTestOnUiThread(new Runnable() {

@Override

public void run()

// xuben:编辑界面中的文本框中文字

txt1.setText(bugben_txt1);

txt2.setText(bugben_txt2);


// xuben:选择文本框1为加粗,文本框2为大号字体

bold.setChecked(true);

big.setChecked(true);


// xuben:等待500ms以避免程序响应慢出错

SystemClock.sleep(500);


// xuben:点击subButton按钮,提交输入文本

subButton.performClick();

}

});


// xuben:从ActivityMonitor监视器中获取HelloBugbenActivity的实例

helloBugbenAutoTest = (HelloBugbenActivity) getInstrumentation()

.waitForMonitor(bugbenMonitor);


// xuben:HelloBugbenActivity的实例helloBugbenAutoTest应不为空

assertTrue(helloBugbenAutoTest != null); 


// xuben:通过helloBugbenAutoTest的findViewById获取文本框

textview1 =(TextView)helloBugbenAutoTest.findViewById(R.id.myTextView01);

textview2 =(TextView)helloBugbenAutoTest.findViewById(R.id.myTextView02);


// xuben:验证文本框1的文本,该文本应为”巴哥奔”

assertEquals(bugben_txt1, textview1.getText().toString());

// xuben:验证本框2的文本,该文本应为”小简洁”

assertEquals(bugben_txt2, textview2.getText().toString());


// xuben:验证文本框1的文本属性,应为加粗

TextPaint tp = textview1.getPaint(); 

Boolean cmpBold = tp.isFakeBoldText();  

assertTrue(cmpBold);


// xuben:验证本框2的文本字号,应为大号

Float cmpSize = textview2.getTextSize();

assertTrue(cmpSize.compareTo(bugben_big_size) == 0);

}  

}


运行结果如图4-32所示。

0?wx_fmt=png  

图4-32 运行成功 


备注:

事实上,真实场景测试至少得分别写四个独立的自动化用例(分别测试文本框1的文字,文本框2文字,文本框1属性,文本框2字号),但这四个用例绝大部分代码雷同,所以此处为了节省大家时间,就写到一起了——这样的弊端在于,如果其中一个用例的验证(assert)挂掉(fail)了,结果显示整个测试用例fail

 0?wx_fmt=png


4.10  Instrumentation工具反思

从上节的例子不难看出,Instrumentation框架运行流程,如图4-33所示。 

0?wx_fmt=png

图4-33 Instrumentation框架运行流程


更直观的表述如图4-34所示。

0?wx_fmt=png图4-34 Instrumentation运行


0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

0?wx_fmt=png

 

更多内容,请点击“阅读原文”,参考《深入理解Android自动化测试》一书,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值