How to use Chrome class of com.intuit.karate.driver.chrome package

Best Karate code snippet using com.intuit.karate.driver.chrome.Chrome

Source:Chrome.java Github

copy

Full Screen

...48import asura.ui.karate.KarateRunner;49/**50 * @author pthomas351 */52public class Chrome extends DevToolsDriver {53 private static Logger logger = LoggerFactory.getLogger(Chrome.class);54 public static final String DEFAULT_PATH_MAC = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";55 public static final String DEFAULT_PATH_WIN = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";56 public static final String DEFAULT_PATH_LINUX = "/usr/bin/google-chrome";57 public Driver parent; // used in 'DriverPoolActor'58 public ScenarioEngine engine;59 public Boolean inject;60 public Consumer<Map<String, Object>> filter;61 public Chrome(DriverOptions options, Command command, String webSocketUrl) {62 super(options, command, webSocketUrl);63 }64 // 自定义65 public Chrome(DriverOptions options, Command command, String webSocketUrl,66 ScenarioEngine engine, Boolean inject, Consumer<Map<String, Object>> filter67 ) {68 super(options, command, webSocketUrl);69 this.engine = engine;70 this.inject = inject;71 this.filter = filter;72 if (this.inject && this.engine != null) {73 this.engine.setDriver(this);74 }75 client.setTextHandler(text -> {76 Map<String, Object> map = Json.of(text).value();77 DevToolsMessage dtm = new DevToolsMessage(this, map);78 if (this.filter != null && !StringUtils.isBlank(dtm.getMethod())) {79 this.filter.accept(map);80 }81 receive(dtm);82 return false; // no async signalling, for normal use, e.g. chrome developer tools83 });84 }85 public void closeClient() {86 client.close();87 }88 public void enableLog() {89 method("Log.enable").send();90 }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 {198 sendKey(c, modifiers, "char", -1);199 }200 }201 }202 public static void loadOverride() {203 logger.info("use override chrome");204 }205 @Override206 public void quit() {207 DriverProvider provider = DriverOptions.getDriverProvider();208 if (provider != null) {209 provider.release(this);210 } else {211 super.quit();212 }213 }214 public void quit(Boolean force) {215 if (force) {216 super.quit();217 }218 }219 public static Chrome start(Map<String, Object> map, ScenarioRuntime sr) {220 DriverOptions options = new DriverOptions(map, sr, 9222,221 FileUtils.isOsWindows() ? DEFAULT_PATH_WIN : FileUtils.isOsMacOsX() ? DEFAULT_PATH_MAC : DEFAULT_PATH_LINUX);222 options.arg("--remote-debugging-port=" + options.port);223 options.arg("--no-first-run");224 options.arg("--disable-translate");225 options.arg("--disable-notifications");226 options.arg("--disable-infobars");227 options.arg("--disable-gpu");228 options.arg("--dbus-stub");229 options.arg("--disable-dev-shm-usage");230 if (options.userDataDir != null) {231 options.arg("--user-data-dir=" + options.userDataDir);232 }233 options.arg("--disable-popup-blocking");234 if (options.headless) {235 options.arg("--headless");236 }237 Command command = options.startProcess();238 String webSocketUrl = null;239 if (map.containsKey("debuggerUrl")) {240 webSocketUrl = (String) map.get("debuggerUrl");241 } else {242 Object targetId = map.get("targetId");243 Object startUrl = map.get("startUrl");244 Object top = map.get("top");245 Http http = options.getHttp();246 Command.waitForHttp(http.urlBase + "/json");247 Response res = http.path("json").get();248 if (res.json().asList().isEmpty()) {249 if (command != null) {250 command.close(true);251 }252 throw new RuntimeException("chrome server returned empty list from " + http.urlBase);253 }254 List<Map<String, Object>> targets = res.json().asList();255 for (Map<String, Object> target : targets) {256 String targetUrl = (String) target.get("url");257 if (targetUrl == null || targetUrl.startsWith("chrome-")) {258 continue;259 }260 if (top != null && top.equals(true)) {261 webSocketUrl = (String) target.get("webSocketDebuggerUrl");262 break;263 } else if (targetId != null) {264 if (targetId.equals(target.get("id"))) {265 webSocketUrl = (String) target.get("webSocketDebuggerUrl");266 break;267 }268 } else if (startUrl != null) {269 String targetTitle = (String) target.get("title");270 if (targetUrl.contains(startUrl.toString()) || targetTitle.contains(startUrl.toString())) {271 webSocketUrl = (String) target.get("webSocketDebuggerUrl");272 break;273 }274 } else {275 String targetType = (String) target.get("type");276 if (!"page".equals(targetType)) {277 continue;278 }279 webSocketUrl = (String) target.get("webSocketDebuggerUrl");280 if (options.attach == null) { // take the first281 break;282 }283 if (targetUrl.contains(options.attach)) {284 break;285 }286 }287 }288 }289 if (webSocketUrl == null) {290 throw new RuntimeException("failed to attach to chrome debug server");291 }292 Boolean inject = (Boolean) map.getOrDefault("_inject", false);293 Consumer<Map<String, Object>> filter = (Consumer<Map<String, Object>>) map.getOrDefault("_filter", null);294 Chrome chrome = new Chrome(options, command, webSocketUrl, sr.engine, inject, filter);295 chrome.activate();296 chrome.enablePageEvents();297 chrome.enableRuntimeEvents();298 chrome.enableTargetEvents();299 chrome.enableLog();300 chrome.setDiscoverTargets();301 if (!options.headless) {302 chrome.initWindowIdAndState();303 }304 return chrome;305 }306 public static Chrome start(Boolean start, Consumer<Map<String, Object>> filter, Boolean inject) {307 Map<String, Object> options = new HashMap();308 options.put("start", start);309 options.put("_inject", inject);310 options.put("_filter", filter);311 return Chrome.start(options, KarateRunner.buildScenarioEngine().runtime);312 }313 public static Chrome start(Map<String, Object> options, Consumer<Map<String, Object>> filter, Boolean inject) {314 options.put("_inject", inject);315 options.put("_filter", filter);316 return Chrome.start(options, KarateRunner.buildScenarioEngine().runtime);317 }318 public static Chrome start(Map<String, Object> options, ScenarioEngine engine, Consumer<Map<String, Object>> filter,319 Boolean inject) {320 options.put("_inject", inject);321 options.put("_filter", filter);322 return Chrome.start(options, engine.runtime);323 }324 public static Chrome start(String chromeExecutablePath, boolean headless) {325 Map<String, Object> options = new HashMap();326 options.put("executable", chromeExecutablePath);327 options.put("headless", headless);328 return Chrome.start(options, null);329 }330 public static Chrome start(Map<String, Object> options) {331 if (options == null) {332 options = new HashMap();333 }334 return Chrome.start(options, null);335 }336 public static Chrome start() {337 return start(null);338 }339 public static Chrome startHeadless() {340 return start(Collections.singletonMap("headless", true));341 }342}...

Full Screen

Full Screen

Chrome

Using AI Code Generation

copy

Full Screen

1Chrome chrome = Chrome.start()2Firefox firefox = Firefox.start()3Safari safari = Safari.start()4Edge edge = Edge.start()5PhantomJS phantomJS = PhantomJS.start()6IEDriver ieDriver = IEDriver.start()7OperaDriver operaDriver = OperaDriver.start()8AndroidDriver androidDriver = AndroidDriver.start()9IOSDriver iosDriver = IOSDriver.start()10HtmlUnitDriver htmlUnitDriver = HtmlUnitDriver.start()11RemoteWebDriver remoteWebDriver = RemoteWebDriver.start()12WebKitDriver webKitDriver = WebKitDriver.start()13WebDriver webDriver = WebDriver.start()14ChromeOptions chromeOptions = ChromeOptions.start()15FirefoxOptions firefoxOptions = FirefoxOptions.start()16SafariOptions safariOptions = SafariOptions.start()17EdgeOptions edgeOptions = EdgeOptions.start()18PhantomJSOptions phantomJSOptions = PhantomJSOptions.start()

Full Screen

Full Screen

Chrome

Using AI Code Generation

copy

Full Screen

1* def driver = com.intuit.karate.driver.chrome.Chrome.start()2* driver.findElement('name', 'q').sendKeys('karate')3* driver.findElement('name', 'q').sendKeys('\n')4* driver.findElement('linkText', 'Karate - API Testing for the Enterprise').click()5* driver.findElement('linkText', 'docs').click()6* driver.findElement('linkText', 'Quickstart').click()7* driver.getTitle() == 'Quickstart - Karate'8* driver.quit()

Full Screen

Full Screen

Chrome

Using AI Code Generation

copy

Full Screen

1def chrome = com.intuit.karate.driver.chrome.Chrome()2def firefox = com.intuit.karate.driver.firefox.Firefox()3def edge = com.intuit.karate.driver.edge.Edge()4def safari = com.intuit.karate.driver.safari.Safari()5def chrome = com.intuit.karate.driver.chrome.Chrome()6def firefox = com.intuit.karate.driver.firefox.Firefox()7def edge = com.intuit.karate.driver.edge.Edge()8def safari = com.intuit.karate.driver.safari.Safari()9def chrome = com.intuit.karate.driver.chrome.Chrome()10def firefox = com.intuit.karate.driver.firefox.Firefox()

Full Screen

Full Screen

Chrome

Using AI Code Generation

copy

Full Screen

1* def driver = new com.intuit.karate.driver.chrome.Chrome()2* def title = driver.getTitle()3* driver.quit()4var driver = new com.intuit.karate.driver.chrome.Chrome()5var title = driver.getTitle()6print(title)7driver.quit()8WebDriver driver = new Chrome();9String title = driver.getTitle();10System.out.println(title);11driver.quit();

Full Screen

Full Screen

Chrome

Using AI Code Generation

copy

Full Screen

1Chrome chrome = Chrome.start()2chrome.type('#lst-ib', 'karate')3chrome.click('input[value="Google Search"]')4chrome.waitForVisible('h3.r a')5chrome.click('h3.r a')6chrome.waitForVisible('h1')7chrome.close()8Firefox firefox = Firefox.start()9firefox.type('#lst-ib', 'karate')10firefox.click('input[value="Google Search"]')11firefox.waitForVisible('h3.r a')12firefox.click('h3.r a')13firefox.waitForVisible('h1')14firefox.close()15InternetExplorer ie = InternetExplorer.start()16ie.type('#lst-ib', 'karate')17ie.click('input[value="Google Search"]')18ie.waitForVisible('h3.r a')19ie.click('h3.r a')20ie.waitForVisible('h1')21ie.close()22Safari safari = Safari.start()23safari.type('#lst-ib', 'karate')24safari.click('input[value="Google Search"]')25safari.waitForVisible('h3.r a')26safari.click('h3.r a')27safari.waitForVisible('h1')28safari.close()29PhantomJS phantom = PhantomJS.start()30phantom.type('#lst-ib', 'karate')31phantom.click('input[value="Google Search"]')32phantom.waitForVisible('h3.r a')33phantom.click('h3.r a')34phantom.waitForVisible('h1')35phantom.close()36HtmlUnit htmlUnit = HtmlUnit.start()37htmlUnit.type('#lst-ib', 'karate')38htmlUnit.click('input[value="Google Search"]')39htmlUnit.waitForVisible('h3.r a')40htmlUnit.click('h3.r a')41htmlUnit.waitForVisible('h1')

Full Screen

Full Screen

Chrome

Using AI Code Generation

copy

Full Screen

1def chrome = com.intuit.karate.driver.chrome.Chrome(options)2chrome.start()3def firefox = com.intuit.karate.driver.firefox.Firefox(profile)4firefox.start()5def safari = com.intuit.karate.driver.safari.Safari()6safari.start()7def edge = com.intuit.karate.driver.edge.Edge()8edge.start()9def phantomjs = com.intuit.karate.driver.phantomjs.PhantomJS()10phantomjs.start()11def opera = com.intuit.karate.driver.opera.Opera()12opera.start()13def android = com.intuit.karate.driver.android.Android()14android.start()15def ios = com.intuit.karate.driver.ios.Ios()16ios.start()17def remote = com.intuit.karate.driver.remote.Remote()18remote.start()19def htmlunit = com.intuit.karate.driver.htmlunit.HtmlUnit()20htmlunit.start()

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.

Run Karate automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in Chrome

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful