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;
}
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;
}
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;
}
Aggregations