How to use screenshot method of com.intuit.karate.driver.MissingElement class

Best Karate code snippet using com.intuit.karate.driver.MissingElement.screenshot

Source:RobotBase.java Github

copy

Full Screen

...58 public final Toolkit toolkit;59 public final Dimension dimension;60 public final Map<String, Object> options;61 public final boolean autoClose;62 public final boolean screenshotOnFailure;63 public final int autoDelay;64 public final Region screen;65 public final String tessData;66 public final String tessLang;67 // mutables68 private String basePath;69 protected Command command;70 protected ScenarioEngine engine;71 protected Logger logger;72 protected Element currentWindow;73 // retry74 private boolean retryEnabled;75 private Integer retryIntervalOverride = null;76 private Integer retryCountOverride = null;77 // debug78 protected boolean debug;79 public boolean highlight;80 public int highlightDuration;81 public void setDebug(boolean debug) {82 this.debug = debug;83 }84 public void setHighlight(boolean highlight) {85 this.highlight = highlight;86 }87 public void setHighlightDuration(int highlightDuration) {88 this.highlightDuration = highlightDuration;89 }90 public void disableRetry() {91 retryEnabled = false;92 retryCountOverride = null;93 retryIntervalOverride = null;94 }95 public void enableRetry(Integer count, Integer interval) {96 retryEnabled = true;97 retryCountOverride = count; // can be null98 retryIntervalOverride = interval; // can be null99 }100 private int getRetryCount() {101 return retryCountOverride == null ? engine.getConfig().getRetryCount() : retryCountOverride;102 }103 private int getRetryInterval() {104 return retryIntervalOverride == null ? engine.getConfig().getRetryInterval() : retryIntervalOverride;105 }106 private <T> T get(String key, T defaultValue) {107 T temp = (T) options.get(key);108 return temp == null ? defaultValue : temp;109 }110 public Logger getLogger() {111 return logger;112 } 113 public RobotBase(ScenarioRuntime runtime) {114 this(runtime, Collections.EMPTY_MAP);115 }116 public RobotBase(ScenarioRuntime runtime, Map<String, Object> options) {117 this.engine = runtime.engine;118 this.logger = runtime.logger;119 try {120 this.options = options;121 basePath = get("basePath", null);122 highlight = get("highlight", false);123 highlightDuration = get("highlightDuration", Config.DEFAULT_HIGHLIGHT_DURATION);124 autoDelay = get("autoDelay", 0);125 tessData = get("tessData", "tessdata");126 tessLang = get("tessLang", "eng");127 toolkit = Toolkit.getDefaultToolkit();128 dimension = toolkit.getScreenSize();129 screen = new Region(this, 0, 0, dimension.width, dimension.height);130 logger.debug("screen dimensions: {}", screen);131 robot = new java.awt.Robot();132 robot.setAutoDelay(autoDelay);133 robot.setAutoWaitForIdle(true);134 //==================================================================135 screenshotOnFailure = get("screenshotOnFailure", true);136 autoClose = get("autoClose", true);137 boolean attach = get("attach", true);138 String window = get("window", null);139 if (window != null) {140 currentWindow = window(window, false, false); // don't retry141 }142 if (currentWindow != null && attach) {143 logger.debug("window found, will re-use: {}", window);144 } else {145 Variable v = new Variable(options.get("fork"));146 if (v.isString()) {147 command = engine.fork(true, v.getAsString());148 } else if (v.isList()) {149 command = engine.fork(true, v.<List>getValue());150 } else if (v.isMap()) {151 command = engine.fork(true, v.<Map>getValue());152 }153 if (command != null) {154 delay(500); // give process time to start155 if (command.isFailed()) {156 throw new KarateException("robot fork command failed: " + command.getFailureReason().getMessage());157 }158 if (window != null) {159 retryCountOverride = get("retryCount", null);160 retryIntervalOverride = get("retryInterval", null);161 currentWindow = window(window); // will retry162 logger.debug("attached to process window: {} - {}", currentWindow, command.getArgList());163 }164 }165 if (currentWindow == null && window != null) {166 throw new KarateException("failed to find window: " + window);167 }168 }169 } catch (Exception e) {170 String message = "robot init failed: " + e.getMessage();171 throw new KarateException(message, e);172 }173 }174 public <T> T retry(Supplier<T> action, Predicate<T> condition, String logDescription, boolean failWithException) {175 long startTime = System.currentTimeMillis();176 int count = 0, max = getRetryCount();177 int interval = getRetryInterval();178 disableRetry(); // always reset179 T result;180 boolean success;181 do {182 if (count > 0) {183 logger.debug("{} - retry #{}", logDescription, count);184 delay(interval);185 }186 result = action.get();187 success = condition.test(result);188 } while (!success && count++ < max);189 if (!success) {190 long elapsedTime = System.currentTimeMillis() - startTime;191 String message = logDescription + ": failed after " + (count - 1) + " retries and " + elapsedTime + " milliseconds";192 logger.warn(message);193 if (failWithException) {194 throw new RuntimeException(message);195 }196 }197 return result;198 }199 public void setBasePath(String basePath) {200 this.basePath = basePath;201 }202 private byte[] readBytes(String path) {203 if (basePath != null) {204 String slash = basePath.endsWith(":") ? "" : "/";205 path = basePath + slash + path;206 }207 return engine.fileReader.readFileAsBytes(path);208 }209 @Override210 public void onFailure(StepResult stepResult) {211 if (screenshotOnFailure && !stepResult.isWithCallResults()) {212 byte[] bytes = screenshot();213 214 }215 }216 @Override217 public Robot retry() {218 return retry(null, null);219 }220 @Override221 public Robot retry(int count) {222 return retry(count, null);223 }224 @Override225 public Robot retry(Integer count, Integer interval) {226 enableRetry(count, interval);227 return this;228 }229 @Override230 public Robot delay(int millis) {231 robot.delay(millis);232 return this;233 }234 private static int mask(int num) {235 switch (num) {236 case 2:237 return InputEvent.BUTTON2_DOWN_MASK;238 case 3:239 return InputEvent.BUTTON3_DOWN_MASK;240 default:241 return InputEvent.BUTTON1_DOWN_MASK;242 }243 }244 @Override245 public Robot click() {246 return click(1);247 }248 @Override249 public Robot rightClick() {250 return click(3);251 }252 @Override253 public Robot click(int num) {254 int mask = mask(num);255 robot.mousePress(mask);256 if (highlight) {257 getLocation().highlight(highlightDuration);258 int toDelay = CLICK_POST_DELAY - highlightDuration;259 if (toDelay > 0) {260 RobotUtils.delay(toDelay);261 }262 } else {263 RobotUtils.delay(CLICK_POST_DELAY);264 }265 robot.mouseRelease(mask);266 return this;267 }268 @Override269 public Robot doubleClick() {270 click();271 delay(40);272 click();273 return this;274 }275 @Override276 public Robot press() {277 robot.mousePress(1);278 return this;279 }280 @Override281 public Robot release() {282 robot.mouseRelease(1);283 return this;284 }285 @Override286 public Robot input(String[] values) {287 return input(values, 0);288 }289 @Override290 public Robot input(String chars, int delay) {291 String[] array = new String[chars.length()];292 for (int i = 0; i < array.length; i++) {293 array[i] = Character.toString(chars.charAt(i));294 }295 return input(array, delay);296 }297 @Override298 public Robot input(String[] values, int delay) {299 for (String s : values) {300 if (delay > 0) {301 delay(delay);302 }303 input(s);304 }305 return this;306 }307 @Override308 public Robot input(String value) {309 if (highlight) {310 getFocused().highlight(highlightDuration);311 }312 StringBuilder sb = new StringBuilder();313 for (char c : value.toCharArray()) {314 if (Keys.isModifier(c)) {315 sb.append(c);316 int[] codes = RobotUtils.KEY_CODES.get(c);317 if (codes == null) {318 logger.warn("cannot resolve char: {}", c);319 robot.keyPress(c);320 } else {321 robot.keyPress(codes[0]);322 }323 continue;324 }325 int[] codes = RobotUtils.KEY_CODES.get(c);326 if (codes == null) {327 logger.warn("cannot resolve char: {}", c);328 robot.keyPress(c);329 robot.keyRelease(c);330 } else if (codes.length > 1) {331 robot.keyPress(codes[0]);332 robot.keyPress(codes[1]);333 robot.keyRelease(codes[1]);334 robot.keyRelease(codes[0]);335 } else {336 robot.keyPress(codes[0]);337 robot.keyRelease(codes[0]);338 }339 }340 for (char c : sb.toString().toCharArray()) {341 int[] codes = RobotUtils.KEY_CODES.get(c);342 if (codes == null) {343 logger.warn("cannot resolve char: {}", c);344 robot.keyRelease(c);345 } else {346 robot.keyRelease(codes[0]);347 }348 }349 return this;350 }351 public Robot clearFocused() {352 return input(Keys.CONTROL + "a" + Keys.DELETE);353 }354 protected int getHighlightDuration() {355 return highlight ? highlightDuration : -1;356 }357 @Override358 public Element input(String locator, String value) {359 return locate(locator).input(value);360 }361 @Override362 public byte[] screenshot() {363 return screenshot(screen);364 }365 @Override366 public byte[] screenshotActive() {367 return getActive().screenshot();368 }369 public byte[] screenshot(int x, int y, int width, int height) {370 return screenshot(new Region(this, x, y, width, height));371 }372 public byte[] screenshot(Region region) {373 BufferedImage image = region.capture();374 byte[] bytes = OpenCvUtils.toBytes(image);375 getRuntime().embed(bytes, ResourceType.PNG);376 return bytes;377 }378 @Override379 public Robot move(int x, int y) {380 robot.mouseMove(x, y);381 return this;382 }383 @Override384 public Robot click(int x, int y) {385 return move(x, y).click();386 }...

