安卓自动化单元测试——登录模块测试

安卓自动化测试

参考资料:Android自动化测试-从入门到入门
构建之法上说单元测试要集成到自动化框架中,要和产品代码一起保存和维护,不是很明白自动化框架是什么。

1.测试准备

  • build.gradle配置


android {   
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//为工程指定一个TestInstrumentationRunner,TestInstrumentationRunner是用来跑所写的所有的测试用例的。
}
}
//还需要在build.gradle中增加instrumentation testing所需要的依赖:
dependencies {
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
}
  • 为了实现自动化测试流程,采用Espresso进行脚本的编写。需要在build.gradle的dependencies中增加如下依赖:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
  • 另外,还需要一个叫做hamcrest的库,用来和Espresso配合使用,因此在build.gradle中添加:
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'

2.Test Case


@RunWith(AndroidJUnit4.class)
@LargeTest
public class RegisterActivityTest extends ActivityInstrumentationTestCase2 {
private Instrumentation mInstrumentation;
private RegisterActivity mRegisterActivity;
private MaterialEditText mPhonumEditText;
private MaterialEditText mSecretEditText;
private MaterialEditText mCheckSecEditText;
private MaterialEditText mCodeEdt;
private Button mGetCodeBtn;
private Button mSubmitBtn;

public RegisterActivityTest()
{

super(RegisterActivity.class);
}

//初始化 @Before
public void setUp() throws Exception {

super.setUp();
// @Before注解表示在执行所有的testCase之前要做的事情

injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mInstrumentation = getInstrumentation();
// getActivity()方法会在开始所有的testCase之前启动相应的Activity

mRegisterActivity= getActivity();
mPhonumEditText=(MaterialEditText)mRegisterActivity.findViewById(R.id.register_phonenum);
mSecretEditText=(MaterialEditText)mRegisterActivity.findViewById(R.id.register_newsecret);
mCheckSecEditText=(MaterialEditText)mRegisterActivity.findViewById(R.id.register_checksecret);
mCodeEdt = (MaterialEditText) mRegisterActivity.findViewById(R.id.register_checknum);
mGetCodeBtn = (Button)mRegisterActivity.findViewById(R.id.register_get_checknum);
mSubmitBtn = (Button) mRegisterActivity.findViewById(R.id.register_submit);

}


//键盘输入
public void input()
{
mRegisterActivity.runOnUiThread(new Runnable()
{
@Override
public void run()
{

// 自动化
mPhonumEditText.requestFocus();
mPhonumEditText.performClick();
}
});
/*由于测试用例在单独的线程上执行,所以此处需要同步application,
* 调用waitForIdleSync等待测试线程和UI线程同步,才能进行输入操作。
* waitForIdleSync和sendKeys不允许在UI线程里运行
*/

mInstrumentation.waitForIdleSync();

//调用sendKeys方法,输入用户名
sendKeys(KeyEvent.KEYCODE_2, KeyEvent.KEYCODE_8,
KeyEvent.KEYCODE_2, KeyEvent.KEYCODE_5,
KeyEvent.KEYCODE_9, KeyEvent.KEYCODE_0,
KeyEvent.KEYCODE_5,KeyEvent.KEYCODE_8,
KeyEvent.KEYCODE_7,KeyEvent.KEYCODE_3,
KeyEvent.KEYCODE_3
);

mRegisterActivity.runOnUiThread(new Runnable()
{

@Override
public void run()
{
// TODO Auto-generated method stub
mSecretEditText.requestFocus();
mSecretEditText.performClick();
}
});

//调用sendKeys方法,输入密码
sendKeys(KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2,
KeyEvent.KEYCODE_3, KeyEvent.KEYCODE_4,
KeyEvent.KEYCODE_5);
mRegisterActivity.runOnUiThread(new Runnable()
{

@Override
public void run()
{ // TODO Auto-generated method stub
mCheckSecEditText.requestFocus();
mCheckSecEditText.performClick();
}
});
//调用sendKeys方法,输入和密码一致的验证码
//调用sendKeys方法,输入和密码不一致的验证码
sendKeys(KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2,
KeyEvent.KEYCODE_3, KeyEvent.KEYCODE_4,
KeyEvent.KEYCODE_5);
}


//登录及获取验证码测试
@Test
public void BtnTest()
{
//3.输入错误格式的帐号
//2.输入帐号的情况下获取验证码
//4.输入6位数的密码
input();
//1.未输入帐号的情况下点击获取验证码
//5.输入不一致密码提交数据
onView(withId(R.id.register_get_checknum)).perform(click());
onView(withId(R.id.register_submit)).perform(click());
}


@Test
public void mSubmitBtnTest()
{
//未输入帐号的情况下点击注册按钮
onView(withId(R.id.register_submit)).perform(click());
}
}


 //测试输入的用户信息
@Test
public void testInput()
{
//调用测试类的input方法,实现输入用户信息(sendKeys实现输入)
input();
//测试验证用户信息的预期值是否等于实际值
assertEquals("28259058733", usernameTv.getText().toString());
//密码的预期值123456与实际值12345不符,Failure;
assertEquals("123456", passwordTv.getText().toString());
}

 //登录按钮测试
@Test
public void loginTest(){
onView(withId(R.id.login)).perform(click()
);
}


//忘记密码按钮测试
@Test
public void forgotpasswordTest(){
onView(withId( R.id.forget_password)).perform(click());
}

//注册按钮测试
@Test
public void sign_inTest(){
onView(withId( R.id.sign_in)).perform(click());
}

posted @ 2016-12-09 23:50  ifelif  阅读(895)  评论(0编辑  收藏  举报
TOP