Search in sources :

Example 6 with IProcessExecutor

use of com.axway.ats.core.process.model.IProcessExecutor in project ats-framework by Axway.

the class MobileDriver method killAdbServer.

/**
     * Kill misbehaving ADB server in order to bring it back later to normal state. The server is automatically started on next ADB command.
     * Then failed ADB operation (start/stop/clear cache, etc. should be retried.)
     */
private void killAdbServer() {
    log.info("Trying to restart ADB server on device: " + getDeviceDescription());
    String[] commandArguments = new String[] { "kill-server" };
    IProcessExecutor pe = null;
    try {
        pe = this.mobileDeviceUtils.executeAdbCommand(commandArguments, false);
    } catch (Exception e) {
        throw new MobileOperationException("Unable to stop ADB server. 'adb kill-server' failed", e);
    }
    if (pe.getExitCode() != 0) {
        log.warn("Unable to stop gracefully the ADB server. Command failed (Exit code: " + pe.getExitCode() + ", STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
        log.info("Trying to forcefully terminate ADB process");
        // fallback to taskkill
        try {
            if (OperatingSystemType.getCurrentOsType().isWindows()) {
                pe = new LocalProcessExecutor(HostUtils.LOCAL_HOST_NAME, "taskkill.exe", new String[] { "/IM", "adb.exe", "/F", "/T" });
            } else {
                pe = new LocalProcessExecutor(HostUtils.LOCAL_HOST_NAME, "killall", new String[] { "adb" });
            }
            pe.execute();
        } catch (Exception e) {
            log.info("Unable to kill ADB server. Command failed (Exit code: " + pe.getExitCode() + ", STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
            throw new MobileOperationException("Unable to stop ADB server with taskkill/killall", e);
        }
        if (pe.getExitCode() != 0) {
            // TODO - research possible error codes for non-existing process to kill
            log.error("Unable to kill ADB server. Command failed (Exit code: " + pe.getExitCode() + ", STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
        }
    }
}
Also used : MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) LocalProcessExecutor(com.axway.ats.core.process.LocalProcessExecutor) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) IProcessExecutor(com.axway.ats.core.process.model.IProcessExecutor)

Example 7 with IProcessExecutor

use of com.axway.ats.core.process.model.IProcessExecutor in project ats-framework by Axway.

the class MobileDriver method clearAppCache.

/**
     * Clear application cache
     *
     * @param applicationPackage application package name
     */
@PublicAtsApi
public void clearAppCache(String applicationPackage) {
    if (isAndroidAgent) {
        if (this.adbLocation == null) {
            throw new MobileOperationException("You must specify a valid Android home location or define " + ANDROID_HOME_ENV_VAR + " environment variable. The ADB executable must be located in a 'platform-tools/' subfolder");
        }
        String[] commandArguments = new String[] { "shell", "pm", "clear", applicationPackage };
        IProcessExecutor pe = null;
        int numRetries = 0;
        while (numRetries <= MAX_ADB_RELATED_RETRIES) {
            if (numRetries > 0) {
                log.warn("Retrying to start application action as previous try failed");
            }
            try {
                pe = this.mobileDeviceUtils.executeAdbCommand(commandArguments, false);
            } catch (Exception e) {
                throw new MobileOperationException("Unable to clear application cache of '" + applicationPackage + "'", e);
            }
            numRetries++;
            if (pe.getExitCode() == 0) {
                break;
            } else {
                if (numRetries <= MAX_ADB_RELATED_RETRIES) {
                    log.error("Unable to clear application cache of '" + applicationPackage + "'. Start command failed (Exit code: " + pe.getExitCode() + ", STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
                    //try to kill ADB and issue start again
                    killAdbServer();
                } else {
                    throw new MobileOperationException("Unable to clear application cache of '" + applicationPackage + "'. Clear cache command failed (Exit code: " + pe.getExitCode() + ", STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
                }
            }
        }
    } else {
        //TODO: Find solution. Note that "this.driver.resetApp();" doesn't reset app cache.
        throw new NotSupportedOperationException("Currently clear application cache operation for iOS is not implemented");
    }
}
Also used : MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) IProcessExecutor(com.axway.ats.core.process.model.IProcessExecutor) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 8 with IProcessExecutor

use of com.axway.ats.core.process.model.IProcessExecutor in project ats-framework by Axway.

the class MobileDriver method stopIOSSimulator.

/**
     * Stopping iOS Simulator
     */
@PublicAtsApi
public void stopIOSSimulator() {
    log.info("Stopping simulator on: " + getDeviceDescription());
    IProcessExecutor pe = null;
    try {
        // the simulator window was named "iPhone Simulator" till Xcode 6, now it is "iOS Simulator"
        pe = getProcessExecutorImpl("killall", new String[] { "iPhone Simulator", "iOS Simulator" });
        pe.execute();
    } catch (Exception e) {
        throw new MobileOperationException("Unable to stop iOS Simulator", e);
    }
    if (pe.getExitCode() != 0) {
        throw new MobileOperationException("Unable to stop iOS Simulator. Stop command failed (STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
    }
}
Also used : MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) IProcessExecutor(com.axway.ats.core.process.model.IProcessExecutor) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 9 with IProcessExecutor

use of com.axway.ats.core.process.model.IProcessExecutor in project ats-framework by Axway.

the class MobileDriver method stopAndroidApplication.

/**
     * Stop application using ADB command: ./adb shell am force-stop  &lt;PACKAGE&gt;<br/>
     * for example: ./adb shell am force-stop com.axway.st.mobile
     *
     * @param applicationPackage application package
     */
private void stopAndroidApplication(String applicationPackage) {
    log.info("Stopping application '" + applicationPackage + "' on device: " + getDeviceDescription());
    String[] commandArguments = new String[] { "shell", "am", "force-stop", applicationPackage };
    IProcessExecutor pe = null;
    int numRetries = 0;
    while (numRetries <= MAX_ADB_RELATED_RETRIES) {
        if (numRetries > 0) {
            log.warn("Retrying to start application action as previous try failed");
        }
        try {
            pe = this.mobileDeviceUtils.executeAdbCommand(commandArguments, false);
        } catch (Exception e) {
            throw new MobileOperationException("Unable to stop Android application with package '" + applicationPackage + "'", e);
        }
        numRetries++;
        if (pe.getExitCode() == 0) {
            break;
        } else {
            if (numRetries <= MAX_ADB_RELATED_RETRIES) {
                log.error("Unable to stop Android application with package '" + applicationPackage + "'. Stop command failed (Exit code: " + pe.getExitCode() + ", STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
                // try to kill ADB and issue stop again
                killAdbServer();
            } else {
                throw new MobileOperationException("Unable to stop Android application with package '" + applicationPackage + "'. Stop command failed (Exit code: " + pe.getExitCode() + ", STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
            }
        }
    }
// while
}
Also used : MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) IProcessExecutor(com.axway.ats.core.process.model.IProcessExecutor)

Example 10 with IProcessExecutor

use of com.axway.ats.core.process.model.IProcessExecutor in project ats-framework by Axway.

the class MobileDeviceUtils method executeAdbCommand.

public IProcessExecutor executeAdbCommand(String[] commandArguments, boolean verifyExitCode) {
    IProcessExecutor pe = null;
    try {
        if (getSystemOperationsImpl().getOperatingSystemType().isWindows()) {
            pe = getProcessExecutorImpl(this.mobileDriver.getAdbLocation() + "adb.exe", commandArguments);
        } else {
            pe = getProcessExecutorImpl(this.mobileDriver.getAdbLocation() + "adb", commandArguments);
        }
        pe.setWorkDirectory(this.mobileDriver.getAdbLocation());
        pe.execute();
        if (verifyExitCode && pe.getExitCode() != 0) {
            throw new MobileOperationException("Adb command failed (STDOUT: '" + pe.getStandardOutput() + "', STDERR: '" + pe.getErrorOutput() + "')");
        }
    } catch (Exception e) {
        throw new MobileOperationException("Adb command failed", e);
    }
    return pe;
}
Also used : MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) IProcessExecutor(com.axway.ats.core.process.model.IProcessExecutor)

Aggregations

IProcessExecutor (com.axway.ats.core.process.model.IProcessExecutor)11 MobileOperationException (com.axway.ats.uiengine.exceptions.MobileOperationException)9 NotSupportedOperationException (com.axway.ats.uiengine.exceptions.NotSupportedOperationException)9 PublicAtsApi (com.axway.ats.common.PublicAtsApi)3 LocalProcessExecutor (com.axway.ats.core.process.LocalProcessExecutor)2 IFileSystemOperations (com.axway.ats.core.filesystem.model.IFileSystemOperations)1 AbstractRealBrowserDriver (com.axway.ats.uiengine.AbstractRealBrowserDriver)1 RobotException (com.axway.ats.uiengine.exceptions.RobotException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)1 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)1