自动化测试框架(三) 简单实例 - 登录场景

Posted by boredream on December 22, 2015

网上资料大部分为环境搭建,即怎么写,但更主要的是写什么,这对于开发来说可能会一头雾水 虽然实际工作中,不同业务代码和测试重点因人而异,但还是可以找到共通点的, 这里我们以应用中的经典场景为例,比如登录/注册等….挨个列出实例 (个人经验,仅供参考~)

写什么 比较简单的方法是根据测试用例进行编写, 所谓测试用例就是在进行具体测试之前,把场景中所有可能的操作和期望的结果都列出来, 然后再进行具体测试(人工或者我们这里用代码方式), 看程序实际运行中执行对应动作时是否为期望结果

比如下面这种简单的登录测试用例 espresso4

最终编写的代码就可以如下,5个test方法对应5个测试用例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.boredream.androidespressotest ;

import android.content.Intent ;
import android.support.test.rule.ActivityTestRule ;
import android.support.test.runner.AndroidJUnit4 ;

import org.junit.Rule ;
import org.junit.Test ;
import org.junit.runner.RunWith ;

import static android.support.test.espresso.Espresso. onView;
import static android.support.test.espresso.action.ViewActions.click ;
import static android.support.test.espresso.action.ViewActions. closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions. typeText;
import static android.support.test.espresso.assertion.ViewAssertions. matches;
import static android.support.test.espresso.matcher.RootMatchers. withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers. isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId ;
import static android.support.test.espresso.matcher.ViewMatchers.withText ;
import static org.hamcrest.core.Is.is ;
import static org.hamcrest.core.IsNot.not ;


@RunWith(AndroidJUnit4.class )
public class LoginActivityTest {

    // ---------------------------    登录测试用例   ---------------------------
    //
    //        编号      输入/动作                            期望的输出/相应
    //        1         使用合法用户名和密码登陆              登陆成功,进入主页
    //        2         使用错误的用户名或密码登陆            显示用户名或密码错误提示信息
    //        3         用户名为空登陆                       显示请输入用户名提示信息
    //        4         密码为空进行登陆                      显示请输入密码提示信息
    //        5         用户名和密码都为空进行登陆            显示请输入用户名提示信息(由上到下以依次判断)

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(LoginActivity. class, true, false);

    @Test
    public void test1() {
        Intent intent = new Intent();
        mActivityRule .launchActivity(intent);

        onView(withId (R.id.et_username)).perform( typeText( "boredream"), closeSoftKeyboard()) ;
        onView(withId (R.id.et_password)).perform( typeText( "123456"), closeSoftKeyboard()) ;
        onView(withId (R.id.btn_login)).perform( click()) ;

        onView(withId (R.id.tv_title))
                 .check(matches(withText ("主页" )));
    }

    @Test
    public void test2() throws InterruptedException {
        Intent intent = new Intent();
        mActivityRule .launchActivity(intent);

        onView(withId (R.id.et_username)).perform( typeText( "boredream"), closeSoftKeyboard()) ;
        onView(withId (R.id.et_password)).perform( typeText( "111111"), closeSoftKeyboard()) ;
        onView(withId (R.id.btn_login)).perform( click()) ;

        onView(withText ("用户名或密码错误" ))
                 .inRoot(withDecorView( not(is(mActivityRule .getActivity().getWindow().getDecorView()))))
                 .check(matches(isDisplayed ()));
    }

    @Test
    public void test3() throws InterruptedException {
        Intent intent = new Intent();
        mActivityRule .launchActivity(intent);

        onView(withId (R.id.et_username)).perform( typeText( ""), closeSoftKeyboard()) ;
        onView(withId (R.id.et_password)).perform( typeText( "123456"), closeSoftKeyboard()) ;
        onView(withId (R.id.btn_login)).perform( click()) ;

        Thread. sleep( 1000);

        onView(withText ("请输入用户名" ))
                 .inRoot(withDecorView( not(is(mActivityRule .getActivity().getWindow().getDecorView()))))
                 .check(matches(isDisplayed ()));
    }

    @Test
    public void test4() throws InterruptedException {
        Intent intent = new Intent();
        mActivityRule .launchActivity(intent);

        onView(withId (R.id.et_username)).perform( typeText( "boredream"), closeSoftKeyboard()) ;
        onView(withId (R.id.et_password)).perform( typeText( ""), closeSoftKeyboard()) ;
        onView(withId (R.id.btn_login)).perform( click()) ;

        Thread. sleep( 1000);

        onView(withText ("请输入密码" ))
                 .inRoot(withDecorView( not(is(mActivityRule .getActivity().getWindow().getDecorView()))))
                 .check(matches(isDisplayed ()));
    }

