How to use resizeScreenshotIfNeeded method of com.galenframework.utils.GalenUtils class

Best Galen code snippet using com.galenframework.utils.GalenUtils.resizeScreenshotIfNeeded

Source:GalenUtils.java Github

copy

Full Screen

...142 resultingImage = image;143 }144 if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) {145 try {146 resultingImage = GalenUtils.resizeScreenshotIfNeeded(driver, resultingImage);147 } catch (Exception ex) {148 LOG.trace("Couldn't resize screenshot", ex);149 }150 }151 ImageIO.write(resultingImage, "png", file);152 return file;153 }154 /**155 * Check the devicePixelRatio and adapts the size of the screenshot as if the ratio was 1.0156 * @param driver157 * @param screenshotImage158 * @return159 */160 public static BufferedImage resizeScreenshotIfNeeded(WebDriver driver, BufferedImage screenshotImage) {161 Double devicePixelRatio = 1.0;162 try {163 devicePixelRatio = ((Number) ((JavascriptExecutor) driver).executeScript(JS_RETRIEVE_DEVICE_PIXEL_RATIO)).doubleValue();164 } catch (Exception ex) {165 ex.printStackTrace();166 }167 if (devicePixelRatio > 1.0 && screenshotImage.getWidth() > 0) {168 Long screenSize = ((Number) ((JavascriptExecutor) driver).executeScript("return Math.max(" +169 "document.body.scrollWidth, document.documentElement.scrollWidth," +170 "document.body.offsetWidth, document.documentElement.offsetWidth," +171 "document.body.clientWidth, document.documentElement.clientWidth);"172 )).longValue();173 Double estimatedPixelRatio = ((double)screenshotImage.getWidth()) / ((double)screenSize);174 if (estimatedPixelRatio > 1.0) {175 int newWidth = (int) (screenshotImage.getWidth() / estimatedPixelRatio);176 int newHeight = (int) (screenshotImage.getHeight() / estimatedPixelRatio);177 Image tmp = screenshotImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);178 BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);179 Graphics2D g2d = scaledImage.createGraphics();180 g2d.drawImage(tmp, 0, 0, null);181 g2d.dispose();182 return scaledImage;183 }184 else return screenshotImage;185 }186 else return screenshotImage;187 }188 public static void scrollVerticallyTo(WebDriver driver, int scroll) {189 ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, " + scroll + ");");190 try {191 waitUntilItIsScrolledToPosition(driver, scroll);192 } catch (InterruptedException e) {193 LOG.trace("Interrupt error during scrolling occurred.", e);194 }195 }196 private static void waitUntilItIsScrolledToPosition(WebDriver driver, int scrollPosition) throws InterruptedException {197 int hardTime = GalenConfig.getConfig().getIntProperty(GalenProperty.SCREENSHOT_FULLPAGE_SCROLLWAIT);198 if (hardTime > 0) {199 Thread.sleep(hardTime);200 }201 int time = GalenConfig.getConfig().getIntProperty(GalenProperty.SCREENSHOT_FULLPAGE_SCROLLTIMEOUT);202 boolean isScrolledToPosition = false;203 while(time >= 0 && !isScrolledToPosition) {204 Thread.sleep(50);205 time -= 50;206 isScrolledToPosition = Math.abs(obtainVerticalScrollPosition(driver) - scrollPosition) < 3;207 }208 }209 private static int obtainVerticalScrollPosition(WebDriver driver) {210 Number scrollLong = (Number) ((JavascriptExecutor)driver).executeScript("return (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;");211 return scrollLong.intValue();212 }213 public static String convertToFileName(String name) {214 return name.toLowerCase().replaceAll("[^\\dA-Za-z\\.\\-]", " ").replaceAll("\\s+", "-");215 }216 217 /**218 * Needed for Javascript based tests219 * @param browserType220 * @return221 */222 public static WebDriver createDriver(String browserType, String url, String size) {223 if (browserType == null) { 224 browserType = GalenConfig.getConfig().getDefaultBrowser();225 }226 227 SeleniumBrowser browser = (SeleniumBrowser) new SeleniumBrowserFactory(browserType).openBrowser();228 229 if (url != null && !url.trim().isEmpty()) {230 browser.load(url); 231 }232 233 if (size != null && !size.trim().isEmpty()) {234 browser.changeWindowSize(GalenUtils.readSize(size));235 }236 237 return browser.getDriver();238 }239 240 public static WebDriver createGridDriver(String gridUrl, String browserName, String browserVersion, String platform, Map<String, String> desiredCapabilities, String size) {241 SeleniumGridBrowserFactory factory = new SeleniumGridBrowserFactory(gridUrl);242 factory.setBrowser(browserName);243 factory.setBrowserVersion(browserVersion);244 245 if (platform != null) {246 factory.setPlatform(Platform.valueOf(platform));247 }248 249 if (desiredCapabilities != null) {250 factory.setDesiredCapabilites(desiredCapabilities);251 }252 253 WebDriver driver = ((SeleniumBrowser)factory.openBrowser()).getDriver();254 255 GalenUtils.resizeDriver(driver, size);256 return driver;257 }258 259 public static void resizeDriver(WebDriver driver, String sizeText) {260 if (sizeText != null && !sizeText.trim().isEmpty()) {261 Dimension size = GalenUtils.readSize(sizeText);262 resizeDriver(driver, size.width, size.height);263 }264 }265 public static void resizeDriver(WebDriver driver, int width, int height) {266 if (GalenConfig.getConfig().getBooleanProperty(GalenProperty.GALEN_BROWSER_VIEWPORT_ADJUSTSIZE)) {267 GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, width, height);268 } else {269 driver.manage().window().setSize(new org.openqa.selenium.Dimension(width, height));270 }271 }272 public static File takeScreenshot(WebDriver driver) throws IOException {273 File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);274 if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) {275 BufferedImage image = Rainbow4J.loadImage(file.getAbsolutePath());276 File newFile = File.createTempFile("screenshot", ".png");277 image = GalenUtils.resizeScreenshotIfNeeded(driver, image);278 Rainbow4J.saveImage(image, newFile);279 return newFile;280 }281 else return file;282 }283 284 public static Properties loadProperties(String fileName) throws IOException {285 286 GalenProperties properties = null;287 if (TestSession.current() != null) {288 properties = TestSession.current().getProperties();289 }290 else properties = new GalenProperties();291 ...

Full Screen

Full Screen

resizeScreenshotIfNeeded

Using AI Code Generation

copy

Full Screen

1import com.galenframework.utils.GalenUtils;2GalenUtils.resizeScreenshotIfNeeded(driver, "C:\\Users\\user\\Desktop\\screenshot.png");3import com.galenframework.utils.GalenUtils;4GalenUtils.resizeScreenshotIfNeeded(driver, "C:\\Users\\user\\Desktop\\screenshot.png", 600, 800);5import com.galenframework.utils.GalenUtils;6GalenUtils.resizeScreenshotIfNeeded(driver, "C:\\Users\\user\\Desktop\\screenshot.png", 600, 800, 0.5);7import com.galenframework.utils.GalenUtils;8GalenUtils.resizeScreenshotIfNeeded(driver, "C:\\Users\\user\\Desktop\\screenshot.png", 600, 800, 0.5, 0.5);9import com.galenframework.utils.GalenUtils;10GalenUtils.resizeScreenshotIfNeeded(driver, "C:\\Users\\user\\Desktop\\screenshot.png", 600, 800, 0.5, 0.5, 0.5);11import com.galenframework.utils.GalenUtils;12GalenUtils.resizeScreenshotIfNeeded(driver, "C:\\Users\\user\\Desktop\\screenshot.png", 600, 800, 0.5, 0.5, 0.5, 0.5);13import com.galenframework.utils.GalenUtils;14GalenUtils.resizeScreenshotIfNeeded(driver, "C:\\Users\\user\\Desktop\\screenshot.png", 600, 800, 0.5, 0.5, 0.5, 0.5, 0.5);15import com.galenframework.utils.GalenUtils;16GalenUtils.resizeScreenshotIfNeeded(driver, "C:\\Users\\user\\Desktop\\screenshot.png", 600, 800, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5);17import com.galenframework.utils.GalenUtils;

Full Screen

Full Screen

resizeScreenshotIfNeeded

Using AI Code Generation

copy

Full Screen

1import com.galenframework.utils.GalenUtils;2import org.openqa.selenium.OutputType;3import org.openqa.selenium.TakesScreenshot;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.firefox.FirefoxDriver;6import org.openqa.selenium.remote.RemoteWebDriver;7import javax.imageio.ImageIO;8import java.io.File;9import java.io.IOException;10public class TestGalenUtils {11 public static void main(String[] args) throws IOException {12 WebDriver driver = new FirefoxDriver();13 byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);14 File screenshotFile = new File("screenshot.png");15 ImageIO.write(GalenUtils.resizeScreenshotIfNeeded(screenshot, 1024), "png", screenshotFile);16 driver.quit();17 }18}19import com.galenframework.utils.GalenUtils;20import org.openqa.selenium.OutputType;21import org.openqa.selenium.TakesScreenshot;22import org.openqa.selenium.WebDriver;23import org.openqa.selenium.firefox.FirefoxDriver;24import org.openqa.selenium.remote.RemoteWebDriver;25import javax.imageio.ImageIO;26import java.io.File;27import java.io.IOException;28public class TestGalenUtils {29 public static void main(String[] args) throws IOException {30 WebDriver driver = new FirefoxDriver();31 byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);32 File screenshotFile = new File("screenshot.jpg");33 ImageIO.write(GalenUtils.resizeScreenshotIfNeeded(screenshot, 1024), "jpg", screenshotFile);34 driver.quit();35 }36}

