Wednesday, February 16, 2011

Android toast notification testing – Android functional testing


A toast notification is a message that pops up on the surface of the window. So the automation test engineers have to test this kind of functional testing.

Robotium 2.1 can support this toast notification testing. Previous Robotoium versions doesn’t support this functional test.

I have found one android service example in the marakana.com site. So you can refer this site and take the source code from the followed link.

This developer has implemented the application with android service with toast notification messages.
Once we can click on Start button that application service the music and it can make one toast notification popup “My Service Created”.



In this source code you can get the MyService.java file. Like the followed code…..

      public void onCreate() {
            Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onCreate");

            player = MediaPlayer.create(this, R.raw.jals);
            player.setLooping(false); // Set looping
      }

      public void onDestroy() {
            Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onDestroy");
            player.stop();
      }
     
      public void onStart(Intent intent, int startid) {
            Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onStart");
            player.start();
      }
We have 3 toast notification messages in this example. In this case we need to automate whether this test case is present or not.

Robotium is providing one method solo.searchText(“-----text------”); we need to pass the expected toast text values.This method can be take care of getting toast notification messages in instrumentation.
It can search the toast message and can gives the return type is a Boolean value.


public void testButtonsToast() throws Exception {
             solo.clickOnButton(0);
             boolean toast1 = solo.searchText("My Service Created");
             boolean toast2 = solo.searchText("My Service Started");
             assertEquals("Toast message appered- My service Created",toast1,true);
             assertEquals("Toast message appered- My service Started",toast2,true);
             solo.clickOnButton(1);
             boolean toast3 = solo.searchText("My Service Stopped");
             assertEquals("Toast message appered- My service stopped",toast3,true);                    
      }






2 comments:

  1. Logic of your asserts is wrong as you have (string, actual boolean, expected boolean) where asserts are expecting values in order (string, expected boolean, actual boolean). Therefore they should be as
    assertEquals("Toast message appered- My service stopped",true, toast3);
    Lastly there is huge possibility that there will be delay so you may have to wait for text

    ReplyDelete
  2. I still don't think this is the right solution to verify toast message. SearchText("") internally navigates through entire screen and if the same message is displayed on the screen then things might fail

    ReplyDelete