Search in sources :

Example 1 with MobileDriver

use of com.axway.ats.uiengine.MobileDriver in project ats-framework by Axway.

the class MobileElement method scrollTo.

/**
     * Scroll to element (at the center of the screen)
     *
     * @return this mobile element which allows chained actions
     */
@SuppressWarnings("unchecked")
@PublicAtsApi
public T scrollTo() {
    try {
        if (MobileElementFinder.getElementContext(this).toUpperCase().startsWith("WEBVIEW")) {
            // in WEBVIEWs the target element exists, while in the NATIVE context it doesn't until we scroll to it
            new MobileElementState(this).waitToBecomeExisting();
            Dimension screenDimensions = ((MobileDriver) getUiDriver()).getScreenDimensions();
            WebElement element = MobileElementFinder.findElement(appiumDriver, this);
            // window.scrollTo(0, element.getLocation().y);    -->  will scroll the element to top-left
            int scrollToY = 0;
            int screenCenter = screenDimensions.getHeight() / 2 + element.getSize().height / 2;
            if (element.getLocation().y < screenCenter) {
                // the element is located after the screen center if we scroll to (0, element.getLocation().y)
                // because it is near the bottom of the application => we can't center it, but it is OK on that position
                scrollToY = element.getLocation().y;
            } else {
                scrollToY = element.getLocation().y - screenCenter;
            }
            ((JavascriptExecutor) appiumDriver).executeScript("window.scrollTo(0," + scrollToY + ")");
        } else {
            if (getElementProperty("name") != null) {
                // only works for NATIVE context
                appiumDriver.scrollTo(getElementProperty("name"));
            }
        }
        return (T) this;
    } catch (Exception e) {
        throw new MobileOperationException(this, "scrollTo", e);
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) MobileDriver(com.axway.ats.uiengine.MobileDriver) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) Dimension(org.openqa.selenium.Dimension) WebElement(org.openqa.selenium.WebElement) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) VerificationException(com.axway.ats.uiengine.exceptions.VerificationException) MobileElementState(com.axway.ats.uiengine.utilities.mobile.MobileElementState) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 2 with MobileDriver

use of com.axway.ats.uiengine.MobileDriver 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)

Aggregations

PublicAtsApi (com.axway.ats.common.PublicAtsApi)2 MobileDriver (com.axway.ats.uiengine.MobileDriver)2 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)1 AbstractRealBrowserDriver (com.axway.ats.uiengine.AbstractRealBrowserDriver)1 MobileOperationException (com.axway.ats.uiengine.exceptions.MobileOperationException)1 NotSupportedOperationException (com.axway.ats.uiengine.exceptions.NotSupportedOperationException)1 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)1 MobileElementState (com.axway.ats.uiengine.utilities.mobile.MobileElementState)1 File (java.io.File)1 Dimension (org.openqa.selenium.Dimension)1 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)1 TakesScreenshot (org.openqa.selenium.TakesScreenshot)1 WebDriver (org.openqa.selenium.WebDriver)1 WebElement (org.openqa.selenium.WebElement)1