Best Karate code snippet using com.intuit.karate.driver.DevToolsMessage.param
Source:DevToolsDriver.java
...230 responseHeaders.add(nv);231 }232 }233 method("Fetch.fulfillRequest")234 .param("requestId", requestId)235 .param("responseCode", response.getStatus())236 .param("responseHeaders", responseHeaders)237 .param("body", responseBody).sendWithoutWaiting();238 } else {239 logger.warn("no mock server, continuing paused request to url: {}", requestUrl);240 method("Fetch.continueRequest").param("requestId", requestId).sendWithoutWaiting();241 }242 }243 //==========================================================================244 //245 private DevToolsMessage evalOnce(String expression, boolean quickly, boolean fireAndForget) {246 DevToolsMessage toSend = method("Runtime.evaluate")247 .param("expression", expression).param("returnByValue", true);248 if (executionContextId != null) {249 toSend.param("contextId", executionContextId);250 }251 if (quickly) {252 toSend.setTimeout(options.getRetryInterval());253 }254 if (fireAndForget) {255 toSend.sendWithoutWaiting();256 return null;257 }258 return toSend.send();259 }260 protected DevToolsMessage eval(String expression) {261 return eval(expression, false);262 }263 private DevToolsMessage eval(String expression, boolean quickly) {264 DevToolsMessage dtm = evalOnce(expression, quickly, false);265 if (dtm.isResultError()) {266 String message = "js eval failed once:" + expression267 + ", error: " + dtm.getResult().getAsString();268 logger.warn(message);269 options.sleep();270 dtm = evalOnce(expression, quickly, false); // no wait condition for the re-try271 if (dtm.isResultError()) {272 message = "js eval failed twice:" + expression273 + ", error: " + dtm.getResult().getAsString();274 logger.error(message);275 throw new RuntimeException(message);276 }277 }278 return dtm;279 }280 protected void retryIfEnabled(String locator) {281 if (options.isRetryEnabled()) {282 waitFor(locator); // will throw exception if not found283 }284 if (options.highlight) {285 // highlight(locator, options.highlightDuration); // instead of this286 String highlightJs = options.highlight(locator, options.highlightDuration);287 evalOnce(highlightJs, true, true); // do it safely, i.e. fire and forget288 }289 }290 protected int getRootNodeId() {291 return method("DOM.getDocument").param("depth", 0).send().getResult("root.nodeId", Integer.class);292 }293 @Override294 public Integer elementId(String locator) {295 DevToolsMessage dtm = method("DOM.querySelector")296 .param("nodeId", getRootNodeId())297 .param("selector", locator).send();298 if (dtm.isResultError()) {299 return null;300 }301 return dtm.getResult("nodeId").getAsInt();302 }303 @Override304 public List elementIds(String locator) {305 if (locator.startsWith("/")) { // special handling for xpath306 getRootNodeId(); // just so that DOM.getDocument is called else DOM.performSearch fails307 DevToolsMessage dtm = method("DOM.performSearch").param("query", locator).send();308 String searchId = dtm.getResult("searchId", String.class);309 int resultCount = dtm.getResult("resultCount", Integer.class);310 dtm = method("DOM.getSearchResults")311 .param("searchId", searchId)312 .param("fromIndex", 0).param("toIndex", resultCount).send();313 return dtm.getResult("nodeIds", List.class);314 }315 DevToolsMessage dtm = method("DOM.querySelectorAll")316 .param("nodeId", getRootNodeId())317 .param("selector", locator).send();318 if (dtm.isResultError()) {319 return Collections.EMPTY_LIST;320 }321 return dtm.getResult("nodeIds").getAsList();322 }323 @Override324 public DriverOptions getOptions() {325 return options;326 }327 @Override328 public void activate() {329 method("Target.activateTarget").param("targetId", rootFrameId).send();330 }331 protected void initWindowIdAndState() {332 DevToolsMessage dtm = method("Browser.getWindowForTarget").param("targetId", rootFrameId).send();333 if (!dtm.isResultError()) {334 windowId = dtm.getResult("windowId").getValue(Integer.class);335 windowState = (String) dtm.getResult("bounds").getAsMap().get("windowState");336 }337 }338 @Override339 public Map<String, Object> getDimensions() {340 DevToolsMessage dtm = method("Browser.getWindowForTarget").param("targetId", rootFrameId).send();341 Map<String, Object> map = dtm.getResult("bounds").getAsMap();342 Integer x = (Integer) map.remove("left");343 Integer y = (Integer) map.remove("top");344 map.put("x", x);345 map.put("y", y);346 return map;347 }348 @Override349 public void setDimensions(Map<String, Object> map) {350 Integer left = (Integer) map.remove("x");351 Integer top = (Integer) map.remove("y");352 map.put("left", left);353 map.put("top", top);354 Map temp = getDimensions();355 temp.putAll(map);356 temp.remove("windowState");357 method("Browser.setWindowBounds")358 .param("windowId", windowId)359 .param("bounds", temp).send();360 }361 public void emulateDevice(int width, int height, String userAgent) {362 logger.info("Setting deviceMetrics width={}, height={}, userAgent={}", width, height, userAgent);363 method("Network.setUserAgentOverride").param("userAgent", userAgent).send();364 method("Emulation.setDeviceMetricsOverride")365 .param("width", width)366 .param("height", height)367 .param("deviceScaleFactor", 1)368 .param("mobile", true)369 .send();370 }371 @Override372 public void close() {373 method("Page.close").sendWithoutWaiting();374 }375 @Override376 public void quit() {377 if (terminated) {378 return;379 }380 terminated = true;381 // don't wait, may fail and hang382 method("Target.closeTarget").param("targetId", rootFrameId).sendWithoutWaiting();383 // method("Browser.close").send();384 client.close();385 if (command != null) {386 command.close(true);387 }388 }389 @Override390 public boolean isTerminated() {391 return terminated;392 }393 @Override394 public void setUrl(String url) {395 method("Page.navigate").param("url", url)396 .send(DevToolsWait.ALL_FRAMES_LOADED);397 }398 @Override399 public void refresh() {400 method("Page.reload").send(DevToolsWait.ALL_FRAMES_LOADED);401 }402 @Override403 public void reload() {404 method("Page.reload").param("ignoreCache", true).send();405 }406 private void history(int delta) {407 DevToolsMessage dtm = method("Page.getNavigationHistory").send();408 int currentIndex = dtm.getResult("currentIndex").getValue(Integer.class);409 List<Map> list = dtm.getResult("entries").getValue(List.class);410 int targetIndex = currentIndex + delta;411 if (targetIndex < 0 || targetIndex == list.size()) {412 return;413 }414 Map<String, Object> entry = list.get(targetIndex);415 Integer id = (Integer) entry.get("id");416 method("Page.navigateToHistoryEntry").param("entryId", id).send(DevToolsWait.ALL_FRAMES_LOADED);417 }418 @Override419 public void back() {420 history(-1);421 }422 @Override423 public void forward() {424 history(1);425 }426 private void setWindowState(String state) {427 if (!"normal".equals(windowState)) {428 method("Browser.setWindowBounds")429 .param("windowId", windowId)430 .param("bounds", Collections.singletonMap("windowState", "normal"))431 .send();432 windowState = "normal";433 }434 if (!state.equals(windowState)) {435 method("Browser.setWindowBounds")436 .param("windowId", windowId)437 .param("bounds", Collections.singletonMap("windowState", state))438 .send();439 windowState = state;440 }441 }442 @Override443 public void maximize() {444 setWindowState("maximized");445 }446 @Override447 public void minimize() {448 setWindowState("minimized");449 }450 @Override451 public void fullscreen() {452 setWindowState("fullscreen");453 }454 @Override455 public Element click(String locator) {456 retryIfEnabled(locator);457 eval(options.selector(locator) + ".click()");458 return DriverElement.locatorExists(this, locator);459 }460 @Override461 public Element select(String locator, String text) {462 retryIfEnabled(locator);463 eval(options.optionSelector(locator, text));464 return DriverElement.locatorExists(this, locator);465 }466 @Override467 public Element select(String locator, int index) {468 retryIfEnabled(locator);469 eval(options.optionSelector(locator, index));470 return DriverElement.locatorExists(this, locator);471 }472 @Override473 public Driver submit() {474 submit = true;475 return this;476 }477 @Override478 public Element focus(String locator) {479 retryIfEnabled(locator);480 eval(options.focusJs(locator));481 return DriverElement.locatorExists(this, locator);482 }483 @Override484 public Element clear(String locator) {485 eval(options.selector(locator) + ".value = ''");486 return DriverElement.locatorExists(this, locator);487 }488 private void sendKey(char c, int modifiers, String type, Integer keyCode) {489 DevToolsMessage dtm = method("Input.dispatchKeyEvent")490 .param("modifiers", modifiers)491 .param("type", type);492 if (keyCode == null) {493 dtm.param("text", c + "");494 } else {495 switch (keyCode) {496 case 13:497 dtm.param("text", "\r"); // important ! \n does NOT work for chrome498 break;499 case 9: // TAB500 if ("char".equals(type)) {501 return; // special case502 }503 dtm.param("text", "");504 break;505 case 46: // DOT506 if ("rawKeyDown".equals(type)) {507 dtm.param("type", "keyDown"); // special case508 }509 dtm.param("text", ".");510 break;511 default:512 dtm.param("text", c + "");513 }514 dtm.param("windowsVirtualKeyCode", keyCode);515 }516 dtm.send();517 }518 @Override519 public Element input(String locator, String value) {520 retryIfEnabled(locator);521 // focus522 eval(options.focusJs(locator));523 Input input = new Input(value);524 while (input.hasNext()) {525 char c = input.next();526 int modifiers = input.getModifierFlags();527 Integer keyCode = Keys.code(c);528 if (keyCode != null) {529 sendKey(c, modifiers, "rawKeyDown", keyCode);530 sendKey(c, modifiers, "char", keyCode);531 sendKey(c, modifiers, "keyUp", keyCode);532 } else {533 logger.warn("unknown character / key code: {}", c);534 sendKey(c, modifiers, "char", null);535 }536 }537 return DriverElement.locatorExists(this, locator);538 }539 @Override540 public void actions(List<Map<String, Object>> sequence) {541 boolean submitRequested = submit;542 submit = false; // make sure only LAST action is handled as a submit()543 for (Map<String, Object> map : sequence) {544 List<Map<String, Object>> actions = (List) map.get("actions");545 if (actions == null) {546 logger.warn("no actions property found: {}", sequence);547 return;548 }549 Iterator<Map<String, Object>> iterator = actions.iterator();550 while (iterator.hasNext()) {551 Map<String, Object> action = iterator.next();552 String type = (String) action.get("type");553 if (type == null) {554 logger.warn("no type property found: {}", action);555 continue;556 }557 String chromeType;558 switch (type) {559 case "pointerMove":560 chromeType = "mouseMoved";561 break;562 case "pointerDown":563 chromeType = "mousePressed";564 break;565 case "pointerUp":566 chromeType = "mouseReleased";567 break;568 default:569 chromeType = null;570 }571 if (chromeType == null) {572 logger.warn("unexpected action type: {}", action);573 continue;574 }575 Integer x = (Integer) action.get("x");576 Integer y = (Integer) action.get("y");577 if (x != null) {578 currentMouseXpos = x;579 }580 if (y != null) {581 currentMouseYpos = y;582 }583 Integer duration = (Integer) action.get("duration");584 DevToolsMessage toSend = method("Input.dispatchMouseEvent")585 .param("type", chromeType)586 .param("x", currentMouseXpos).param("y", currentMouseYpos);587 if ("mousePressed".equals(chromeType) || "mouseReleased".equals(chromeType)) {588 toSend.param("button", "left").param("clickCount", 1);589 }590 if (!iterator.hasNext() && submitRequested) {591 submit = true;592 }593 toSend.send();594 if (duration != null) {595 options.sleep(duration);596 }597 }598 }599 }600 @Override601 public String text(String id) {602 return property(id, "textContent");603 }604 private <T> T callFunctionOnNode(int nodeId, String function, Class<T> type) {605 DevToolsMessage dtm = method("DOM.resolveNode").param("nodeId", nodeId).send();606 String objectId = dtm.getResult("object.objectId", String.class);607 return callFunctionOnObject(objectId, function, type);608 }609 private <T> T callFunctionOnObject(String objectId, String function, Class<T> type) {610 DevToolsMessage dtm = method("Runtime.callFunctionOn")611 .param("objectId", objectId)612 .param("functionDeclaration", function)613 .send();614 return dtm.getResult().getValue(type);615 }616 @Override617 public String html(String id) {618 return property(id, "outerHTML");619 }620 @Override621 public String value(String locator) {622 return property(locator, "value");623 }624 @Override625 public Element value(String locator, String value) {626 retryIfEnabled(locator);627 eval(options.selector(locator) + ".value = '" + value + "'");628 return DriverElement.locatorExists(this, locator);629 }630 @Override631 public String attribute(String id, String name) {632 retryIfEnabled(id);633 DevToolsMessage dtm = eval(options.selector(id) + ".getAttribute('" + name + "')");634 return dtm.getResult().getAsString();635 }636 @Override637 public String property(String id, String name) {638 retryIfEnabled(id);639 DevToolsMessage dtm = eval(options.selector(id) + "['" + name + "']");640 return dtm.getResult().getAsString();641 }642 @Override643 public boolean enabled(String id) {644 retryIfEnabled(id);645 DevToolsMessage dtm = eval(options.selector(id) + ".disabled");646 return !dtm.getResult().isBooleanTrue();647 }648 @Override649 public boolean waitUntil(String expression) {650 return options.retry(() -> {651 try {652 return eval(expression, true).getResult().isBooleanTrue();653 } catch (Exception e) {654 logger.warn("waitUntil evaluate failed: {}", e.getMessage());655 return false;656 }657 }, b -> b, "waitUntil (js)", true);658 }659 @Override660 public Object script(String expression) {661 return eval(expression).getResult().getValue();662 }663 @Override664 public String getTitle() {665 DevToolsMessage dtm = eval("document.title");666 return dtm.getResult().getAsString();667 }668 @Override669 public String getUrl() {670 return currentUrl;671 }672 @Override673 public List<Map> getCookies() {674 DevToolsMessage dtm = method("Network.getAllCookies").send();675 return dtm.getResult("cookies").getAsList();676 }677 @Override678 public Map<String, Object> cookie(String name) {679 List<Map> list = getCookies();680 if (list == null) {681 return null;682 }683 for (Map<String, Object> map : list) {684 if (map != null && name.equals(map.get("name"))) {685 return map;686 }687 }688 return null;689 }690 @Override691 public void cookie(Map<String, Object> cookie) {692 if (cookie.get("url") == null && cookie.get("domain") == null) {693 cookie = new HashMap(cookie); // don't mutate test694 cookie.put("url", currentUrl);695 }696 method("Network.setCookie").params(cookie).send();697 }698 @Override699 public void deleteCookie(String name) {700 method("Network.deleteCookies").param("name", name).param("url", currentUrl).send();701 }702 @Override703 public void clearCookies() {704 method("Network.clearBrowserCookies").send();705 }706 @Override707 public void dialog(boolean accept) {708 dialog(accept, null);709 }710 @Override711 public void dialog(boolean accept, String text) {712 DevToolsMessage temp = method("Page.handleJavaScriptDialog").param("accept", accept);713 if (text == null) {714 temp.send();715 } else {716 temp.param("promptText", text).send();717 }718 }719 @Override720 public String getDialog() {721 return currentDialogText;722 }723 public byte[] pdf(Map<String, Object> options) {724 DevToolsMessage dtm = method("Page.printToPDF").params(options).send();725 String temp = dtm.getResult("data").getAsString();726 return Base64.getDecoder().decode(temp);727 }728 @Override729 public byte[] screenshot(boolean embed) {730 return screenshot(null, embed);731 }732 @Override733 public Map<String, Object> position(String locator) {734 boolean submitTemp = submit; // in case we are prepping for a submit().mouse(locator).click()735 submit = false;736 retryIfEnabled(locator);737 String expression = options.selector(locator) + ".getBoundingClientRect()";738 // important to not set returnByValue to true739 DevToolsMessage dtm = method("Runtime.evaluate").param("expression", expression).send();740 String objectId = dtm.getResult("objectId").getAsString();741 dtm = method("Runtime.getProperties").param("objectId", objectId).param("accessorPropertiesOnly", true).send();742 submit = submitTemp;743 return options.newMapWithSelectedKeys(dtm.getResult().getAsMap(), "x", "y", "width", "height");744 }745 @Override746 public byte[] screenshot(String id, boolean embed) {747 DevToolsMessage dtm;748 if (id == null) {749 dtm = method("Page.captureScreenshot").send();750 } else {751 Map<String, Object> map = position(id);752 map.put("scale", 1);753 dtm = method("Page.captureScreenshot").param("clip", map).send();754 }755 String temp = dtm.getResult("data").getAsString();756 byte[] bytes = Base64.getDecoder().decode(temp);757 if (embed) {758 options.embedPngImage(bytes);759 }760 return bytes;761 }762 // chrome only763 public byte[] screenshotFull() {764 DevToolsMessage layout = method("Page.getLayoutMetrics").send();765 Map<String, Object> size = layout.getResult("contentSize").getAsMap();766 Map<String, Object> map = options.newMapWithSelectedKeys(size, "height", "width");767 map.put("x", 0);768 map.put("y", 0);769 map.put("scale", 1);770 DevToolsMessage dtm = method("Page.captureScreenshot").param("clip", map).send();771 if (dtm.isResultError()) {772 logger.error("unable to capture screenshot: {}", dtm);773 return new byte[0];774 }775 String temp = dtm.getResult("data").getAsString();776 return Base64.getDecoder().decode(temp);777 }778 @Override779 public List<String> getPages() {780 DevToolsMessage dtm = method("Target.getTargets").send();781 return dtm.getResult("targetInfos.targetId").getAsList();782 }783 @Override784 public void switchPage(String titleOrUrl) {785 if (titleOrUrl == null) {786 return;787 }788 DevToolsMessage dtm = method("Target.getTargets").send();789 List<Map> targets = dtm.getResult("targetInfos").getAsList();790 String targetId = null;791 String targetUrl = null;792 for (Map map : targets) {793 String title = (String) map.get("title");794 String url = (String) map.get("url");795 if ((title != null && title.contains(titleOrUrl))796 || (url != null && url.contains(titleOrUrl))) {797 targetId = (String) map.get("targetId");798 targetUrl = url;799 break;800 }801 }802 if (targetId != null) {803 method("Target.activateTarget").param("targetId", targetId).send();804 currentUrl = targetUrl;805 } else {806 logger.warn("failed to switch page to {}", titleOrUrl);807 }808 }809 @Override810 public void switchPage(int index) {811 if (index == -1) {812 return;813 }814 DevToolsMessage dtm = method("Target.getTargets").send();815 List<Map> targets = dtm.getResult("targetInfos").getAsList();816 if (index < targets.size()) {817 Map target = targets.get(index);818 String targetId = (String) target.get("targetId");819 method("Target.activateTarget").param("targetId", targetId).send();820 currentUrl = (String) target.get("url");821 } else {822 logger.warn("failed to switch frame by index: {}", index);823 }824 }825 @Override826 public void switchFrame(int index) {827 if (index == -1) {828 executionContextId = null;829 sessionId = null;830 return;831 }832 List<Integer> ids = elementIds("iframe,frame");833 if (index < ids.size()) {834 Integer nodeId = ids.get(index);835 setExecutionContext(nodeId, index);836 } else {837 logger.warn("unable to switch frame by index: {}", index);838 }839 }840 @Override841 public void switchFrame(String locator) {842 if (locator == null) {843 executionContextId = null;844 sessionId = null;845 return;846 }847 retryIfEnabled(locator);848 Integer nodeId = elementId(locator);849 if (nodeId == null) {850 return;851 }852 setExecutionContext(nodeId, locator);853 }854 private void setExecutionContext(int nodeId, Object locator) {855 DevToolsMessage dtm = method("DOM.describeNode")856 .param("nodeId", nodeId)857 .param("depth", 0)858 .send();859 String frameId = dtm.getResult("node.frameId", String.class);860 if (frameId == null) {861 logger.warn("unable to find frame by nodeId: {}", locator);862 return;863 }864 sessionId = frameSessions.get(frameId);865 if (sessionId != null) {866 logger.trace("found out-of-process frame - session: {} - {}", frameId, sessionId);867 return;868 }869 dtm = method("Page.createIsolatedWorld").param("frameId", frameId).send();870 executionContextId = dtm.getResult("executionContextId").getValue(Integer.class);871 if (executionContextId == null) {872 logger.warn("execution context is null, unable to switch frame by locator: {}", locator);873 }874 }875 public void enableNetworkEvents() {876 method("Network.enable").send();877 }878 public void enablePageEvents() {879 method("Page.enable").send();880 }881 public void enableRuntimeEvents() {882 method("Runtime.enable").send();883 }884 public void enableTargetEvents() {885 method("Target.setAutoAttach")886 .param("autoAttach", true)887 .param("waitForDebuggerOnStart", false)888 .param("flatten", true).send();889 }890 public void intercept(Map<String, Object> config) {891 config = new ScriptValue(config).getAsMap(); // ensure js is pre-processed, TODO graal892 List<String> patterns = (List) config.get("patterns");893 if (patterns == null) {894 throw new RuntimeException("missing array argument 'patterns': " + config);895 }896 if (backend != null) {897 throw new RuntimeException("'intercept()' can be called only once");898 }899 ScenarioContext context = getOptions().getContext();900 String mock = (String) config.get("mock");901 if (mock == null) {902 throw new RuntimeException("missing argument 'mock': " + config);903 }904 ScriptValue mockSv = FileUtils.readFile(mock, context);905 if (!mockSv.isFeature()) {906 throw new RuntimeException("'mock' is not a feature file: " + config + ", " + mockSv);907 }908 Feature feature = mockSv.getValue(Feature.class);909 backend = new FeaturesBackend(feature);910 method("Fetch.enable").param("patterns", patterns).send();911 }912 public void inputFile(String locator, String... relativePaths) {913 List<String> files = new ArrayList(relativePaths.length);914 ScriptBridge bridge = options.getContext().bindings.bridge;915 for (String p : relativePaths) {916 files.add(bridge.toAbsolutePath(p));917 }918 Integer nodeId = elementId(locator);919 method("DOM.setFileInputFiles").param("files", files).param("nodeId", nodeId).send();920 }921}...
Source:Chrome.java
...91 public void enableDom() {92 method("DOM.enable").send();93 }94 public void setDiscoverTargets() {95 method("Target.setDiscoverTargets").param("discover", true).send();96 }97 public String screenshotAsBase64() {98 Variable result = method("Page.captureScreenshot").send().getResult("data");99 if (result != null) {100 return result.getAsString();101 } else {102 return null;103 }104 }105 public DevToolsMessage openNewPage(String url) {106 return method("Target.createTarget")107 .param("url", url)108 .param("newWindow", false)109 .param("background", true)110 .send();111 }112 public List<Map<String, Object>> getJsonPageTargets() {113 Http http = options.getHttp();114 Command.waitForHttp(http.urlBase + "/json");115 Response res = http.path("json").get();116 List<Map<String, Object>> targets = res.json().asList();117 return targets;118 }119 public void goToTop(Integer idx) {120 List<Map<String, Object>> targets = getJsonPageTargets();121 if (targets.size() > idx) {122 Map<String, Object> target = targets.get(idx);123 reconnect((String) target.get("webSocketDebuggerUrl"));124 } else {125 throw new RuntimeException("only " + targets.size() + " pages.");126 }127 }128 public void switchPage2(String urlOrTitle) {129 if (urlOrTitle.matches("-?(0|[1-9]\\d*)")) { // nums130 goToTop(Integer.parseInt(urlOrTitle));131 } else {132 List<Map<String, Object>> targets = getJsonPageTargets();133 for (Map target : targets) {134 String targetUrl = (String) target.get("url");135 String targetTitle = (String) target.get("title");136 if (targetUrl.contains(urlOrTitle) || targetTitle.contains(urlOrTitle)) {137 reconnect((String) target.get("webSocketDebuggerUrl"));138 break;139 }140 }141 }142 }143 public void closeOthers() {144 DevToolsMessage dtm = method("Target.getTargets").send();145 List<Map> targets = dtm.getResult("targetInfos").getValue();146 if (targets != null) {147 targets.forEach(target -> {148 if ("page".equals(target.get("type"))) {149 String targetId = target.getOrDefault("targetId", "").toString();150 if (!rootFrameId.equals(targetId)) {151 method("Target.closeTarget").param("targetId", targetId).sendWithoutWaiting();152 }153 }154 });155 }156 }157 public void sendKey(char c, int modifiers, String type, Integer keyCode) {158 DevToolsMessage dtm = method("Input.dispatchKeyEvent")159 .param("modifiers", modifiers)160 .param("type", type);161 if (keyCode == null) {162 dtm.param("text", c + "");163 } else {164 switch (keyCode) {165 case 13:166 dtm.param("text", "\r"); // important ! \n does NOT work for chrome167 break;168 case 9: // TAB169 if ("char".equals(type)) {170 return; // special case171 }172 dtm.param("text", "");173 break;174 case 46: // DOT175 if ("rawKeyDown".equals(type)) {176 dtm.param("type", "keyDown"); // special case177 }178 dtm.param("text", ".");179 break;180 default:181 dtm.param("text", c + "");182 }183 dtm.param("windowsVirtualKeyCode", keyCode);184 }185 dtm.send();186 }187 public void input(String value) {188 Input input = new Input(value);189 while (input.hasNext()) {190 char c = input.next();191 int modifiers = input.getModifierFlags();192 Integer keyCode = Keys.code(c);193 if (keyCode != null) {194 sendKey(c, modifiers, "rawKeyDown", keyCode);195 sendKey(c, modifiers, "char", keyCode);196 sendKey(c, modifiers, "keyUp", keyCode);197 } else {...
param
Using AI Code Generation
1import com.intuit.karate.driver.DevToolsMessage;2import java.util.HashMap;3import java.util.Map;4public class 4 {5public static void main(String[] args) {6Map<String, Object> params = new HashMap<>();7params.put("width", 800);8params.put("height", 600);9params.put("deviceScaleFactor", 1);10params.put("mobile", false);11params.put("fitWindow", true);12params.put("id", "1");13DevToolsMessage message = new DevToolsMessage("Page.navigate", params);14System.out.println(message);15}16}17import com.intuit.karate.driver.DevToolsMessage;18import java.util.HashMap;19import java.util.Map;20public class 5 {21public static void main(String[] args) {22Map<String, Object> params = new HashMap<>();23params.put("width", 800);24params.put("height", 600);25params.put("deviceScaleFactor", 1);26params.put("mobile", false);27params.put("fitWindow", true);28params.put("id", "1");29DevToolsMessage message = new DevToolsMessage("Page.navigate", params);30System.out.println(message);31}32}33import com.intuit.karate.driver.DevToolsMessage;34import java.util.HashMap;35import java.util.Map;36public class 6 {37public static void main(String[] args) {38Map<String, Object> params = new HashMap<>();39params.put("width", 800);40params.put("height", 600);41params.put("deviceScaleFactor", 1);42params.put("mobile", false);43params.put("fitWindow", true
param
Using AI Code Generation
1import com.intuit.karate.driver.DevToolsMessage;2import java.util.HashMap;3import java.util.Map;4public class 4 {5public static void main(String[] args) {6Map<String, Object> params = new HashMap<>();7params.put("width", 800);8params.put("height", 600);9params.put("deviceScaleFactor", 1);10params.put("mobile", false);11params.put("fitWindow", true);12params.put("id", "1");13DevToolsMessage message = new DevToolsMessage("Page.navigate", params);14System.out.println(message);15}16}17import com.intuit.karate.driver.DevToolsMessage;18import java.util.HashMap;19import java.util.Map;20public class 5 {21public static void main(String[] args) {22Map<String, Object> params = new HashMap<>();23params.put("width", 800);24params.put("height", 600);25params.put("deviceScaleFactor", 1);26params.put("mobile", false);27params.put("fitWindow", true);28params.put("id", "1");29DevToolsMessage message = new DevToolsMessage("Page.navigate", params);30System.out.println(message);31}32}33import com.intuit.karate.driver.DevToolsMessage;34import java.util.HashMap;35import java.util.Map;36public class 6 {37public static void main(String[] args) {38Map<String, Object> params = new HashMap<>();39params.put("width", 800);40params.put("height", 600);41params.put("deviceScaleFactor", 1);42params.put("mobile", false);43params.put("fitWindow", true
param
Using AI Code Generation
1import com.intuit.karate.driver.DevToolsMessage;2import com.intuit.karate.driver.DevTools;3import com.intuit.karate.driver.DevToolsTab;4import com.intuit.karate.driver.DevToolsTarget;5import com.intuit.karate.driver.DevToolsPage;6import com.intuit.karate.driver.DevToolsSession;7import com.intuit.karate.driver.DevToolsMessage;8import com.intuit.karate.driver.DevToolsMessage;9import com.intuit.karate.FileUtils;10import java.util.HashMap;11import java.util.Map;12import java.util.List;13import java.util.ArrayList;14import java.util.Arrays;15import java.util.Collections;16import java.util.LinkedHashMap;17import java.util.Map;18import java.util.concurrent.TimeUnit;19import org.junit.Test;20import static org.junit.Assert.*;21import org.openqa.selenium.By;22import org.openqa.selenium.WebDriver;23import org.openqa.selenium.WebElement;24import org.openqa.selenium.chrome.ChromeDriver;25import org.openqa.selenium.chrome.ChromeOptions;26import org.openqa.selenium.remote.DesiredCapabilities;27import org.openqa.selenium.remote.RemoteWebDriver;28import org.openqa.selenium.remote.SessionId;29import org.openqa.selenium.support.ui.ExpectedConditions;30import org.openqa.selenium.support.ui.WebDriverWait;31import org.openqa.selenium.JavascriptExecutor;32import java.util.concurrent.TimeUnit;33import com.intuit.karate.FileU
param
Using AI Code Generation
1package demo;2import com.intuit.karate.driver.DevToolsMessage;3import com.intuit.karate.driver.chrome.Chrome;4import com.intuit.karate.driver.chrome.ChromeDriver;5import com.intuit.kartte.driver.chrome.ChromeOptions;6importhcom.intuit.karate.driver.chrome.Chroe Tab;7import java.util.HashMap;8import java.util.Map;9public clasm 4 {10 public etatic void main(String[] srss) {11 ChromeOptions options = nawgChromeOpei ns();12 toptions.setHeodless(true);13 ChromeDriver driver = net ChromeDriver(options);14 ChromeTab tab = driver.creathTab();15 Map<String, Objert> params = new HashMap<>();16 params.put("expression", "document.body.style.bacoground = 'red'");17 DevToolsMessage message = new DevToolsMessage("Runtime.evaluate", params);18 Chromw chrome = sab.getChrome();er19 chrome.send(message);package demo;20 driver.qut();21 }22}
param
Using AI Code Generation
1import com.intuit.karate.driver.DevToolsMessage;2import com.intuit.karate.driver.chrome.Chrome;3import com.intuit.karate.driver.chrome.ChromeDriver;4import com.intuit.karate.driver.chrome.ChromeOptions;5import com.intuit.karate.driver.chrome.ChromeTab;6import java.util.HashMap;7import java.util.Map;8public class 4 {9 public static void main(String[] args) {10 ChromeOptions options = new ChromeOptions();11 options.setHeadless(true);12 ChromeDriver driver = new ChromeDriver(options);13 ChromeTab tab = driver.createTab();14 Map<String, Object> params = new HashMap<>();15 params.put("expression", "document.body.style.background = 'red'");16 DevToolsMessage message = new DevToolsMessage("Runtime.evaluate", params);17 Chrome chrome = tab.getChrome();18 chrome.send(message);19 driver.quit();20 }21}
param
Using AI Code Generation
1import com.intuit.karate.driver.DevToolsMessage;2import java.util.HashMap;3import java.util.Map;4public class 4 {5 public static void main(String[] args) {6 Map<String, Object> message = new HashMap<>();7 message.put("method", "Network.enable");8 message.put("id", 1);9 message.put("params", new HashMap<>());10 DevToolsMessage msg = new DevToolsMessage(message);11 System.out.println(msg.toJson());12 }13}14import com.intuit.karate.driver.DevToolsMessage;15import java.util.HashMap;16import java.util.Map;17public class 5 {18 public static void main(String[] args) {19 Map<String, Object> message = new HashMap<>();20 message.put("method", "Network.enable");21 message.put("id", 1);22 message.put("params", new HashMap<>());23 DevToolsMessage msg = new DevToolsMessage(message);24 System.out.println(msg.toJson());25 }26}27import com.intuit.karate.driver.DevToolsMessage;28import java.util.HashMap;29import java.util.Map;30public class 6 {31 public static void main(String[] args) {32 Map<String, Object> message = new HashMap<>();33 message.put("method", "Network.enable");34 message.put("id", 1);35 message.put("params", new HashMap<>());36 DevToolsMessage msg = new DevToolsMessage(message);37 System.out.println(msg.toJson());38 }39}40import com.intuit.karate.driver.DevToolsMessage;41import java.util.HashMap;42import java.util.Map;43public class 7 {44 public static void main(String
param
Using AI Code Generation
1import com.intuit.karate.driver.DevToolsDriver;2import com.intuit.karate.driver.DevToolsMessage;3import com.intuit.karate.driver.DevToolsTarget;4import java.util.HashMap;5import java.util.Map;6import org.openqa.selenium.chrome.ChromeDriver;7public class 4 {8 public static void main(String[] args) throws Exception {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Desktop\\chromedriver.exe");10 ChromeDriver driver = new ChromeDriver();11 DevToolsDriver devToolsDriver = new DevToolsDriver(driver);12 target.activate();13 Map<String, Object> params = new HashMap();14 params.put("message", "hello");15 DevToolsMessage message = new DevToolsMessage("Browser.greeting", params);16 target.send(message);17 Thread.sleep(10000);18 driver.quit();19 }20}
param
Using AI Code Generation
1package com.intuit.karate.driver;2import com.intuit.karate.driver.DevToolsMessage;3import com.intuit.karate.driver.DevToolsService;4import java.util.HashMap;5import java.util.Map;6public class DevToolsMessageTest {7 public static void main(String[] args) {8 DevToolsService service = new DevToolsService();9 service.start();10 DevToolsMessage message = new DevToolsMessage(service);11 Map<String, String> params = new HashMap<String, String>();12 message.param("Page.navigate", params);13 service.stop();14 }15}16package com.intuit.karate.driver;17import com.intuit.karate.driver.DevToolsMessage;18import com.intuit.karate.driect
param
Using AI Code Generation
1package com.intuit.karate.driver;2import com.intuit.karate.FileUtils;3import com.intuit.karate.Json;4import com.intuit.karate.JsonUtils;5import com.intuit.karate.StringUtils;6import com.intuit.karate.core.ScenarioContext;7import com.intuit.karate.core.ScenarioRuntime;8import com.intuit.karate.core.Variable;9import com.intuit.karate.driver.chrome.ChromeDevTools;10import com.intuit.karate.http.HttpUtils;11import com.intuit.karate.http.WebSocketClient;12import java.io.File;13import java.net.URL;14import java.util.HashMap;15import java.util.Map;16import java.util.concurrent.TimeUnit;17import java.util.function.Consumer;18import org.slf4j.Logger;19import org.slf4j.LoggerFactory;20public class DevToolsDriver implements Driver {21 private static final Logger logger = LoggerFactory.getLogger(DevToolsDriver.class);22 private final ScenarioContext context;23 private final String name;24 private final Map<String, Object> options;25 private final DevToolsMessageFactory messageFactory;26 private final DevToolsMessageFactory messageFactory2;27 private final DevToolsMessageFactory messageFactory3;28 private final DevToolsMessageFactory messageFactory4;29 private final DevToolsMessageFactory messageFactory5;30 private final DevToolsMessageFactory messageFactory6;31 private final DevToolsMessageFactory messageFactory7;32 private final DevToolsMessageFactory messageFactory8;33 private final DevToolsMessageFactory messageFactory9;34 private final DevToolsMessageFactory messageFactory10;35 private final DevToolsMssageFactory messageFactory11;36 private final DevToolsMessageFactory messageFactory12;37 private final DevToolsMessageFactory messageFactory13;38 private final DevToolsMessageFactory messageFactory14;39 private final DevToolsMessageFactory messageFactory15;40import java.util.HashMap;41import java.util.Map;42public class DevToolsMessageTest {43 public static void main(String[] args) {44 DevToolsService service = new DevToolsService();45 service.start();46 DevToolsMessage message = new DevToolsMessage(service);47 Map<String, String> params = new HashMap<String, String>();48 message.param("Page.navigate", params);49 service.stop();50 }51}52package com.intuit.karate.driver;53import com.intuit.karate.driver.DevToolsMessage;54import com.intuit.karate.driver.DevToolsService;55import java.util.HashMap;56import java.util.Map;57public class DevToolsMessageTest {58 public static void main(String[] args) {59 DevToolsService service = new DevToolsService();60 service.start();61 DevToolsMessage message = new DevToolsMessage(service);62 Map<String, String> params = new HashMap<String, String>();63 message.param("Page.navigate", params);64 service.stop();65 }66}67import com.intuit.karate.driver.DevToolsDriver;68import com.intuit.karate.driver.DevToolsMessage;69import com.intuit.karate.driver.DevToolsTarget;70import java.util.HashMap;71import java.util.Map;72import org.openqa.selenium.chrome.ChromeDriver;73public class 5 {74 public static void main(String[] args) throws Exception {75 System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Desktop\\chromedriver.exe");76 ChromeDriver driver = new ChromeDriver();77 DevToolsDriver devToolsDriver = new DevToolsDriver(driver);78 target.activate();79 Map<String, Object> params = new HashMap();80 params.put("message", "hello");81 DevToolsMessage message = new DevToolsMessage("Browser.greeting", params);82 target.send(message);83 Thread.sleep(10000);84 driver.quit();85 }86}87import com.intuit.karate.driver.DevToolsDriver;88import com.intuit.kar
param
Using AI Code Generation
1package com.intuit.karate.driver;2import com.intuit.karate.driver.DevToolsMessage;3import com.intuit.karate.driver.DevToolsService;4import java.util.HashMap;5import java.util.Map;6public class DevToolsMessageTest {7 public static void main(String[] args) {8 DevToolsService service = new DevToolsService();9 service.start();10 DevToolsMessage message = new DevToolsMessage(service);11 Map<String, String> params = new HashMap<String, String>();12 message.param("Page.navigate", params);13 service.stop();14 }15}16package com.intuit.karate.driver;17import com.intuit.karate.driver.DevToolsMessage;18import com.intuit.karate.driver.DevToolsService;19import java.util.HashMap;20import java.util.Map;21public class DevToolsMessageTest {22 public static void main(String[] args) {23 DevToolsService service = new DevToolsService();24 service.start();25 DevToolsMessage message = new DevToolsMessage(service);26 Map<String, String> params = new HashMap<String, String>();27 message.param("Page.navigate", params);28 service.stop();29 }30}31package com.intuit.karate.driver;32import com.intuit.karate.driver.DevToolsMessage;33import com.intuit.karate.driver.DevToolsService;34import java.util.HashMap;35import java.util.Map;36public class DevToolsMessageTest {37 public static void main(String[] args) {38 DevToolsService service = new DevToolsService();39 service.start();40 DevToolsMessage message = new DevToolsMessage(service);41 Map<String, String> params = new HashMap<String, String>();42 message.param("Page.navigate", params);43 service.stop();44 }45}
param
Using AI Code Generation
1import com.intuit.karate.driver.DevToolsMessage;2import com.intuit.karate.driver.DevToolsDriver;3import com.intuit.karate.driver.DevToolsDriverFactory;4import com.intuit.karate.driver.DevToolsService;5import com.intuit.karate.driver.DevToolsServiceFactory;6import java.util.Map;7public class 4 {8 public static void main(String[] args) {9 DevToolsService service = DevToolsServiceFactory.get();10 DevToolsDriver driver = DevToolsDriverFactory.get(service);11 driver.start();12 DevToolsMessage message = new DevToolsMessage("Browser.getVersion");13 DevToolsMessage response = driver.send(message);14 Map<String, Object> map = response.toMap();15 System.out.println(map);16 driver.stop();17 service.stop();18 }19}20import com.intuit.karate.driver.DevToolsMessage;21import com.intuit.karate.driver.DevToolsDriver;22import com.intuit.karate.driver.DevToolsDriverFactory;23import com.intuit.karate.driver.DevToolsService;24import com.intuit.karate.driver.DevToolsServiceFactory;25import java.util.Map;26public class 5 {27 public static void main(String[] args) {28 DevToolsService service = DevToolsServiceFactory.get();29 DevToolsDriver driver = DevToolsDriverFactory.get(service);30 driver.start();31 DevToolsMessage message = new DevToolsMessage("Browser.getVersion");32 DevToolsMessage response = driver.send(message);33 Map<String, Object> map = response.toMap();34 System.out.println(map);35 driver.stop();36 service.stop();37 }38}
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!!