Skip to content

Commit

Permalink
refactored screenshoting using pageloadtimeout instead of future.get
Browse files Browse the repository at this point in the history
  • Loading branch information
vdelendik committed Jun 9, 2022
1 parent 007419f commit d49f5ed
Showing 1 changed file with 64 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
Expand All @@ -40,6 +36,7 @@
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand All @@ -50,6 +47,7 @@
import com.qaprosoft.carina.core.foundation.commons.SpecialKeywords;
import com.qaprosoft.carina.core.foundation.report.ReportContext;
import com.qaprosoft.carina.core.foundation.utils.Configuration;
import com.qaprosoft.carina.core.foundation.utils.R;
import com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter;
import com.qaprosoft.carina.core.foundation.webdriver.augmenter.DriverAugmenter;
import com.qaprosoft.carina.core.foundation.webdriver.screenshot.IScreenshotRule;
Expand Down Expand Up @@ -481,85 +479,62 @@ private static void resizeImg(BufferedImage bufferedImage, int width, int height
* @return screenshot image
*/
private static BufferedImage takeFullScreenshot(WebDriver driver, WebDriver augmentedDriver) throws Exception {
Future<?> future = Executors.newSingleThreadExecutor().submit(new Callable<BufferedImage>() {
public BufferedImage call() throws IOException {
BufferedImage screenShot;
if (driver.getClass().toString().contains("windows")) {
File screenshot = ((WindowsDriver<?>) driver).getScreenshotAs(OutputType.FILE);
screenShot = ImageIO.read(screenshot);
} else if (driver.getClass().toString().contains("java_client")) {
// Mobile Native app
File screenshot = ((AppiumDriver<?>) driver).getScreenshotAs(OutputType.FILE);
screenShot = ImageIO.read(screenshot);
} else if (Configuration.getDriverType().equals(SpecialKeywords.MOBILE)) {
ru.yandex.qatools.ashot.Screenshot screenshot;
if (Configuration.getPlatform().equals("ANDROID")) {
String pixelRatio = String.valueOf(((EventFiringWebDriver) augmentedDriver).getCapabilities().getCapability("pixelRatio"));
if (!pixelRatio.equals("null")) {
float dpr = Float.parseFloat(pixelRatio);
screenshot = (new AShot()).shootingStrategy(ShootingStrategies
.viewportRetina(SpecialKeywords.DEFAULT_SCROLL_TIMEOUT, SpecialKeywords.DEFAULT_BLOCK, SpecialKeywords.DEFAULT_BLOCK,
dpr))
.takeScreenshot(augmentedDriver);
screenShot = screenshot.getImage();
} else {
screenshot = (new AShot()).shootingStrategy(ShootingStrategies
.viewportRetina(SpecialKeywords.DEFAULT_SCROLL_TIMEOUT, SpecialKeywords.DEFAULT_BLOCK, SpecialKeywords.DEFAULT_BLOCK,
SpecialKeywords.DEFAULT_DPR))
.takeScreenshot(augmentedDriver);
screenShot = screenshot.getImage();
}
} else {
int deviceWidth = augmentedDriver.manage().window().getSize().getWidth();
String deviceName = "";
if (augmentedDriver instanceof EventFiringWebDriver) {
deviceName = String.valueOf(((EventFiringWebDriver) augmentedDriver).getCapabilities().getCapability("deviceName"));
} else if (augmentedDriver instanceof RemoteWebDriver) {
deviceName = String.valueOf(((RemoteWebDriver) augmentedDriver).getCapabilities().getCapability("deviceName"));
}
screenshot = new AShot().shootingStrategy(getScreenshotShuttingStrategy(deviceWidth, deviceName)).takeScreenshot(augmentedDriver);
screenShot = screenshot.getImage();
}
} else {
// regular web
ru.yandex.qatools.ashot.Screenshot screenshot;
screenshot = (new AShot()).shootingStrategy(ShootingStrategies.viewportPasting(SpecialKeywords.DEFAULT_SCROLL_TIMEOUT))
.takeScreenshot(augmentedDriver);
screenShot = screenshot.getImage();
}
return screenShot;
}
});


BufferedImage screenShot = null;
// default timeout for driver quit 1/3 of explicit
long timeout = Configuration.getInt(Parameter.EXPLICIT_TIMEOUT) / 3;
augmentedDriver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS);
try {
LOGGER.debug("starting full size screenshot capturing...");
screenShot = (BufferedImage) future.get(timeout, TimeUnit.SECONDS);
} catch (java.util.concurrent.TimeoutException e) {
LOGGER.error("Unable to capture full screenshot during " + timeout + "sec!");
} catch (InterruptedException e) {
LOGGER.error("Unable to capture full screenshot during " + timeout + "sec!");
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
if (e.getMessage() != null) {
String msg = e.getMessage();
if (msg.contains(SpecialKeywords.DRIVER_CONNECTION_REFUSED)
|| msg.contains(SpecialKeywords.DRIVER_CONNECTION_REFUSED2)) {
LOGGER.error("Unable to capture full screenshot due to the driver connection refused!");
} else if (msg.contains(SpecialKeywords.DRIVER_NO_SUCH_WINDOW)) {
LOGGER.error("Unable to capture full screenshot due to the " + SpecialKeywords.DRIVER_NO_SUCH_WINDOW);
if (driver.getClass().toString().contains("windows")) {
File screenshot = ((WindowsDriver<?>) driver).getScreenshotAs(OutputType.FILE);
screenShot = ImageIO.read(screenshot);
} else if (driver.getClass().toString().contains("java_client")) {
// Mobile Native app
File screenshot = ((AppiumDriver<?>) driver).getScreenshotAs(OutputType.FILE);
screenShot = ImageIO.read(screenshot);
} else if (Configuration.getDriverType().equals(SpecialKeywords.MOBILE)) {
ru.yandex.qatools.ashot.Screenshot screenshot;
if (Configuration.getPlatform().equals("ANDROID")) {
String pixelRatio = String.valueOf(((EventFiringWebDriver) augmentedDriver).getCapabilities().getCapability("pixelRatio"));
if (!pixelRatio.equals("null")) {
float dpr = Float.parseFloat(pixelRatio);
screenshot = (new AShot()).shootingStrategy(ShootingStrategies
.viewportRetina(SpecialKeywords.DEFAULT_SCROLL_TIMEOUT, SpecialKeywords.DEFAULT_BLOCK, SpecialKeywords.DEFAULT_BLOCK,
dpr))
.takeScreenshot(augmentedDriver);
screenShot = screenshot.getImage();
} else {
screenshot = (new AShot()).shootingStrategy(ShootingStrategies
.viewportRetina(SpecialKeywords.DEFAULT_SCROLL_TIMEOUT, SpecialKeywords.DEFAULT_BLOCK, SpecialKeywords.DEFAULT_BLOCK,
SpecialKeywords.DEFAULT_DPR))
.takeScreenshot(augmentedDriver);
screenShot = screenshot.getImage();
}
} else {
LOGGER.error("ExecutionException error detected on capture full screenshot!", e);
int deviceWidth = augmentedDriver.manage().window().getSize().getWidth();
String deviceName = "";
if (augmentedDriver instanceof EventFiringWebDriver) {
deviceName = String.valueOf(((EventFiringWebDriver) augmentedDriver).getCapabilities().getCapability("deviceName"));
} else if (augmentedDriver instanceof RemoteWebDriver) {
deviceName = String.valueOf(((RemoteWebDriver) augmentedDriver).getCapabilities().getCapability("deviceName"));
}
screenshot = new AShot().shootingStrategy(getScreenshotShuttingStrategy(deviceWidth, deviceName)).takeScreenshot(augmentedDriver);
screenShot = screenshot.getImage();
}
} else {
// regular web
ru.yandex.qatools.ashot.Screenshot screenshot;
screenshot = (new AShot()).shootingStrategy(ShootingStrategies.viewportPasting(SpecialKeywords.DEFAULT_SCROLL_TIMEOUT))
.takeScreenshot(augmentedDriver);
screenShot = screenshot.getImage();
}
} catch (Exception e) {
// for undefined failure keep full stacktrace to handle later correctly!
LOGGER.error("Undefined error on capture full screenshot detected!", e);
} finally {
LOGGER.debug("finished full size screenshot call.");
//restore default pageLoadTimeout driver timeout
augmentedDriver.manage().timeouts().pageLoadTimeout(getPageLoadTimeout(), TimeUnit.SECONDS);
LOGGER.debug("finished full size screenshot call.");
}
return screenShot;
}
Expand All @@ -574,43 +549,24 @@ public BufferedImage call() throws IOException {
* @return screenshot image
*/
private static BufferedImage takeVisibleScreenshot(WebDriver augmentedDriver) throws Exception {
Future<?> future = Executors.newSingleThreadExecutor().submit(new Callable<BufferedImage>() {
public BufferedImage call() throws IOException {
return ImageIO.read(((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE));
}
});

BufferedImage screenShot = null;
// default timeout for driver quit 1/3 of explicit
long timeout = Configuration.getInt(Parameter.EXPLICIT_TIMEOUT) / 3;
augmentedDriver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS);

try {
LOGGER.debug("starting screenshot capturing...");
screenShot = (BufferedImage) future.get(timeout, TimeUnit.SECONDS);
} catch (java.util.concurrent.TimeoutException e) {
screenShot = ImageIO.read(((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE));
} catch (TimeoutException e) {
LOGGER.warn("Unable to capture screenshot during " + timeout + " sec!");
} catch (InterruptedException e) {
String message = "Unable to capture screenshot during " + timeout + " sec!";
LOGGER.warn(message);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
if (e.getMessage() != null) {
String msg = e.getMessage();
if (msg.contains(SpecialKeywords.DRIVER_CONNECTION_REFUSED)
|| msg.contains(SpecialKeywords.DRIVER_CONNECTION_REFUSED2)) {
LOGGER.error("ExecutionException error on capture screenshot due to the driver connection refused");
} else if (msg.contains(SpecialKeywords.DRIVER_NO_SUCH_WINDOW)) {
LOGGER.error("ExecutionException error on capture screenshot due to the " + SpecialKeywords.DRIVER_NO_SUCH_WINDOW);
} else {
//TODO: clear log messages
LOGGER.error("ExecutionException error detected on capture visible screenshot: " + e.getMessage(), e);
LOGGER.info(e.getCause().toString());
}
}
} catch (Exception e) {
String message = "Undefined error on capture screenshot detected: " + e.getMessage();
LOGGER.error(message);
} finally {
LOGGER.debug("finished screenshot call.");
//restore default pageLoadTimeout driver timeout
augmentedDriver.manage().timeouts().pageLoadTimeout(getPageLoadTimeout(), TimeUnit.SECONDS);
LOGGER.debug("finished screenshot call.");
}
return screenShot;

Expand Down Expand Up @@ -797,4 +753,15 @@ private static WebDriver castDriver(WebDriver drv) {
return drv;
}

private static long getPageLoadTimeout() {
long timeout = 300;
if (!R.CONFIG.get("capabilities.idleTimeout").isEmpty()) {
long idleTimeout = R.CONFIG.getLong("capabilities.idleTimeout");
if (idleTimeout < timeout) {
timeout = idleTimeout;
}
}
return timeout;
}

}

0 comments on commit d49f5ed

Please sign in to comment.