Search in sources :

Example 1 with DriverAugmenter

use of com.qaprosoft.carina.core.foundation.webdriver.augmenter.DriverAugmenter in project carina by qaprosoft.

the class Screenshot method captureFullSize.

/**
 * Captures full size screenshot based on auto_screenshot global parameter, creates thumbnail and copies both images to specified screenshots
 * location.
 *
 * @param driver
 *          instance used for capturing.
 * @param comment
 * 			String comment
 * @param artifact
 * 			boolean artifact
 * @return screenshot name.
 */
public static BufferedImage captureFullSize(WebDriver driver, String comment, boolean artifact) {
    String screenName;
    BufferedImage screen = null;
    try {
        if (!isCaptured(comment)) {
            // LOGGER.debug("Unable to capture screenshot as driver seems invalid: " + comment);
            return null;
        }
        LOGGER.debug("Screenshot->captureFullSize starting...");
        // remove all DriverListener casting to WebDriver
        driver = castDriver(driver);
        // Define test screenshot root
        File testScreenRootDir = ReportContext.getTestDir();
        // Capture full page screenshot and resize
        screenName = comment + ".png";
        String screenPath = testScreenRootDir.getAbsolutePath() + "/" + screenName;
        WebDriver augmentedDriver = driver;
        if (!driver.toString().contains("AppiumNativeDriver")) {
            // do not augment for Appium 1.x anymore
            augmentedDriver = new DriverAugmenter().augment(driver);
        }
        screen = takeFullScreenshot(driver, augmentedDriver);
        if (screen == null) {
            // do nothing and return empty
            return null;
        }
        if (Configuration.getInt(Parameter.BIG_SCREEN_WIDTH) != -1 && Configuration.getInt(Parameter.BIG_SCREEN_HEIGHT) != -1) {
            resizeImg(screen, Configuration.getInt(Parameter.BIG_SCREEN_WIDTH), Configuration.getInt(Parameter.BIG_SCREEN_HEIGHT), screenPath);
        }
        File screenshot = new File(screenPath);
        ImageIO.write(screen, "PNG", screenshot);
        // Uploading screenshot to Amazon S3
        if (artifact) {
            com.zebrunner.agent.core.registrar.Artifact.attachToTest(comment + ".png", screenshot);
        } else {
            com.zebrunner.agent.core.registrar.Screenshot.upload(Files.readAllBytes(screenshot.toPath()), Instant.now().toEpochMilli());
        }
        // add screenshot comment to collector
        ReportContext.addScreenshotComment(screenName, comment);
        return screen;
    } catch (IOException e) {
        LOGGER.error("Unable to capture screenshot due to the I/O issues!", e);
    } catch (WebDriverException e) {
        if (isCaptured(e.getMessage())) {
            // display exception as we suspect to make screenshot for this use-case
            LOGGER.warn("Unable to capture screenshot due to the WebDriverException!");
            LOGGER.debug(ERROR_STACKTRACE, e);
        } else {
            // Do not display exception by default as we don't suspect to make screenshot for this use-case
            LOGGER.debug("Unable to capture screenshot due to the WebDriverException!", e);
        }
    } catch (Exception e) {
        LOGGER.warn("Unable to capture screenshot due to the Exception!");
        LOGGER.debug(ERROR_STACKTRACE, e);
    } finally {
        LOGGER.debug("Screenshot->captureFullSize finished.");
    }
    return screen;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) EventFiringWebDriver(org.openqa.selenium.support.events.EventFiringWebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) WebDriverException(org.openqa.selenium.WebDriverException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) DriverAugmenter(com.qaprosoft.carina.core.foundation.webdriver.augmenter.DriverAugmenter) WebDriverException(org.openqa.selenium.WebDriverException)

Example 2 with DriverAugmenter

use of com.qaprosoft.carina.core.foundation.webdriver.augmenter.DriverAugmenter in project carina by qaprosoft.

the class Screenshot method capture.

/**
 * Captures application screenshot, creates thumbnail and copies both images to specified screenshots location.
 *
 * @param driver
 *            instance used for capturing.
 * @param isTakeScreenshot
 *            perform actual capture or not
 * @param comment
 *            String
 * @param fullSize
 *            Boolean
 * @return screenshot name.
 */
