Android Test - Auto Test Multi Activities

本文介绍了一个简单的Android应用及其测试项目的创建过程。通过实例演示了如何使用Instrumentation进行UI测试,包括自动输入用户名、密码及触发按钮点击等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android SDK中“Resources”-“Tutorials”下有“Notepad Tutorial”和“Activity Testing”两个项目,一个示例是指导你如何快速开发一个Android小程序,一个是指导你如何对项目进行测试,两个项目都适合在入门的时候好好学习。

其中的“Activity Testing”是对“Samples”-“Spinner”项目进行测试,其中包含了UI测试、状态破坏和状态恢复测试。这个项目只有一个Activity,测试起来也不麻烦,细心阅读文档就可以完成。但是一个程序只有一个Activity应该是很难遇见的吧,那么应该对多活动(Multi Activities)的程序进行测试呢?
其实我这也是随便整整,大家随便看看。

[size=large]在查看SDK关于测试的章节后,有疑问如下:
测试Activity、Service、Provider都是自动化的,那么我们如何控制运行过程?
如何在界面模拟操作,如点击按钮,输入文字内容等等。
[/size]

新建一个项目,项目名为Login,包名为com.vruc.android.login,程序名为Login,活动名为AuthenticateActivity;同时添加一个项目名为LoginTest,包名为com.vruc.android.login.test,程序名为LoginTest的测试项目。

完整的Login项目:
1.更改main.xml文件名为login.xml,更改代码为下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:id="@+id/username_field"
android:layout_height="wrap_content" android:layout_width="match_parent"></EditText>
<EditText android:id="@+id/password_field"
android:layout_height="wrap_content" android:layout_width="match_parent"></EditText>
<Button android:text="Login" android:id="@+id/login_button"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

2.打开AuthenticateActivity.java文件,为“@+id/login_button”添加点击事件,具体代码就是向WelcomeActivity传递当前“@+id/username_field”中的输入文字并结束当前activity,具体代码为下:

public class AuthenticateActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button login = (Button) findViewById(R.id.login_button);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AuthenticateActivity.this,WelcomeActivity.class);
i.putExtra(ACCOUNT_SERVICE,((EditText) findViewById(R.id.username_field)).getText().toString());
startActivity(i);
finish();
}
});
}
}

3.在layout目录下添加新文件welcome.xml,更改代码为下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/welcome_message"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="15pt"></TextView>
</LinearLayout>

4.添加新的WelcomeActivity.java文件并在AndroidMainifest.xml中注册,重写onCreate事件,具体代码就是为“@+id/welcome_message”赋值,从LoginActivity中传递过来的“@+id/username_field”的值,具体代码为下:

public class WelcomeActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
Intent i = this.getIntent();
((TextView)findViewById(R.id.welcome_message)).setText(i.getStringExtra(ACCOUNT_SERVICE));
}


现在可以运行一下Login项目,可以发现填写在“@+id/username_field”中的文字在点击“@+id/login_button”按钮后出现在了WelcomeActivity中。

完整的LoginTest项目
1.添加LoginTest.java文件,继承类为android.test.InstrumentationTestCase
2.完整LoginTest.java中测试代码:
	public static final String TEST_USERNAME = "TEST_USERNAME";
public static final String TEST_PASSWORD = "TEST_PASSWORD";

public void testUserLogin() {

// 注册最开始的活动并运行
Instrumentation instrumentation = getInstrumentation();
ActivityMonitor monitor = instrumentation.addMonitor(
AuthenticateActivity.class.getName(), null, false);

// 运行活动
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(instrumentation.getTargetContext(), AuthenticateActivity.class.getName());
instrumentation.startActivitySync(intent);

// 等待Authenticate活动开始
Activity currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);
assertTrue(currentActivity != null);

// 自动输入预定的用户名
View currentView = currentActivity.findViewById(com.vruc.android.login.R.id.username_field);
assertTrue(currentView != null);
TouchUtils.clickView(this, currentView);
instrumentation.sendStringSync(TEST_USERNAME);

// 自动输入预定的密码
currentView = currentActivity.findViewById(com.vruc.android.login.R.id.password_field);
assertTrue(currentView != null);
TouchUtils.clickView(this, currentView);
instrumentation.sendStringSync(TEST_PASSWORD);

// 移除当前活动监视,注册新的活动监视,要在还没有按下按钮前准备
instrumentation.removeMonitor(monitor);
monitor = instrumentation.addMonitor(WelcomeActivity.class.getName(), null, false);

// 自动点击登陆按钮
currentView = currentActivity.findViewById(com.vruc.android.login.R.id.login_button);
assertTrue(currentView != null);
TouchUtils.clickView(this, currentView);

// 等待Welcome活动开始
currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);

currentView = currentActivity.findViewById(com.vruc.android.login.R.id.welcome_message);
assertTrue(currentView != null);
assertEquals(TEST_USERNAME, ((TextView) currentView).getText().toString());

}


[size=large]运行测试程序后可以发现testUserLogin顺利通过,也可以在模拟器查看具体的运行过程,就像你新手操作模拟器一般。[/size]

接下来解释下“Instrumentation”类,未完待续。。。。。。。。
内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值