Monday, December 31, 2012

Using Robotium to test Android “Text Fields”

Robotium supports various functions to test Android single and multi line text fields.
The following are few APIs supported by Robotium to test the Text Fields,
  • searchEditText(String text)
  • clickOnEditText(int index)
  • clearEditText(int index)
  • typeText(int index, String text)
  • enterText(int index, String text)
Let us write a test case for the Sample project shown on the right side,

Test case for verifying the Text Fields :

Steps :
  1. Wait for the activity to launch
  2. Enter a text in the Text Field
  3. Verify the text entered in the Text Field
  4. Clear the Text Entered in the Text Field
  5. Enter a new Test again and Verify the text entered.
Test Code:
   1: public void testEditField() throws Exception {

   2:    solo.waitForActivity("MainActivity");

   3:             

   4:    solo.clickOnEditText(0);

   5:    String strInput = "Sample code 1";

   6:    solo.enterText(0, strInput);

   7:    boolean actual = solo.searchEditText(strInput);

   8:    assertEquals("text entered is not matching",true, actual);

   9:  

  10:    solo.clearEditText(0);

  11:    strInput = "Sample code 2";

  12:    solo.typeText(0, strInput);

  13:    actual = solo.searchEditText(strInput);

  14:    assertEquals("text entered is not matching",true, actual);

  15:  }

Explanation:
  1. solo.waitforActivity( ), is to wait for the activity to launch

  2. Enter Text in the Text Field :  to enter a text in the Text filed, both ‘solo.enterText()’ and ‘solo.typeText()’ can be used.

  3. in ‘enterText(int index, String text)’,  index is the index of the Text Field.

    • if multiple Text Fields exists, first one will be assigned an index of 0, second one will get an index 1 and so on..  index increases from Top to Bottom or Left to Right

    • Verify the Text Entered :  to verify the text contained in the Text Filed, ‘solo.searchEditText()’ can be used. it returns True, if pattern matches otherwise return False

  4. Clear the Text in the Text Field : to clear the Text exist in the Text Field, ‘solo.clearEditText()’ can be used.

  5. ‘assertEquals()’, can be used to assert True/False

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.