2

有人有为 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 框中填写一些文本,然后通过警报验证是否给出了正确的响应。

4

2 回答 2

2

这对我有用: onView(withId(R.id.text_edit_id)).perform(typeText(someString));

于 2020-03-29T20:51:37.393 回答
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();
    }
}
于 2013-08-28T13:53:00.097 回答