Best Karate code snippet using com.intuit.karate.robot.Region.screenshot
Source:RobotBase.java
...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 }...
screenshot
Using AI Code Generation
1* def region = com.intuit.karate.robot.Region(0, 0, 100, 100)2* def image = region.screenshot()3* def region = com.intuit.karate.robot.Region(0, 0, 100, 100)4* def image = region.screenshot()5* def region = com.intuit.karate.robot.Region(0, 0, 100, 100)6* def image = region.screenshot()7* def region = com.intuit.karate.robot.Region(0, 0, 100, 100)8* def image = region.screenshot()9* def region = com.intuit.karate.robot.Region(0, 0, 100, 100)10* def image = region.screenshot()11* def region = com.intuit.karate.robot.Region(0, 0, 100, 100)12* def image = region.screenshot()13* def region = com.intuit.karate.robot.Region(0, 0, 100, 100)14* def image = region.screenshot()
screenshot
Using AI Code Generation
1* def region = karate.call('classpath:com/intuit/karate/robot/region.feature@init')2* def screenshot = region.screenshot()3* def screenshot2 = region.screenshot('png')4* def screenshot3 = region.screenshot('jpg')5* def screenshot4 = region.screenshot('gif')6* def screenshot5 = region.screenshot('bmp')7* def screenshot6 = region.screenshot(0, 0, 100, 100)8* def screenshot7 = region.screenshot(0, 0, 100, 100, 'png')9* def screenshot8 = region.screenshot(0, 0, 100, 100, 'jpg')10* def screenshot9 = region.screenshot(0, 0, 100, 100, 'gif')11* def screenshot10 = region.screenshot(0, 0, 100, 100, 'bmp')12* def screenshot11 = region.screenshot(0, 0, 100, 100, 0.5)13* def screenshot12 = region.screenshot(0, 0, 100, 100, 0.5, 'png')14* def screenshot13 = region.screenshot(0, 0, 100, 100, 0.5, 'jpg')15* def screenshot14 = region.screenshot(0, 0, 100, 100, 0.5, 'gif')16* def screenshot15 = region.screenshot(0, 0, 100, 100, 0.5, 'bmp')17* def screenshot16 = region.screenshot(0, 0, 100, 100, 0.5, 0.5)18* def screenshot17 = region.screenshot(0, 0, 100, 100, 0.5, 0.5, 'png')19* def screenshot18 = region.screenshot(0, 0, 100, 100, 0.5, 0.5, 'jpg')20* def screenshot19 = region.screenshot(0, 0, 100, 100, 0.5, 0.5, 'gif')21* def screenshot20 = region.screenshot(0, 0, 100, 100, 0.5, 0.5, 'bmp')22* def screenshot21 = region.screenshot(0,
screenshot
Using AI Code Generation
1* def region = karate.get('region')2* def screenshot = region.screenshot()3* def screenshotFile = karate.writeToFile(screenshot, 'png')4* def screenshotBase64 = karate.toBase64(screenshot)5* def region = karate.get('region')6* def screenshot = region.screenshot()7* def screenshotFile = karate.writeToFile(screenshot, 'png')8* def screenshotBase64 = karate.toBase64(screenshot)9* def region = karate.get('region')10* def screenshot = region.screenshot()11* def screenshotFile = karate.writeToFile(screenshot, 'png')12* def screenshotBase64 = karate.toBase64(screenshot)13* def region = karate.get('region')14* def screenshot = region.screenshot()15* def screenshotFile = karate.writeToFile(screenshot, 'png')16* def screenshotBase64 = karate.toBase64(screenshot)17* def region = karate.get('region')18* def screenshot = region.screenshot()19* def screenshotFile = karate.writeToFile(screenshot, 'png')20* def screenshotBase64 = karate.toBase64(screenshot)21* def region = karate.get('region')22* def screenshot = region.screenshot()23* def screenshotFile = karate.writeToFile(screenshot, 'png')24* def screenshotBase64 = karate.toBase64(screenshot)25* def region = karate.get('region')26* def screenshot = region.screenshot()27* def screenshotFile = karate.writeToFile(screenshot, 'png')28* def screenshotBase64 = karate.toBase64(screenshot)
screenshot
Using AI Code Generation
1 * def region = new com.intuit.karate.robot.Region()2 * def screen = region.getScreen()3 * def img = screen.capture(0, 0, 100, 200)4 * img.save('region.png')5 * def img = screen.capture(0, 0, 100, 200, 1000)6 * img.save('region-delay.png')7 * def img = screen.capture(0, 0, 100, 200, 1000, 'region-delay-file.png')8 * img.save('region-delay-file.png')9 * def img = screen.capture(0, 0, 100, 200, 1000, 'region-delay-file.png')10 * img.save('region-delay-file.png')11 * def img = screen.capture(0, 0, 100, 200, 1000, 'region-delay-file.png')12 * img.save('region-delay-file.png')13 * def path = img.save('region-delay-file.png')14 * def img = screen.capture(0, 0, 100, 200, 1000, 'region-delay-file.png')15 * img.save('region-delay-file.png')16 * def path = img.save('region-delay-file.png')17 * def img = screen.capture(0, 0, 100, 200, 1000, 'region-delay-file.png')18 * img.save('region-delay-file.png')19 * def path = img.save('region-delay-file.png')
screenshot
Using AI Code Generation
1import com.intuit.karate.robot.Region2def region = Region()3region.screenshot('screenshot.png')4import com.intuit.karate.robot.Region5def region = Region(10, 10, 200, 200)6region.screenshot('screenshot.png')7import com.intuit.karate.robot.Region8def region = Region(10, 10, 200, 200)9def image = region.screenshot('screenshot.png')10import com.intuit.karate.robot.Region11def region = Region(10, 10, 200, 200)12def image = region.screenshot('screenshot.png')13import com.intuit.karate.robot.Region14def region = Region()15region.screenshot('screenshot.png')16import com.intuit.karate.robot.Region17def region = Region(10, 10, 200, 200)18region.screenshot('screenshot.png')
screenshot
Using AI Code Generation
1And screenshot 'google.png' { width: 600, height: 400 }2And screenshot 'google.png' { width: 600, height: 400, x: 200, y: 100 }3And screenshot 'google.png' { width: 600, height: 400, x: 200, y: 100, quality: 0.8 }4And screenshot 'google.png' { width: 600, height: 400, x: 200, y: 100, quality: 0.8, format: 'png' }5And screenshot 'google.png' { width: 600, height: 400, x: 200, y: 100, quality: 0.8, format: 'png', type: 'image/png' }6And screenshot { width: 600, height: 400, x: 200, y: 100, quality: 0.8, format: 'png', type: 'image/png' }7And screenshot 'google.png' { width: 600, height: 400, x: 200, y: 100, quality: 0.8, format: 'png', type: 'image/png' }8And screenshot { width: 600, height: 400, x: 200, y: 100, quality: 0.8, format: 'png', type: 'image/png' }9And screenshot 'google.png' { width: 600, height: 400, x: 200
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!