Search in sources :

Example 11 with LocalFileSystemOperations

use of com.axway.ats.core.filesystem.LocalFileSystemOperations in project ats-framework by Axway.

the class Test_FileSystemSnapshot method checkPermissions.

@Test
public void checkPermissions() {
    if (new LocalSystemOperations().getOperatingSystemType().isWindows()) {
        Logger.getLogger(Test_FileSystemSnapshot.class).warn("We skip this test as it is not applicable for Windows OS");
        return;
    }
    // disable all checks
    configurator.setFileSnapshotCheckFileSize(false);
    configurator.setFileSnapshotCheckModificationTime(false);
    configurator.setFileSnapshotCheckFileMd5(false);
    configurator.setFileSnapshotCheckFilePermissions(false);
    // we work with 2 files only
    String firstFile = FILES_ROOT + "permissions/sub-dir1/file3.xml";
    String secondFile = FILES_ROOT + "permissions/sub-dir2/file3.xml";
    // remember the current permissions
    String firstPermissions = new LocalFileSystemOperations().getFilePermissions(firstFile);
    String secondPermissions = new LocalFileSystemOperations().getFilePermissions(secondFile);
    // make permissions different
    new LocalFileSystemOperations().setFilePermissions(firstFile, "333");
    new LocalFileSystemOperations().setFilePermissions(secondFile, "777");
    try {
        // TEST1: The pair of files have different permissions, but we have disabled the check, so no
        // error will be thrown when comparing
        FileSystemSnapshot snapshot1 = new FileSystemSnapshot("snap1");
        snapshot1.addDirectory("F1", FILES_ROOT + "permissions/sub-dir1");
        snapshot1.takeSnapshot();
        FileSystemSnapshot snapshot2 = new FileSystemSnapshot("snap2");
        snapshot2.addDirectory("F1", FILES_ROOT + "permissions/sub-dir2");
        snapshot2.takeSnapshot();
        snapshot1.compare(snapshot2);
        // globally enable checking the file permissions
        configurator.setFileSnapshotCheckFilePermissions(true);
        // TEST2: Now we will do the check and error must be registered
        FileSystemSnapshot snapshot3 = new FileSystemSnapshot("snap1");
        snapshot3.addDirectory("F1", FILES_ROOT + "permissions/sub-dir1");
        snapshot3.takeSnapshot();
        FileSystemSnapshot snapshot4 = new FileSystemSnapshot("snap2");
        snapshot4.addDirectory("F1", FILES_ROOT + "permissions/sub-dir2");
        snapshot4.takeSnapshot();
        try {
            // expected exception - file permissions difference
            snapshot3.compare(snapshot4);
            // log permissions
            LocalFileSystemOperations lfs = new LocalFileSystemOperations();
            System.err.println("Snapshots compare is expected to fail. Permissions dump:");
            System.err.println("Permissions for " + firstFile + ": " + lfs.getFilePermissions(firstFile));
            System.err.println("Permissions for " + secondFile + ": " + lfs.getFilePermissions(secondFile));
            thisShouldNotBeReached();
        } catch (FileSystemSnapshotException se) {
            verifyError(se, ".*Permissions: .*");
        }
    } finally {
        // restore the original permissions
        new LocalFileSystemOperations().setFilePermissions(firstFile, firstPermissions);
        new LocalFileSystemOperations().setFilePermissions(secondFile, secondPermissions);
    }
}
Also used : LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) LocalSystemOperations(com.axway.ats.core.system.LocalSystemOperations) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 12 with LocalFileSystemOperations

use of com.axway.ats.core.filesystem.LocalFileSystemOperations in project ats-framework by Axway.

the class UiEngineUtilities method createScreenshot.

/**
     * Create a screenshot image (supported image format/type is PNG).<br/>
     * If the screenshot image file already exists it will be automatically replaced by the new one.<br/>
     * <br/>
     * Currently the supported UI drivers are {@link AbstractRealBrowserDriver} and {@link MobileDriver}:
     * <ul>
     *     <li>{@link AbstractRealBrowserDriver} - the method makes a best effort to create a screenshot,
     *     depending on the browser to return the following in order of preference:
     *          <ul>
     *              <li>Entire page</li>
     *              <li>Current window</li>
     *              <li>Visible portion of the current frame</li>
     *              <li>The screenshot of the entire display containing the browser</li>
     *          </ul>
     *     </li>
     *     <li>{@link MobileDriver} - creates a screenshot of the mobile device screen.<br/>
     *          <b>NOTE:</b> There is a <a href="https://github.com/selendroid/selendroid/issues/325">known issue</a> on Android Virtual Device with <b>"Use Host GPU"</b> enabled option.
     *          So in order to get a screenshot it should be disabled. Keep in mind that it will affect the performance, because
     *          it is a performance acceleration option.
     *     </li>
     * </ul>
     *
     * @param filePath the screenshot image file path
     * @param uiDriver {@link UiDriver} instance
     */