Full Screen

Full Screen

resizeScreenshotIfNeeded

Using AI Code Generation

copy

Full Screen

1import com.galenframework.utils.GalenUtils;2import java.awt.image.BufferedImage;3import java.io.File;4import java.io.IOException;5import javax.imageio.ImageIO;6import org.openqa.selenium.OutputType;7import org.openqa.selenium.TakesScreenshot;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.chrome.ChromeDriver;10public class GalenTest {11 public static void main(String[] args) throws IOException {12 System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");13 WebDriver driver = new ChromeDriver();14 BufferedImage image = ImageIO.read(new File("C:\\Users\\User\\Desktop\\galenframework.com.png"));15 BufferedImage resizedImage = GalenUtils.resizeScreenshotIfNeeded(image, 600, 400);16 ImageIO.write(resizedImage, "png", new File("C:\\Users\\User\\Desktop\\galenframework.com_resized.png"));17 driver.quit();18 }19}

Full Screen

Full Screen

resizeScreenshotIfNeeded

Using AI Code Generation

copy

Full Screen

1import com.galenframework.utils.GalenUtils2import com.galenframework.utils.GalenUtils.resizeScreenshotIfNeeded3import org.openqa.selenium.WebDriver4import java.awt.image.BufferedImage5import java.io.File6import java.nio.file.Files7import static com.galenframework.utils.GalenUtils.resizeScreenshotIfNeeded8def driver = createDriver()9driver.manage().window().setSize(new Dimension(browserWidth, browserHeight))10def screenshot = driver.getScreenshotAs(OutputType.FILE)11def image = resizeScreenshotIfNeeded(screenshot, browserWidth)12image.saveAs(new File("screenshot.png"))13driver.quit()14def resizeScreenshotIfNeeded(File screenshot, int width) {15 def image = ImageIO.read(screenshot)16 def resizedImage = GalenUtils.resizeImage(image, width)17}18def saveAs(BufferedImage image, File file) {19 ImageIO.write(image, "png", file)20}21def createDriver() {22 def driver = new ChromeDriver()23 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)24}25def resizeImage(BufferedImage image, int width) {26 def height = (width * image.getHeight()) / image.getWidth()27 def resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)28 def g = resizedImage.createGraphics()29 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)30 g.drawImage(image, 0, 0, width, height, 0, 0, image.getWidth(), image.getHeight(), null)31 g.dispose()32}33import com.galenframework.utils.GalenUtils

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful