Showing posts with label Menus. Show all posts
Showing posts with label Menus. Show all posts

Tuesday, January 1, 2013

How to test Android Long click menus using Robotium?

in Android, Long click menus can be found in List items, texts, views, screen.  

Robotium supports various functions to handle Long click menus,

  • clickLongInList(int line)
  • clickLongOnText(String text)
  • clickLongOnView(android.view.View view)
  • clickLongOnScreen(float x, float y)
Let us see usage of these APIs with an example test case and test code for the project shown on the right side.

project has a list of items and each support a long press functionality. on long press, will open a menu with various options like "Open, Delete, Edit title"

Let us write a test case for this project,

Test case for Android Long click menus :

Steps:
  1. Open notepad application
  2. press the menu item 'Add note' and add note 'Note 1'
  3. long click on the 'Note 1' in the list, which will open a menu, select 'Delete' option.
  4. verify the note is delete or not?
Test Code :
public void testRemoveNote() throws Exception {

        solo.clickOnMenuItem("Add note");

        //Assert that NoteEditor activity is opened

        solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); 

        //In text field 0, add Note 1

        solo.enterText(0, "Note 1");

        solo.clickOnMenuItem("Save");

 

       solo.clickLongOnText("Note 1");

        //Clicks on Delete in the context menu

        solo.clickOnText("Delete");          

     

        //Note 1 test should not be found

        boolean expected = false;   

        boolean actual = solo.searchText("Note 1");

        //Assert that Note 1 text is not found

        assertEquals("Note 1 Text is found", expected, actual);  

        

    }

Explanation:

  1. Add Note : use 'solo.clickOnMenuItem()' to press on the menu item 'Add Note'. 
  2. Long click : 'solo.clickLongOnText()' is used to perform long click operation on the specified text.

    • use 'solo.clickOnText()', to select the text from long click menu.

test Android Menu items using Robotium

Robotium supports various functions to test Android menu items. in Android applications Menu items appear on the pressing the menu button after launching the application.

  • pressMenuItem(int index)
  • pressMenuItem(int index, int itemsPerRow)
  • clickOnMenuItem(String text)
  • clickOnMenuItem(String text, boolean subMenu)

Right side shown is the sample Notepad Applications, which contains menu item called "Add note".

Let us write sample code to handle the menu items.

Test case for Android Menu items :

Steps:
  1. Launch application, verify the current activity
  2. open Menu item 'Add note'
  3. add new note
  4. go back to previous activity
  5. take screenshot
Test Code:
public void testAddNote() throws Exception {

        solo.clickOnMenuItem("Add note");

        //Assert that NoteEditor activity is opened

        solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); 

        //In text field 0, add Note 1

        solo.enterText(0, "Note 1");

        solo.clickOnMenuItem("Save");     

        //Clicks on menu item

        solo.clickOnMenuItem("Add note");

        //In text field 0, add Note 2

        solo.enterText(0, "Note 2");

        solo.clickOnMenuItem("Save");

        //Go back to first activity named "NotesList"

        solo.goBackToActivity("NotesList"); 

        //Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/".

        solo.takeScreenshot();

        boolean expected = true;

        boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2");

        //Assert that Note 1 & Note 2 are found

        assertEquals("Note 1 and/or Note 2 are not found", expected, actual); 

 

}

Explanation:

  1. Verify the current activity : ‘solo.assertCurrentActivity()’ is used to verify the current opened activity, which returns a boolean value based on the comparison.

  2. Open Menu item : ‘solo.clickOnMenuItem()’ is used to Open the Manu and click on the menu item with a given name.

  3. ‘solo.goBackToActivity()’ is used to go back to the activity with a given name.

  4. take screenshot : ‘solo.takescreenshot()’ will capture the android screen and save to SD card under “/sdcard/Robotium-Screenshots/”. Function supports two arguments

    • with “no arguments” – this will save the screenshot with current date/time
    • with “name” as an argument – this will save the screenshot with the specified name (String)