Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Aug 02, 2017 12:52

By Shir Konky

This method closes all applications on an iOS device through Task manager.

Prerequisites:

  1. Add toggleAssistiveTouch method to your project
  2. Add launchAssistiveTouch method to your project

TaskManager

TaskManager

 

/*****************************************************************************
 * Close all applications through task manager with the use of assistive touch
 * @param  driver      the remote web driver
 * @return true/false  upon success
 *****************************************************************************/
public static boolean closeAllApps(RemoteWebDriver driver) throws Exception {
    //defining variables
    boolean PMAppsFlag=false; //to avoid closing PMs apps
    HashMap<String, Object> params1 = new HashMap<String, Object>();
    HashMap<String, Object> params2 = new HashMap<String, Object>();
    switchToContext("VISUAL");
    try {
        //launch assistivetouch
        if (!launchAssistiveTouch(driver))
            return false;
        //launch task manager
        params1.put("timeout", 10);
        params1.put("content", "Device");
        driver.executeScript("mobile:text:select", params1);
        params1.put("content", "More");
        driver.executeScript("mobile:text:select", params1);
        params1.put("content", "MultiTasking");
        driver.executeScript("mobile:text:select", params1);
        Thread.sleep(2000);
        //define region for application to look for and close:
        params1.clear();
        params1.put("target", "any");
        params1.put("threshold", 95);
        params1.put("screen.top", "28%");
        params1.put("screen.height", "72%");
        params1.put("screen.left", "31%");
        params1.put("screen.width", "37%");
        params1.put("content","\"PMConfig\" \"PMVideo\" \"PMConfigManager\" \"PMVideoManager\" \"PMVitalsManager\" \"PMAgent\"" );
        //close all applications
        for (int i=0; i < 20; i++) {
            //check if this is an pm application
            Object ret = driver.executeScript("mobile:text:find", params1);
            if (ret.toString().equals("true")) {
                if (PMAppsFlag == false){
                    //swipe to next application in task manager
                    params2.put("start", "50%,50%");
                    params2.put("end", "10%,50%");
                    params2.put("duration", "1");
                    driver.executeScript("mobile:touch:swipe", params2);
                    Thread.sleep(1000);
                    PMAppsFlag=true;
                    continue;
                }
                else { //end of loop
                    break;
                }
            }
            else {    //this is not a pm application
                //this is not a perfecto application so we need to close it
                PMAppsFlag = false;
                params2.put("start", "50%,60%");
                params2.put("end", "50%,10%");
                params2.put("duration", "1");
                driver.executeScript("mobile:touch:swipe", params2);
                Thread.sleep(1000);
                continue;
            }
        }
        //turn off assistive touch
        toggleAssistiveTouch(driver,false);
        return true;
    } catch (Exception e) {
        System.out.println(e.toString());
        return false;
    }
}