@PublicAtsApi
public static void createScreenshot(String filePath, UiDriver uiDriver) {
    WebDriver webDriver = null;
    if (uiDriver instanceof AbstractRealBrowserDriver) {
        AbstractRealBrowserDriver browserDriver = (AbstractRealBrowserDriver) uiDriver;
        webDriver = (WebDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.toString());
    } else if (uiDriver instanceof MobileDriver) {
        MobileDriver mobileDriver = (MobileDriver) uiDriver;
        webDriver = (WebDriver) mobileDriver.getInternalObject(InternalObjectsEnum.WebDriver.toString());
        ((AppiumDriver) webDriver).context(MobileDriver.NATIVE_CONTEXT);
    } else {
        throw new NotSupportedOperationException("Currently it is not possible to create a screenshot with driver: " + uiDriver.getClass().getSimpleName());
    }
    File scrTmpFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
    File scrFile = new File(filePath);
    if (scrFile.exists() && !scrFile.delete()) {
        log.warn("The Screenshot image file '" + filePath + "' already exists, but couldn't be deleted. You can find the current Screenshot image here: " + scrTmpFile.getAbsolutePath());
    } else if (!scrTmpFile.renameTo(scrFile)) {
        // if renameTo() fails we will try to copy the file
        try {
            new LocalFileSystemOperations().copyFile(scrTmpFile.getCanonicalPath(), scrFile.getCanonicalPath(), true);
            scrTmpFile.delete();
        } catch (Exception e) {
            log.warn("Unable to create Screenshot image file: " + filePath);
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) AbstractRealBrowserDriver(com.axway.ats.uiengine.AbstractRealBrowserDriver) LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) MobileDriver(com.axway.ats.uiengine.MobileDriver) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) File(java.io.File) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) TakesScreenshot(org.openqa.selenium.TakesScreenshot) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 13 with LocalFileSystemOperations

use of com.axway.ats.core.filesystem.LocalFileSystemOperations in project ats-framework by Axway.

the class Test_HttpClient method verifyDownloadedFileMatch.

private void verifyDownloadedFileMatch(String expectedFile, String actualFile) throws IOException {
    // check the file contents
    String actualFileMD5Sum;
    String expectedFileMD5Sum;
    try {
        LocalFileSystemOperations lfso = new LocalFileSystemOperations();
        actualFileMD5Sum = lfso.computeMd5Sum(actualFile, Md5SumMode.BINARY);
        expectedFileMD5Sum = lfso.computeMd5Sum(expectedFile, Md5SumMode.BINARY);
    } catch (FileSystemOperationException fsoe) {
        throw new RuntimeException("Error calculating MD5 sum", fsoe);
    }
    if (!actualFileMD5Sum.equals(expectedFileMD5Sum)) {
        throw new RuntimeException("The " + actualFile + " file is different than the expected " + expectedFile);
    } else {
        log.info("Matched actual '" + actualFile + "' and expected '" + expectedFile + "' files");
    }
}
Also used : LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException)

Aggregations

LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)13 PublicAtsApi (com.axway.ats.common.PublicAtsApi)7 IFileSystemOperations (com.axway.ats.core.filesystem.model.IFileSystemOperations)6 Validator (com.axway.ats.core.validation.Validator)6 File (java.io.File)2 BaseTest (com.axway.ats.action.BaseTest)1 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 InvalidMatcherException (com.axway.ats.agent.core.templateactions.exceptions.InvalidMatcherException)1 XmlUtilitiesException (com.axway.ats.agent.core.templateactions.exceptions.XmlUtilitiesException)1 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 FileSystemSnapshotException (com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)1 LocalSystemOperations (com.axway.ats.core.system.LocalSystemOperations)1 AbstractRealBrowserDriver (com.axway.ats.uiengine.AbstractRealBrowserDriver)1 MobileDriver (com.axway.ats.uiengine.MobileDriver)1 NotSupportedOperationException (com.axway.ats.uiengine.exceptions.NotSupportedOperationException)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1