How to use prefixReturn method of com.intuit.karate.driver.WebDriver class

Best Karate code snippet using com.intuit.karate.driver.WebDriver.prefixReturn

Source:WebDriver.java Github

copy

Full Screen

...120 protected boolean isCookieError(Response res) {121 return res.getStatus() != 200;122 }123 private Element evalLocator(String locator, String dotExpression) {124 eval(prefixReturn(DriverOptions.selector(locator) + "." + dotExpression));125 // if the js above did not throw an exception, the element exists126 return DriverElement.locatorExists(this, locator);127 }128 private Element evalFocus(String locator) {129 eval(options.focusJs(locator));130 // if the js above did not throw an exception, the element exists131 return DriverElement.locatorExists(this, locator);132 }133 protected Variable eval(String expression, List args) {134 Json json = Json.object().set("script", expression).set("args", (args == null) ? Collections.EMPTY_LIST : args);135 Response res = http.path("execute", "sync").post(json);136 if (isJavaScriptError(res)) {137 logger.warn("javascript failed, will retry once: {}", res.getBodyAsString());138 options.sleep();139 res = http.path("execute", "sync").post(json);140 if (isJavaScriptError(res)) {141 String message = "javascript failed twice: " + res.getBodyAsString();142 logger.error(message);143 throw new RuntimeException(message);144 }145 }146 return new Variable(res.json().get("value"));147 }148 protected Variable eval(String expression) {149 return eval(expression, null);150 }151 protected String getElementKey() {152 return "element-6066-11e4-a52e-4f735466cecf";153 }154 protected String getJsonForInput(String text) {155 return Json.object().set("text", text).toString();156 }157 protected String getJsonForHandle(String text) {158 return Json.object().set("handle", text).toString();159 }160 protected String getJsonForFrame(String text) {161 return Json.object().set("id", text).toString();162 }163 protected String selectorPayload(String locator) {164 if (locator.startsWith("{")) {165 locator = DriverOptions.preProcessWildCard(locator);166 }167 Json json = Json.object();168 if (locator.startsWith("/")) {169 json.set("using", "xpath").set("value", locator);170 } else {171 json.set("using", "css selector").set("value", locator);172 }173 return json.toString();174 }175 @Override176 public String elementId(String locator) {177 String json = selectorPayload(locator);178 Response res = http.path("element").postJson(json);179 if (isLocatorError(res)) {180 logger.warn("locator failed, will retry once: {}", res.getBodyAsString());181 options.sleep();182 res = http.path("element").postJson(json);183 if (isLocatorError(res)) {184 String message = "locator failed twice: " + res.getBodyAsString();185 logger.error(message);186 throw new RuntimeException(message);187 }188 }189 return res.json().<List<String>>get("$.." + getElementKey()).get(0);190 }191 @Override192 public List<String> elementIds(String locator) {193 return http.path("elements")194 .postJson(selectorPayload(locator)).json().get("$.." + getElementKey());195 }196 @Override197 public DriverOptions getOptions() {198 return options;199 }200 @Override201 public void setUrl(String url) {202 Json json = Json.object().set("url", url);203 http.path("url").post(json);204 }205 @Override206 public Map<String, Object> getDimensions() {207 return http.path("window", "rect").get().json().get("value");208 }209 @Override210 public void setDimensions(Map<String, Object> map) {211 http.path("window", "rect").post(map);212 }213 @Override214 public void refresh() {215 http.path("refresh").postJson("{}");216 }217 @Override218 public void reload() {219 // not supported by webdriver220 refresh();221 }222 @Override223 public void back() {224 http.path("back").postJson("{}");225 }226 @Override227 public void forward() {228 http.path("forward").postJson("{}");229 }230 @Override231 public void maximize() {232 http.path("window", "maximize").postJson("{}");233 }234 @Override235 public void minimize() {236 http.path("window", "minimize").postJson("{}");237 }238 @Override239 public void fullscreen() {240 http.path("window", "fullscreen").postJson("{}");241 }242 @Override243 public Element focus(String locator) {244 return retryIfEnabled(locator, () -> evalFocus(locator));245 }246 @Override247 public Element clear(String locator) {248 return retryIfEnabled(locator, () -> evalLocator(locator, "value = ''"));249 }250 @Override251 public Element input(String locator, String value) {252 return retryIfEnabled(locator, () -> {253 String elementId;254 if (locator.startsWith("(")) {255 evalFocus(locator);256 elementId = http.path("element", "active").get()257 .json().getFirst("$.." + getElementKey());258 } else {259 elementId = elementId(locator);260 }261 http.path("element", elementId, "value").postJson(getJsonForInput(value));262 return DriverElement.locatorExists(this, locator);263 });264 }265 @Override266 public Element click(String locator) {267 return retryIfEnabled(locator, () -> evalLocator(locator, "click()"));268 }269 @Override270 public Driver submit() {271 options.setPreSubmitHash(getSubmitHash());272 return this;273 }274 @Override275 public Element select(String locator, String text) {276 return retryIfEnabled(locator, () -> {277 eval(options.optionSelector(locator, text));278 // if the js above did not throw an exception, the element exists279 return DriverElement.locatorExists(this, locator);280 });281 }282 @Override283 public Element select(String locator, int index) {284 return retryIfEnabled(locator, () -> {285 eval(options.optionSelector(locator, index));286 // if the js above did not throw an exception, the element exists287 return DriverElement.locatorExists(this, locator);288 });289 }290 @Override291 public void actions(List<Map<String, Object>> actions) {292 http.path("actions").post(Collections.singletonMap("actions", actions));293 }294 @Override295 public void close() {296 http.path("window").delete();297 open = false;298 }299 @Override300 public boolean isTerminated() {301 return terminated;302 }303 @Override304 public void quit() {305 if (terminated) {306 return;307 }308 terminated = true;309 if (open) {310 close();311 }312 // delete session313 try {314 http.delete();315 } catch (Exception e) {316 logger.warn("session delete failed: {}", e.getMessage());317 }318 if (command != null) {319 command.close(true);320 }321 }322 @Override323 public String getUrl() {324 return http.path("url").get().json().get("value");325 }326 private String evalReturn(String locator, String dotExpression) {327 return eval("return " + DriverOptions.selector(locator) + "." + dotExpression).getAsString();328 }329 @Override330 public String html(String locator) {331 return retryIfEnabled(locator, () -> evalReturn(locator, "outerHTML"));332 }333 @Override334 public String text(String locator) {335 return retryIfEnabled(locator, () -> evalReturn(locator, "textContent"));336 }337 @Override338 public String value(String locator) {339 return retryIfEnabled(locator, () -> evalReturn(locator, "value"));340 }341 @Override342 public Element value(String locator, String value) {343 return retryIfEnabled(locator, () -> evalLocator(locator, "value = '" + value + "'"));344 }345 @Override346 public String attribute(String locator, String name) {347 return retryIfEnabled(locator, () -> evalReturn(locator, "getAttribute('" + name + "')"));348 }349 @Override350 public String property(String locator, String name) {351 return retryIfEnabled(locator, () -> evalReturn(locator, name));352 }353 @Override354 public Map<String, Object> position(String locator) {355 return position(locator, false);356 }357 @Override358 public Map<String, Object> position(String locator, boolean relative) {359 return retryIfEnabled(locator, ()360 -> eval("return " + DriverOptions.selector(locator) + ".getBoundingClientRect()").getValue());361 }362 @Override363 public boolean enabled(String locator) {364 return retryIfEnabled(locator, ()365 -> eval("return !" + DriverOptions.selector(locator) + ".disabled").isTrue());366 }367 private String prefixReturn(String expression) {368 return expression.startsWith("return ") ? expression : "return " + expression;369 }370 @Override371 public boolean waitUntil(String expression) {372 return options.retry(() -> {373 try {374 return eval(prefixReturn(expression)).isTrue();375 } catch (Exception e) {376 logger.warn("waitUntil evaluate failed: {}", e.getMessage());377 return false;378 }379 }, b -> b, "waitUntil (js)", true);380 }381 @Override382 public Object script(String expression) {383 expression = prefixReturn(expression);384 return eval(expression).getValue();385 }386 @Override387 public String getTitle() {388 return http.path("title").get().json().get("value");389 }390 @Override391 public List<Map> getCookies() {392 return http.path("cookie").get().json().get("value");393 }394 @Override395 public Map<String, Object> cookie(String name) {396 return http.path("cookie", name).get().json().get("value");397 }...

Full Screen

Full Screen

prefixReturn

Using AI Code Generation

copy

Full Screen

1* def driver = prefixReturn { driver }2* def driver = prefixReturn { driver() }3* def driver = prefixReturn { driver }4* def driver = prefixReturn { driver() }5* def driver = prefixReturn { driver }6* def driver = prefixReturn { driver() }7* def driver = prefixReturn { driver }8* def driver = prefixReturn { driver() }9* def driver = prefixReturn { driver }10* def driver = prefixReturn { driver() }11* def driver = prefixReturn { driver }12* def driver = prefixReturn { driver() }13* def driver = prefixReturn { driver }14* def driver = prefixReturn { driver() }15* def driver = prefixReturn { driver }16* def driver = prefixReturn { driver() }17* def driver = prefixReturn { driver }18* def driver = prefixReturn { driver() }

Full Screen

Full Screen

prefixReturn

Using AI Code Generation

copy

Full Screen

1* def driver = karate.call('classpath:com/intuit/karate/driver/webdriver.feature').driver2* driver.prefixReturn(prefix)3* def response = driver.get('/hello')4* response.html() == '<html><body>hello world</body></html>'5* def driver = karate.call('classpath:com/intuit/karate/driver/appium.feature').driver6* driver.prefixReturn(prefix)7* def response = driver.get('/hello')8* response.html() == '<html><body>hello world</body></html>'9* def driver = karate.call('classpath:com/intuit/karate/driver/jetty.feature').driver10* driver.prefixReturn(prefix)11* def response = driver.get('/hello')12* response.html() == '<html><body>hello world</body></html>'13* def driver = karate.call('classpath:com/intuit/karate/driver/okhttp.feature').driver14* driver.prefixReturn(prefix)15* def response = driver.get('/hello')16* response.html() == '<html><body>hello world</body></html>'17* def driver = karate.call('classpath:com/intuit/karate/driver/rest-assured.feature').driver18* driver.prefixReturn(prefix)19* def response = driver.get('/hello')20* response.html() == '<html><body>hello world</body></html>'21* def driver = karate.call('classpath:com/intuit/kar

Full Screen

Full Screen

prefixReturn

Using AI Code Generation

copy

Full Screen

1* def driver = com.intuit.karate.driver.DriverFactory.getWebDriver()2* def prefixReturn = driver.prefixReturn('prefix')3* prefixReturn('return')4* def driver = com.intuit.karate.driver.DriverFactory.getDriver()5* def prefixReturn = driver.prefixReturn('prefix')6* prefixReturn('return')7* def driver = com.intuit.karate.driver.DriverFactory.getDriver()8* def prefixReturn = driver.prefixReturn('prefix')9* prefixReturn('return')10* def driver = com.intuit.karate.driver.DriverFactory.getDriver()11* def prefixReturn = driver.prefixReturn('prefix')12* prefixReturn('return')13* def driver = com.intuit.karate.driver.DriverFactory.getDriver()14* def prefixReturn = driver.prefixReturn('prefix')15* prefixReturn('return')16* def driver = com.intuit.karate.driver.DriverFactory.getDriver()17* def prefixReturn = driver.prefixReturn('prefix')18* prefixReturn('return')19* def driver = com.intuit.karate.driver.DriverFactory.getDriver()20* def prefixReturn = driver.prefixReturn('prefix')21* prefixReturn('return')22* def driver = com.intuit.karate.driver.DriverFactory.getDriver()23* def prefixReturn = driver.prefixReturn('prefix')24* prefixReturn('return')25* def driver = com.intuit.karate.driver.DriverFactory.getDriver()26* def prefixReturn = driver.prefixReturn('prefix')27* prefixReturn('return')

Full Screen

Full Screen

prefixReturn

Using AI Code Generation

copy

Full Screen

1* def driver = prefixReturn('driver')2* def element = prefixReturn('element')3* def text = prefixReturn('text')4* def value = prefixReturn('value')5def driver = prefixReturn('driver')6def element = prefixReturn('element')7def text = prefixReturn('text')8def value = prefixReturn('value')9* def driver = prefixReturn('driver')10* def element = prefixReturn('element')11* def text = prefixReturn('text')12* def value = prefixReturn('value')13def driver = prefixReturn('driver')14def element = prefixReturn('element')15def text = prefixReturn('text')16def value = prefixReturn('value')17* def driver = prefixReturn('driver')18* def element = prefixReturn('element')19* def text = prefixReturn('text')20* def value = prefixReturn('value')21def driver = prefixReturn('driver')22def element = prefixReturn('element')23def text = prefixReturn('text')24def value = prefixReturn('value')25* def driver = prefixReturn('driver')

Full Screen

Full Screen

prefixReturn

Using AI Code Generation

copy

Full Screen

1* def driver = karate.getWebDriver()2* def prefixReturn = driver.prefixReturn(prefix)3* prefixReturn = driver.prefixReturn(prefix, ' ')4* prefixReturn = driver.prefixReturn(prefix, ' ', ' ')5* def driver = karate.getWebDriver()6* def prefixReturn = driver.prefixReturn(prefix)7* prefixReturn = driver.prefixReturn(prefix, ' ')8* prefixReturn = driver.prefixReturn(prefix, ' ', ' ')9* def driver = karate.getWebDriver()10* def prefixReturn = driver.prefixReturn(prefix)11* prefixReturn = driver.prefixReturn(prefix, ' ')12* prefixReturn = driver.prefixReturn(prefix, ' ', ' ')13* def driver = karate.getWebDriver()14* def prefixReturn = driver.prefixReturn(prefix)15* prefixReturn = driver.prefixReturn(prefix, ' ')16* prefixReturn = driver.prefixReturn(prefix, ' ', ' ')17* def driver = karate.getWebDriver()18* def prefixReturn = driver.prefixReturn(prefix)19* prefixReturn = driver.prefixReturn(prefix, ' ')20* prefixReturn = driver.prefixReturn(prefix, ' ', ' ')21* def driver = karate.getWebDriver()22* def prefixReturn = driver.prefixReturn(prefix)

Full Screen

Full Screen

prefixReturn

Using AI Code Generation

copy

Full Screen

1def driver = driver()2def response = prefix.get("/hello")3def driver = driver()4def response = prefix.get("/hello")5def driver = driver()6def response = prefix.get("/hello")7def driver = driver()8def response = prefix.get("/hello")9def driver = driver()10def response = prefix.get("/hello")11def driver = driver()12def response = prefix.get("/hello")13def driver = driver()14def response = prefix.get("/hello")15def driver = driver()16def response = prefix.get("/hello")17def driver = driver()

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