如何在Android测试中填写EditText字段

时间:2013-08-28 02:21:45

标签: android android-edittext android-testing

有没有人有为android项目创建测试的经验?我开始设置我的测试项目并注意到根本没有太多文档。我正在尝试设置测试以测试用户名和密码字段。我没有填写输入用户名或密码。但是现在当我尝试设置它们时,我会继续java.lang.NullPointerException,但我不明白为什么或如何。以下是我正在处理的代码示例。

public class GasTrackerTab1Test extends ActivityInstrumentationTestCase2<GasTrackerTab1> {  
    private Activity mActivity; // MyActivity is the class name of the app under test
    private EditText username;
    private EditText password;
    private Button loginButton;

    @SuppressWarnings("deprecation")
    public GasTrackerTab1Test() {
        super("com.wallproductions.gas.tracker", GasTrackerTab1.class);
    }

    @Override
    protected void setUp() throws Exception {
        /*
         * Call the super constructor (required by JUnit)
         */

        super.setUp();

        /*
         * prepare to send key events to the app under test by turning off touch mode.
         * Must be done before the first call to getActivity()
         */
        setActivityInitialTouchMode(false);

        /*
         * Start the app under test by starting its main activity. The test runner already knows
         * which activity this is from the call to the super constructor, as mentioned
         * previously. The tests can now use instrumentation to directly access the main
         * activity through mActivity.
         */
        mActivity = getActivity();

        username = (EditText)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.login_user_name);
        password = (EditText)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.login_password);
        loginButton = (Button)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.mainloginbtn);

    } // end of setUp() method definition

    /*
     * Tests the initial values of key objects in the app under test, to ensure the initial
     * conditions make sense. If one of these is not initialized correctly, then subsequent
     * tests are suspect and should be ignored.
     */

    public void testPreconditions() {
        assertNotNull(username);
        assertNotNull(password);
    }

    public void testInvalidUserNamePassword() {
        mActivity.runOnUiThread(
            new Runnable() {
                public void run() {
                    username.setFocus();
                    username.setText("tester");
                    password.setFocus();
                    password.setText("test1234");
                    loginButton.performClick();
                }
            }
        );
    }
}

问题是有什么好的文档可以看出来解决这个问题吗?另外,如何使用一些文本填写EditText框,然后通过警报验证是否给出了正确的响应。

2 个答案:

答案 0 :(得分:0)

我相信我已经找到了答案

http://www.java2s.com/Open-Source/Android/android-core/platform-tools-tradefederation/com/android/tradefed/uitestapp/EditBoxActivityTest.java.htm

基本上我需要在我的方法上面添加@UiThreadTest,然后使用setText工作。不使用Runable类。

public class GasTrackerTab1Test extends ActivityInstrumentationTestCase2<GasTrackerTab1> {  
    private Activity mActivity; // MyActivity is the class name of the app under test
    private EditText username;
    private EditText password;
    private Button loginButton;

    @SuppressWarnings("deprecation")
    public GasTrackerTab1Test() {
        super("com.wallproductions.gas.tracker", GasTrackerTab1.class);
    }

    @Override
    protected void setUp() throws Exception {
        /*
         * Call the super constructor (required by JUnit)
         */

        super.setUp();

        /*
         * prepare to send key events to the app under test by turning off touch mode.
         * Must be done before the first call to getActivity()
         */
        setActivityInitialTouchMode(false);

        /*
         * Start the app under test by starting its main activity. The test runner already knows
         * which activity this is from the call to the super constructor, as mentioned
         * previously. The tests can now use instrumentation to directly access the main
         * activity through mActivity.
         */
        mActivity = getActivity();

        username = (EditText)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.login_user_name);
        password = (EditText)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.login_password);
        loginButton = (Button)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.mainloginbtn);

    } // end of setUp() method definition

    /*
     * Tests the initial values of key objects in the app under test, to ensure the initial
     * conditions make sense. If one of these is not initialized correctly, then subsequent
     * tests are suspect and should be ignored.
     */

    public void testPreconditions() {
        assertNotNull(username);
        assertNotNull(password);
    }

    /**
     * Test that the edit box on {@link EditBoxActivity} can focused.
     */
    @UiThreadTest
    public void testUsernameTextFocus() {
        assertNotNull(username);
        assertTrue(username.requestFocus());
        assertTrue(username.hasFocus());
    }

    @UiThreadTest
    public void testPasswordTextFocus() {
        assertNotNull(password);
        assertTrue(password.requestFocus());
        assertTrue(password.hasFocus());
    }

    @UiThreadTest
    public void testInvalidUserNamePassword() {     
        username.requestFocus();
        username.setText("testing");
        password.requestFocus();
        password.setText("whatever");
        loginButton.callOnClick();
    }
}

答案 1 :(得分:0)

这对我有用: <div id="my-div" style="background: green" onclick="increase(event)"> 0 </div>