private static String capture(WebDriver driver, boolean isTakeScreenshot, String comment, boolean fullSize) {
    String screenName = "";
    if (isTakeScreenshot) {
        LOGGER.debug("Screenshot->capture starting...");
        // remove all DriverListener casting to WebDriver
        driver = castDriver(driver);
        try {
            if (!isCaptured(comment)) {
                // LOGGER.debug("Unable to capture screenshot as driver seems invalid: " + comment);
                return screenName;
            }
            // Define test screenshot root
            File testScreenRootDir = ReportContext.getTestDir();
            // Capture full page screenshot and resize
            screenName = System.currentTimeMillis() + ".png";
            String screenPath = testScreenRootDir.getAbsolutePath() + "/" + screenName;
            WebDriver augmentedDriver = driver;
            // hotfix to converting proxy into the valid driver
            if (driver instanceof Proxy) {
                try {
                    InvocationHandler innerProxy = Proxy.getInvocationHandler((Proxy) driver);
                    // "arg$2" is by default RemoteWebDriver;
                    // "arg$1" is EventFiringWebDriver
                    // wrap into try/catch to make sure we don't affect test execution
                    Field locatorField = innerProxy.getClass().getDeclaredField("arg$2");
                    locatorField.setAccessible(true);
                    augmentedDriver = driver = (WebDriver) locatorField.get(innerProxy);
                } catch (Exception e) {
                // do nothing and receive augmenting warning in the logs
                }
            }
            if (!driver.toString().contains("AppiumNativeDriver")) {
                // do not augment for Appium 1.x anymore
                augmentedDriver = new DriverAugmenter().augment(driver);
            }
            BufferedImage screen;
            // Create screenshot
            if (fullSize) {
                screen = takeFullScreenshot(driver, augmentedDriver);
            } else {
                screen = takeVisibleScreenshot(augmentedDriver);
            }
            if (screen == null) {
                // do nothing and return empty
                return "";
            }
            if (Configuration.getInt(Parameter.BIG_SCREEN_WIDTH) != -1 && Configuration.getInt(Parameter.BIG_SCREEN_HEIGHT) != -1) {
                resizeImg(screen, Configuration.getInt(Parameter.BIG_SCREEN_WIDTH), Configuration.getInt(Parameter.BIG_SCREEN_HEIGHT), screenPath);
            }
            File screenshot = new File(screenPath);
            ImageIO.write(screen, "PNG", screenshot);
            com.zebrunner.agent.core.registrar.Screenshot.upload(Files.readAllBytes(screenshot.toPath()), Instant.now().toEpochMilli());
            // add screenshot comment to collector
            ReportContext.addScreenshotComment(screenName, comment);
        } catch (NoSuchWindowException e) {
            LOGGER.warn("Unable to capture screenshot due to NoSuchWindowException!");
            LOGGER.debug(ERROR_STACKTRACE, e);
        } catch (IOException e) {
            LOGGER.warn("Unable to capture screenshot due to the I/O issues!");
            LOGGER.debug(ERROR_STACKTRACE, e);
        } catch (WebDriverException e) {
            LOGGER.warn("Unable to capture screenshot due to the WebDriverException!");
            LOGGER.debug(ERROR_STACKTRACE, e);
        } catch (Exception e) {
            LOGGER.warn("Unable to capture screenshot due to the Exception!");
            LOGGER.debug(ERROR_STACKTRACE, e);
        } finally {
            LOGGER.debug("Screenshot->capture finished.");
        }
    }
    return screenName;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) EventFiringWebDriver(org.openqa.selenium.support.events.EventFiringWebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) Field(java.lang.reflect.Field) Proxy(java.lang.reflect.Proxy) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) IOException(java.io.IOException) File(java.io.File) InvocationHandler(java.lang.reflect.InvocationHandler) WebDriverException(org.openqa.selenium.WebDriverException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) BufferedImage(java.awt.image.BufferedImage) DriverAugmenter(com.qaprosoft.carina.core.foundation.webdriver.augmenter.DriverAugmenter) WebDriverException(org.openqa.selenium.WebDriverException)

Example 3 with DriverAugmenter

use of com.qaprosoft.carina.core.foundation.webdriver.augmenter.DriverAugmenter in project carina by qaprosoft.

the class Screenshot method captureMetadata.

/**
 * Captures driver screenshot for Alice-AI metadata and put it to appropriate metadata location
 *
 * @param driver
 *            instance used for capturing.
 * @param screenName
 *            String
 * @return screenshot name.
 */
public static String captureMetadata(WebDriver driver, String screenName) {
    String screenPath = "";
    try {
        screenPath = ReportContext.getMetadataFolder().getAbsolutePath() + "/" + screenName.replaceAll("\\W+", "_") + ".png";
        WebDriver augmentedDriver = driver;
        if (!driver.toString().contains("AppiumNativeDriver")) {
            // do not augment for Appium 1.x anymore
            augmentedDriver = new DriverAugmenter().augment(driver);
        }
        BufferedImage screen;
        // Create screenshot
        screen = takeVisibleScreenshot(augmentedDriver);
        ImageIO.write(screen, "PNG", new File(screenPath));
    } catch (IOException e) {
        LOGGER.error("Unable to capture screenshot due to the I/O issues!", e);
    } catch (Exception e) {
        LOGGER.error("Unable to capture screenshot!", e);
    }
    return screenPath;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) EventFiringWebDriver(org.openqa.selenium.support.events.EventFiringWebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) WebDriverException(org.openqa.selenium.WebDriverException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) DriverAugmenter(com.qaprosoft.carina.core.foundation.webdriver.augmenter.DriverAugmenter)

Aggregations

DriverAugmenter (com.qaprosoft.carina.core.foundation.webdriver.augmenter.DriverAugmenter)3 BufferedImage (java.awt.image.BufferedImage)3 File (java.io.File)3 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 NoSuchWindowException (org.openqa.selenium.NoSuchWindowException)3 WebDriver (org.openqa.selenium.WebDriver)3 WebDriverException (org.openqa.selenium.WebDriverException)3 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)3 EventFiringWebDriver (org.openqa.selenium.support.events.EventFiringWebDriver)3 Field (java.lang.reflect.Field)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Proxy (java.lang.reflect.Proxy)1