LoginActivity 自动化测试

public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {

    LoginActivity mActivity;
    TextView mTvUsername;
    TextView mPassword;
    EditText mEtxUsername;
    EditText mEtxPassword;
    Button mBtnOK;
    Button mBtnCancel;

    public LoginActivityTest()
    {
        super(LoginActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = getActivity();
        EMChat.getInstance().init(mActivity);
        ServerManager.getInstance().logout();
        mTvUsername = (TextView) mActivity.findViewById(R.id.textView_username);
        mEtxUsername = (EditText) mActivity.findViewById(R.id.editText_username);
        mEtxPassword = (EditText) mActivity.findViewById(R.id.editText_password);
        mBtnOK = (Button)mActivity.findViewById(R.id.button_OK);
        mBtnCancel = (Button)mActivity.findViewById(R.id.button_Cancel);

    }

    public void testUI()
    {
        Assert.assertEquals(mTvUsername.getText(), "Username");

        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mEtxUsername.requestFocus();
            }
        });
        getInstrumentation().waitForIdleSync();
        getInstrumentation().sendStringSync("yh");
        getInstrumentation().waitForIdleSync();

        //填写一个错误的密码
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mEtxPassword.requestFocus();
            }
        });
        getInstrumentation().waitForIdleSync();
        getInstrumentation().sendStringSync("yhyh");
        getInstrumentation().waitForIdleSync();

        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mBtnOK.requestFocus();
                mBtnOK.performClick();

            }
        });
        getInstrumentation().waitForIdleSync();
        Assert.assertFalse(mActivity.getLoginResult().contains("Success"));

        //填写一个正确的密码
        getInstrumentation().waitForIdleSync();
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mEtxPassword.requestFocus();
                mEtxPassword.setText("");
            }
        });
        getInstrumentation().waitForIdleSync();
        getInstrumentation().sendStringSync("yh");
        getInstrumentation().waitForIdleSync();
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mBtnOK.requestFocus();
                mBtnOK.performClick();

            }
        });
        getInstrumentation().waitForIdleSync();
        Assert.assertTrue(mActivity.getLoginResult().contains("Success"));


    }

    @Override
    public void tearDown()
    {
        try {
            ServerManager.getInstance().logout();
            super.tearDown();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


你可能感兴趣的:(LoginActivity 自动化测试)