Forum Discussion

chopper_elation's avatar
chopper_elation
New Member
9 years ago

How do I create a swipe routine that looks for an app on mobile device

I don't want to always guarantee what screen a mobile device is starting on. So after pressing the Apps menu on Android, how do I set up my test to Swipe until app is found, then it can click on the app icon. Thanks!

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    I would recommend against the image-based approach, because it may be unreliable.

     

    The recommended way to start an app is to add its .apk to TestedApps and use:

    TestedApps.appname.Run()

     

    If the app is pre-installed and you don't contol it (Calculator for example), you can run it using the "am start" shell command. You need to know the application's package name + activity name, or the intent action.

    // JScript
    Mobile.SetCurrent("MyDevice");
    // Run Calculator Mobile.Device.ShellExecute("am start -n com.android.calculator2/.Calculator");
    // Run Calendar Mobile.Device.ShellExecute("am start -n com.android.calendar/.AllInOneActivity");
    // Open Settings Mobile.Device.ShellExecute("am start -n com.android.settings/.Settings");
    // Run Camera Mobile.Device.ShellExecute("am start -a android.media.action.IMAGE_CAPTURE")

    To find the application's activity name for use with "am start", run the app on the device, then select the device in the Object Browser and view the RunningTaskInfo.TopActivity property. The activity syntax is PackageName/ShortClassName. For example:

     

    PackageName = com.android.calculator2
    ShortClassName = .Calculator
    =>
    com.android.calculator2/.Calculator


    For more information about the "am start" command, see Using activity manager (am) in Android developer documentation.

    • TanyaYatskovska's avatar
      TanyaYatskovska
      SmartBear Alumni (Retired)

      Thanks for your solution, Helen. It’s a very interesting approach!

       

      chopper_elation, you can choose between two solutions or suggest your own :)

  • anhvu's avatar
    anhvu
    Frequent Contributor

    Here is my script

     

    function openAppwithADBShell(device,package,activity)
    {
    var status = true;
    var msg = "";
    //Check Device
    if(Mobile["Device"](device)["Exists"])
    {
    Mobile["Device"](device)["ShellExecute"]("adb shell am start "+ package +"/"+ activity);
    //Delay
    Delay(10000)
    }
    else
    {
    status = false;
    msg = Log["Error"]("openAppwithADBShell: "+"Failed!"+"The device: "+device+" is not existed!")
    }
    return(new Array("openAppwithADBShell",status,msg));
    }

     

    You should download the System Android Information app to know what is the package and activity of your app.

    For example: 

    //Package: com.apple.android.music
    //Activity: com.apple.android.music.onboarding.activities.SplashActivity

    The activity is the one contain the MAIN function.

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Chopper_elation,

     

    You can do the following:

    1. You need to add the images of your app icon and the menu icon to TestComplete’s Image Repository. TestComplete will search for objects based on these images and touch them on the screen. Please read the Image-Based Testing Principles ( http://smartbear.com/viewarticle/75229/ ) article for details.
    2. To swipe the menu window, you can call the Swipe method of the AndroidDevice object. So your test may look like this (this is a sample app that is looking for the GPSTest application on Sony):

     

    function Test() {
      var nameAndroidDevice = "*";
      var AndroidDevice = Mobile.Device(nameAndroidDevice);
      Mobile.SetCurrent(nameAndroidDevice);
      var SonyLaunchApp = ImageRepository.SonyLaunchApp;
      // find the menu icon and touch it
      SonyLaunchApp.MenuButton.Touch();
      // swipe the window until the needed app is found
      AndroidDevice.Swipe(700, 200, 100, 600, 1);
      var GPSTestButton = SonyLaunchApp.GPSTest;
      if (GPSTestButton.Exists())
         // the app was found
         GPSTestButton.Touch();
      AndroidDevice.Swipe(700, 200, 100, 600, 1);
      if (GPSTestButton.Exists())
        GPSTestButton.Touch();
    }