Search in sources :

Example 11 with IProcessExecutor

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

the class MobileDeviceUtils method listFiles.

/**
    *
    * @param path a path on the mobile device. It can be a directory or file (if you want to check
    * for specific file existence)
    * <br/>
    * <b>Note for iOS:</b> You have to specify a relative path to the application data folder<br/>
    * For example: <i>"Documents/MyAppFiles"</i><br/>
    * and we'll internally search for files in:
    *  <i>"/Users/&lt;username&gt;/Library/Developer/CoreSimulator/Devices/&lt;device_id&gt;/data/Containers/Data/Application/&lt;app_id&gt;/Documents/MyAppFiles/"</i><br/>
    * which is the iOS Simulator application data folder path
    *
    * @return {@link FileInfo} array with all the files and folders from the target path
    */
public FileInfo[] listFiles(String path) {
    List<FileInfo> files = new ArrayList<FileInfo>();
    if (this.mobileDriver.isAndroidAgent()) {
        String[] commandArguments = new String[] { "shell", "ls", "-lan", path };
        try {
            IProcessExecutor processExecutor = executeAdbCommand(commandArguments, true);
            String result = processExecutor.getStandardOutput();
            if (!StringUtils.isNullOrEmpty(result)) {
                String[] lines = result.split("[\r\n]+");
                for (String line : lines) {
                    Matcher m = LS_ENTRY_PATTERN.matcher(line);
                    if (m.matches()) {
                        /*
                             * group 1 -> file size
                             * group 2 -> modification date and time
                             * group 3 -> file name
                             */
                        String fileName = m.group(3);
                        String absolutePath = new String(IoUtils.normalizeUnixDir(path) + fileName).replace("//", "/");
                        if (line.startsWith("l") && line.contains("->")) {
                            // a link
                            // after '->'
                            absolutePath = fileName.substring(fileName.lastIndexOf("->") + 2).trim();
                            // before '->'
                            fileName = fileName.substring(0, fileName.indexOf("->")).trim();
                        }
                        String fileSize = m.group(1);
                        FileInfo file = new FileInfo(fileName, absolutePath, line.startsWith("d") || fileSize.isEmpty());
                        if (!fileSize.isEmpty()) {
                            file.setSize(Long.parseLong(fileSize));
                        }
                        file.setModificationDate(LS_DATE_FORMAT.parse(m.group(2)));
                        files.add(file);
                    }
                }
            }
        } catch (Exception e) {
            throw new MobileOperationException("Unable to list files for path '" + path + "'", e);
        }
    } else {
        // iOS case supposed
        IFileSystemOperations fileSystemOperations = getFileSystemOperatoinsImpl();
        String[] filePaths = fileSystemOperations.findFiles(getiOSApplicationDataPath() + path, ".*", true, true, false);
        for (String filePath : filePaths) {
            boolean isDirectory = filePath.endsWith("/");
            if (isDirectory) {
                filePath = filePath.substring(0, filePath.length() - 1);
            }
            String fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
            FileInfo file = new FileInfo(fileName, filePath, isDirectory);
            file.setSize(fileSystemOperations.getFileSize(filePath));
            files.add(file);
        }
    }
    return files.toArray(new FileInfo[files.size()]);
}
Also used : Matcher(java.util.regex.Matcher) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) ArrayList(java.util.ArrayList) IFileSystemOperations(com.axway.ats.core.filesystem.model.IFileSystemOperations) 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