    @Test
    public void test5() throws InterruptedException {
        Intent intent = new Intent();
        mActivityRule .launchActivity(intent);

        onView(withId (R.id.et_username)).perform( typeText( ""), closeSoftKeyboard()) ;
        onView(withId (R.id.et_password)).perform( typeText( ""), closeSoftKeyboard()) ;
        onView(withId (R.id.btn_login)).perform( click()) ;

        Thread. sleep( 1000);

        onView(withText ("请输入用户名" ))
                 .inRoot(withDecorView( not(is(mActivityRule .getActivity().getWindow().getDecorView()))))
                 .check(matches(isDisplayed ()));
    }

}

注意:

  1. 这里判断是否进入主页是获取标题栏控件,然后判断内容是否为主页,这种check是夸页面判断的,无需任何处理~ 直接挨个写语句判断即可
  2. 由于多个测试方法一起执行,前一个toast会停留一段时间然后对后面的测试代码判断造成影响,因此每个验证之前都特意等待了1000毫秒, 这个Sleep不是为了等待请求数据回来而设的,是处理Toast问题的, 如果大家有更好的方法也可以回复告诉我下 3ks~

下面是功能代码,即待测试的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.boredream.androidespressotest ;

import android.app.ProgressDialog ;
import android.content.Intent ;
import android.os.AsyncTask ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.text.TextUtils ;
import android.view.View ;
import android.widget.Button ;
import android.widget.EditText ;
import android.widget.Toast ;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private Button btn_login;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super .onCreate(savedInstanceState);
        setContentView(R.layout. activity_login);

        initView() ;
    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.et_username) ;
        et_password = (EditText) findViewById(R.id.et_password) ;
        btn_login = (Button) findViewById(R.id.btn_login) ;
        progressDialog = new ProgressDialog( this);
        progressDialog .setTitle("登录中...");

        btn_login .setOnClickListener(this) ;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
        }
    }

    /**
     * 发起登录操作
     */
    private void login() {
        // 验证密码
        String password = et_password.getText().toString().trim();
        if(TextUtils. isEmpty(password)) {
            Toast.makeText (this, "请输入密码" , Toast.LENGTH_SHORT).show() ;
            return;
        }

        // 验证用户名
        String username = et_username.getText().toString().trim();
        if(TextUtils. isEmpty(username)) {
            Toast.makeText (this, "请输入用户名" , Toast.LENGTH_SHORT).show() ;
            return;
        }

        // 验证通过
        requestLogin(username , password);
    }

    /**
     * 调用服务器登录接口(此处为模拟)
     */
    private void requestLogin(final String username, final String password) {
        new AsyncTask<Void, Void , Void>() {

            @Override
            protected void onPreExecute() {
                // 发起请求
                progressDialog.show() ;
            }

            @Override
            protected Void doInBackground(Void... params) {
                // 请求ing...
                try {
                    Thread.sleep(1000 );
                } catch (InterruptedException e) {
                    e.printStackTrace() ;
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                // 请求返回后
                progressDialog.dismiss() ;

                // 这里简单的判断下模拟服务器的账号处理
                if (! username.equals("boredream" ) || !password.equals( "123456")) {
                    showToast( "用户名或密码错误" );
                    return;
                }

                // 登录成功,跳转到主页
                Intent intent = new Intent(LoginActivity.this, MainActivity. class);
                startActivity(intent);
            }

        }.execute();
    }

    private void showToast(String text) {
        Toast.makeText( this, text, Toast.LENGTH_SHORT).show() ;
    }
}

注意到,我这里是先判断密码非空,再判断用户名非空,是故意弄反弄错的,然后运行下看下结果 直接右键测试类 run,就会执行其中全部测试方法, 如果要针对某个方法则选中方法名右键执行即可 题外话: 我也是才接触自动化单元测试,边研究边分享出来,觉得还是很新颖的, Espresso的取义确实也很贴切, run一下测试代码,然后就可以端着个Espresso(意式浓缩咖啡)在旁边看程序自己输啊点啊的了 然后是测试执行结果 espresso5

先看下方控制台 醒目的红色进度条表示有错误, 左侧有执行结果信息, 5个都执行结束了 Done: 5 of 5, 其中失败一个 Failed: 1 控制台左边会列出来错误的方法,右边会显示运行的结果,最主要的是错误部分的信息 从信息部分可以看到,找不到带有”请输入用户名”的view视图, 然后下面是定位到具体测试代码 109行, 从上面的代码可以看到正是test5的验证语句

这当然了,我们故意弄错提示的是”请输入密码”, 而其他4个都符合预期执行正确

可以在我的Github主页关注该项目 https://github.com/boredream/AndroidEspressoTest