Dealing with the soft keyboard in a Robotium test
Recently I developed some unit tests (see my previous blog entry) by running them on an Android emulator with a tablet form factor. I ran them on the emulator instead of my physical tablet because the tests would eventually be executed on a Hudson continuous integration server. When I needed to update some of the test cases due to changes in the app, I ran them on my tablet, where they promptly failed.
The problem on the tablet was that the soft keyboard came into view and obscured the button that I wanted Robotium to press. It’s necessary to make these work on both tablets, where developers will run them, and on the emulator, where Hudson will run them.
After a brief search on the web, I learned of several options. One of them did not work at all for me. This was the suggestion to disable the keyboard thus:
solo.getEditText(index).setInputType(0); solo.enterText(index, "value");
The other suggestion was to use the back method of Solo class to make the keyboard go away. I put this command into the tests like this (line 16):
public void testCreateConnectionWithOnlyUserAndPW()
{
solo.pressMenuItem(0); //Settings
assertTrue("did not get PrefsActivity", solo.waitForActivity("PrefsActivity"));
getInstrumentation().invokeMenuActionSync(solo.getCurrentActivity(), R.id.menu_add_account, 0);
assertTrue("did not get AuthenticatorActivity", solo.waitForActivity("AuthenticatorActivity"));
//check that we are on the view to add a connection...
assertNotNull("did not get conn text", solo.getText("Add Connection"));
solo.enterText(1, "bcidemo");//user id
solo.enterText(2, "whatever");//password
solo.goBack(); //close the softkeyboard
solo.clickOnButton("Next");
assertTrue("did not get URL error msg",solo.searchText("Server URL cannot be empty"));
assertTrue("did not get desc error msg",solo.searchText("Description cannot be empty"));
assertTrue("did not get format error msg",solo.searchText("Server URL format is not correct"));
}
This solution worked fine when the tests ran on the tablet, but failed miserably when they ran on the emulator. Fix one, break one. By watching the emulator I realized that the soft keyboard never appeared.
I hadn’t specified the optional property of keyboard support when creating the emulator file because I hadn’t needed it. By editing the emulator file in the AVD Manager to add keyboard support, I got the keyboard to show up. Now the tests can dismiss the keyboard in both situations. It’s rather a shame to make something appear so you can get rid of it, but it’s a complicated world.
Hi, Thanks
Tha solo.goback saved me some time.
cheers
Hello,
Isn’t there an option to check whether the keyboard pops-up? I deal with different situations on different devices (On my HTC One X, API 16, i can click a dialog button when the keyboard is up, but on my Samsung Galaxy Tab, API 14, i cannot (it raises an exception when i try)
Cheers