Full Screen

Full Screen

Source:ExportJob.java Github

copy

Full Screen

...105 completeViaSleep();106 }107 waitForComplete(chrome);108 name = handle + ".png";109 exportResult = chrome.screenshot();110 } catch (final RuntimeException e) {111 LOG.error("Unrecoverable failure while sending snapshot job to chrome instance.", e);112 throw new RuntimeException("Could not finish web document snapshot.", e);113 } finally {114 chrome.quit();115 }116 return new SnapshotJob(this);117 }118 public PrintJob print() {119 final HashMap<String, Object> printParams = new HashMap<>();120 printParams.put("printBackground", true);121 return print(printParams);122 }123 public PrintJob print(final Map<String, Object> printParams) {...

Full Screen

Full Screen

screenshot

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.driver.MissingElement;2import com.intuit.karate.driver.DriverOptions;3import com.intuit.karate.driver.Driver;4import com.intuit.karate.driver.Element;5import com.intuit.karate.driver.DriverOptions;6import com.intuit.karate.FileUtils;7import java.io.File;8import java.io.IOException;9import java.util.HashMap;10import java.util.Map;11import org.junit.Test;12import org.junit.runner.RunWith;13import com.intuit.karate.Results;14import com.intuit.karate.Runner;15import static org.junit.Assert.*;16public class 4 {17public void test4() {18Results results = Runner.path("classpath:4.feature").tags("~@ignore").parallel(1);19assertTrue(results.getErrorMessages(), results.getFailCount() == 0);20}21}22* def driver = Driver.start('chrome', DriverOptions.options().headless(false))23* def element = driver.element('input[name="q"]')24* element.type('karate')25* element.press('enter')26* def element2 = driver.element('div#resultStats')27* element2.screenshot('screenshot.png')28* def file = new File('screenshot.png')29* def actual = new MissingElement(driver, 'div#resultStats').screenshot()30* def expected = new MissingElement(driver, 'div#resultStats').screenshot()31* def actualFile = new File('actual.png')32* def expectedFile = new File('expected.png')33* def actualFileFile = new File('actualFile.png')34* def expectedFileFile = new File('expectedFile.png')

Full Screen

Full Screen

screenshot

Using AI Code Generation

copy

Full Screen

1package com.intuit.karate.driver;2import java.io.File;3import java.io.IOException;4import org.openqa.selenium.OutputType;5import org.openqa.selenium.TakesScreenshot;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import com.google.common.io.Files;9import com.intuit.karate.FileUtils;10public class MissingElement extends RuntimeException {11 private static final long serialVersionUID = 1L;12 private WebElement element;13 private String message;14 private String screenshot;15 public MissingElement(String message, WebElement element) {16 this.message = message;17 this.element = element;18 }19 public String getMessage() {20 return message;21 }22 public WebElement getElement() {23 return element;24 }25 public String getScreenshot() {26 return screenshot;27 }28 public void takeScreenshot(WebDriver driver) {29 File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);30 try {31 String path = FileUtils.getTempDirPath() + File.separator + "screenshot.png";32 Files.copy(file, new File(path));33 screenshot = path;34 } catch (IOException e) {35 throw new RuntimeException(e);36 }37 }38}39package com.intuit.karate.driver;40import java.io.File;41import java.io.IOException;42import org.openqa.selenium.OutputType;43import org.openqa.selenium.TakesScreenshot;44import org.openqa.selenium.WebDriver;45import org.openqa.selenium.WebElement;46import com.google.common.io.Files;47import com.intuit.karate.FileUtils;48public class MissingElement extends RuntimeException {49 private static final long serialVersionUID = 1L;50 private WebElement element;51 private String message;52 private String screenshot;53 public MissingElement(String message, WebElement element) {54 this.message = message;55 this.element = element;56 }57 public String getMessage() {58 return message;59 }60 public WebElement getElement() {61 return element;62 }63 public String getScreenshot() {64 return screenshot;65 }66 public void takeScreenshot(WebDriver driver) {67 File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);68 try {69 String path = FileUtils.getTempDirPath() + File.separator + "screenshot.png";70 Files.copy(file, new File(path));71 screenshot = path;72 } catch (IOException e

Full Screen

Full Screen

screenshot

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.driver.MissingElement;2import com.intuit.karate.driver.DriverOptions;3import com.intuit.karate.driver.Driver;4import com.intuit.karate.FileUtils;5import java.io.File;6import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8public class 4 {9 public static void main(String[] args) {10 DriverOptions options = DriverOptions.chrome();11 options.setHeadless(true);12 options.setHeadless(false);13 Driver driver = Driver.start(options);14 WebDriver webDriver = driver.getUnderlyingDriver();15 MissingElement element = new MissingElement(webDriver, By.id("foo"));16 File file = element.screenshot();17 FileUtils.copyFile(file, new File("foo.png"));18 driver.quit();19 }20}21import com.intuit.karate.driver.MissingElement;22import com.intuit.karate.driver.DriverOptions;23import com.intuit.karate.driver.Driver;24import com.intuit.karate.FileUtils;25import java.io.File;26import org.openqa.selenium.By;27import org.openqa.selenium.WebDriver;28public class 5 {29 public static void main(String[] args) {30 DriverOptions options = DriverOptions.chrome();31 options.setHeadless(true);32 options.setHeadless(false);33 Driver driver = Driver.start(options);34 WebDriver webDriver = driver.getUnderlyingDriver();35 MissingElement element = new MissingElement(webDriver, By.id("foo"));36 File file = element.screenshot();37 FileUtils.copyFile(file, new File("foo.png"));38 driver.quit();39 }40}41import com.intuit.karate.driver.MissingElement;42import com.intuit.karate.driver.DriverOptions;43import com.intuit.karate.driver.Driver;44import com.intuit.karate.FileUtils;45import java.io.File;46import org.openqa.selenium.By;47import org.openqa.selenium.WebDriver;48public class 6 {49 public static void main(String[] args) {

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