Waiting for Toast Notifications in Android Open Applications

Applies to TestComplete 14.10, last modified on June 5, 2019

When testing mobile applications, you may need to delay the test execution until a certain toast notification is displayed.

To wait for a certain toast notification, you can use a specific WaitForControlWithText method. TestComplete adds this method to the Device.Process objects that correspond to open Android applications. The method delays the test execution until a control with the specified text becomes available or until the specified timeout elapses.

The code below demonstrates how to delay the test execution until a notification with the "Toast Example" text is displayed. If a control with the specified text is not found until the timeout elapses, TestComplete posts a warning to the test log.

JavaScript, JScript

function Test1()
{
  Mobile.SetCurrent("MyDevice");
  TestedApps.MyAndroidApp.Run();
  var appObj = Mobile.Device().Process("smartbear.example.demoapp");
  appObj.RootLayout("").Button("buttonToast").TouchButton();
    // Waits until a Toast notification displays the specified text
  if (appObj.WaitForControlWithText("Toast Example", 3000))
    // Posts an image with the notification
    Log.Picture(appObj.RootLayout("").Picture(), "Toast was displayed.");
  else 
    // Posts a warning to the test log
    Log.Warning("Toast was not displayed.");
}

Python

def Test1():
    Mobile.SetCurrent("MyDevice")
    TestedApps.MyAndroidApp.Run()
    appObj = Mobile.Device().Process("smartbear.example.demoapp")
    appObj.RootLayout("").Button("buttonToast").TouchButton()
    # Waits until a Toast notification displays the specified text
    if appObj.WaitForControlWithText("Toast Example", 3000):
        # Posts an image with the notification
        Log.Picture(appObj.RootLayout("").Picture(), "Toast was displayed.")
    else:
        # Posts a warning to the test log
        Log.Warning("Toast was not displayed.")

VBScript

Sub Test1()
  Dim appObj
  Call Mobile.SetCurrent("MyDevice")
  Call TestedApps.MyAndroidApp.Run
  Set appObj = Mobile.Device.Process("smartbear.example.demoapp")
  Call appObj.RootLayout("").Button("buttonToast").TouchButton
  ' Waits until a Toast notification displays the specified text
  If appObj.WaitForControlWithText("Toast Example", 3000) Then
    ' Posts an image with the notification
    Call Log.Picture(appObj.RootLayout("").Picture, "Toast was displayed.")
  Else
    ' Posts a warning to the test log.
    Call Log.Warning("Toast was not displayed.")
  End If
End Sub

DelphiScript

procedure Test1;
var appObj;
begin
  Mobile.SetCurrent('MyDevice');
  TestedApps.MyAndroidApp.Run;
  appObj := Mobile.Device.Process('smartbear.example.demoapp');
  appObj.RootLayout('').Button('buttonToast').TouchButton;
  // Waits until a Toast notification displays the specified text
  if appObj.WaitForControlWithText('Toast Example', 3000) then
    // Posts an image with the notification
    Log.Picture(appObj.RootLayout('').Picture, 'Toast was displayed.')
  else
    // Posts a warning to the test log
    Log.Warning('Toast was not displayed.');
end;

C++Script, C#Script

function Test1()
{
  Mobile["SetCurrent"]("MyDevice");
  TestedApps["MyAndroidApp"]["Run"]();
  var appObj = Mobile["Device"]["Process"]("smartbear.example.demoapp");
  appObj["RootLayout"]("")["Button"]("buttonToast")["TouchButton"]();
  // Waits until a Toast notification displays the specified text
  if (appObj["WaitForControlWithText"]("Toast Example", 3000))
    // Posts an image with the notification
    Log["Picture"](appObj["RootLayout"]("")["Picture"](), "Toast was displayed.");
  else
    // Posts a warning to the test log
    Log["Warning"]("Toast was not displayed.");
}

See Also

WaitForControlWithText Method
Checking Object State
Testing Android Open Applications
Testing Android Applications

Highlight search results