public BufferedImage takeScreenshotScrollVertically() { BufferedImage combinedImage = new BufferedImage(this.getViewportWidth(), this.getDocHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = combinedImage.createGraphics(); int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight()); for (int j = 0; j < verticalIterations; j++) { this.scrollTo(0, j * this.getViewportHeight()); wait(scrollTimeout); Image image = takeScreenshot(); g.drawImage(image, 0, this.getCurrentScrollY(), null); if(this.getDocHeight() == image.getHeight(null)){ break; } } g.dispose(); return combinedImage; }
public BufferedImage takeScreenshotScrollHorizontally() { BufferedImage combinedImage = new BufferedImage(this.getDocWidth(), this.getViewportHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = combinedImage.createGraphics(); int horizontalIterations = (int) Math.ceil(((double) this.getDocWidth()) / this.getViewportWidth()); for (int i = 0; i < horizontalIterations; i++) { this.scrollTo(i * this.getViewportWidth(), 0); wait(scrollTimeout); Image image = takeScreenshot(); g.drawImage(image, this.getCurrentScrollX(), 0, null); if(this.getDocWidth() == image.getWidth(null)){ break; } } g.dispose(); return combinedImage; }
public BufferedImage takeScreenshotEntirePageUsingChromeCommand() { this.driver = getDriverAfterValidation(this.driver); try { CommandInfo cmd = new CommandInfo("/session/:sessionId/chromium/send_command_and_get_result", HttpMethod.POST); Method defineCommand = HttpCommandExecutor.class.getDeclaredMethod("defineCommand", String.class, CommandInfo.class); defineCommand.setAccessible(true); defineCommand.invoke(((RemoteWebDriver) this.driver).getCommandExecutor(), "sendCommand", cmd); } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { throw new RuntimeException(e); } int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight()); for (int j = 0; j < verticalIterations; j++) { this.scrollTo(0, j * this.getViewportHeight()); wait(scrollTimeout); } Object metrics = this.evaluate(FileUtil.getJsScript(ALL_METRICS)); this.sendCommand("Emulation.setDeviceMetricsOverride", metrics); Object result = this.sendCommand("Page.captureScreenshot", ImmutableMap.of("format", "png", "fromSurface", true)); String base64EncodedPng = (String) ((Map<String, ?>) result).get("data"); InputStream in = new ByteArrayInputStream(OutputType.BYTES.convertFromBase64Png(base64EncodedPng)); BufferedImage bImageFromConvert; try { bImageFromConvert = ImageIO.read(in); } catch (IOException e) { throw new RuntimeException("Error while converting results from bytes to BufferedImage"); } return bImageFromConvert; }
public BufferedImage takeScreenshotEntirePageUsingChromeCommand() { //should use devicePixelRatio by default as chrome command executor makes screenshot account for that Object devicePixelRatio = executeJsScript(DEVICE_PIXEL_RATIO); this.devicePixelRatio = devicePixelRatio instanceof Double? (Double)devicePixelRatio: (Long)devicePixelRatio*1.0; try { CommandInfo cmd = new CommandInfo("/session/:sessionId/chromium/send_command_and_get_result", HttpMethod.POST); Method defineCommand = HttpCommandExecutor.class.getDeclaredMethod("defineCommand", String.class, CommandInfo.class); defineCommand.setAccessible(true); defineCommand.invoke(((RemoteWebDriver) this.driver).getCommandExecutor(), "sendCommand", cmd); } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { throw new RuntimeException(e); } int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight()); for (int j = 0; j < verticalIterations; j++) { this.scrollTo(0, j * this.getViewportHeight()); wait(scrollTimeout); } Object metrics = this.evaluate(FileUtil.getJsScript(ALL_METRICS)); this.sendCommand("Emulation.setDeviceMetricsOverride", metrics); Object result = this.sendCommand("Page.captureScreenshot", ImmutableMap.of("format", "png", "fromSurface", true)); String base64EncodedPng = (String) ((Map<String, ?>) result).get("data"); InputStream in = new ByteArrayInputStream(OutputType.BYTES.convertFromBase64Png(base64EncodedPng)); BufferedImage bImageFromConvert; try { bImageFromConvert = ImageIO.read(in); } catch (IOException e) { throw new RuntimeException("Error while converting results from bytes to BufferedImage"); } return bImageFromConvert; }
public BufferedImage takeScreenshotEntirePage() { final int _docWidth = this.getDocWidth(); final int _docHeight = this.getDocHeight(); BufferedImage combinedImage = new BufferedImage(_docWidth, _docHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = combinedImage.createGraphics(); int _viewportWidth = this.getViewportWidth(); int _viewportHeight = this.getViewportHeight(); final int scrollBarMaxWidth = 40; // this is probably too high, but better to be safe than sorry if (_viewportWidth < _docWidth || (_viewportHeight < _docHeight && _viewportWidth - scrollBarMaxWidth < _docWidth)) _viewportHeight-=scrollBarMaxWidth; // some space for a scrollbar if (_viewportHeight < _docHeight) _viewportWidth-=scrollBarMaxWidth; // some space for a scrollbar int horizontalIterations = (int) Math.ceil(((double) _docWidth) / _viewportWidth); int verticalIterations = (int) Math.ceil(((double) _docHeight) / _viewportHeight); outer_loop: for (int j = 0; j < verticalIterations; j++) { this.scrollTo(0, j * _viewportHeight); for (int i = 0; i < horizontalIterations; i++) { this.scrollTo(i * _viewportWidth, _viewportHeight * j); wait(scrollTimeout); Image image = takeScreenshot(); g.drawImage(image, this.getCurrentScrollX(), this.getCurrentScrollY(), null); if(_docWidth == image.getWidth(null) && _docHeight == image.getHeight(null)){ break outer_loop; } } } g.dispose(); return combinedImage; }
public BufferedImage takeScreenshotEntirePageDefault() { final int _docWidth = this.getDocWidth(); final int _docHeight = this.getDocHeight(); BufferedImage combinedImage = new BufferedImage(_docWidth, _docHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = combinedImage.createGraphics(); int _viewportWidth = this.getViewportWidth(); int _viewportHeight = this.getViewportHeight(); final int scrollBarMaxWidth = 40; // this is probably too high, but better to be safe than sorry if (_viewportWidth < _docWidth || (_viewportHeight < _docHeight && _viewportWidth - scrollBarMaxWidth < _docWidth)) _viewportHeight-=scrollBarMaxWidth; // some space for a scrollbar if (_viewportHeight < _docHeight) _viewportWidth-=scrollBarMaxWidth; // some space for a scrollbar int horizontalIterations = (int) Math.ceil(((double) _docWidth) / _viewportWidth); int verticalIterations = (int) Math.ceil(((double) _docHeight) / _viewportHeight); outer_loop: for (int j = 0; j < verticalIterations; j++) { this.scrollTo(0, j * _viewportHeight); for (int i = 0; i < horizontalIterations; i++) { this.scrollTo(i * _viewportWidth, _viewportHeight * j); wait(scrollTimeout); Image image = takeScreenshot(); g.drawImage(image, this.getCurrentScrollX(), this.getCurrentScrollY(), null); if(_docWidth == image.getWidth(null) && _docHeight == image.getHeight(null)){ break outer_loop; } } } g.dispose(); return combinedImage; }