Best Karate code snippet using com.intuit.karate.core.ScenarioEngine.autoDef
Source:Driver.java
1/*2 * The MIT License3 *4 * Copyright 2018 Intuit Inc.5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in14 * all copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22 * THE SOFTWARE.23 */24package com.intuit.karate.driver;25import com.intuit.karate.core.AutoDef;26import com.intuit.karate.core.Plugin;27import com.intuit.karate.core.Config;28import com.intuit.karate.core.FeatureRuntime;29import com.intuit.karate.core.ScenarioEngine;30import com.intuit.karate.core.ScenarioRuntime;31import com.intuit.karate.core.StepResult;32import com.intuit.karate.http.ResourceType;33import java.util.ArrayList;34import java.util.Collections;35import java.util.List;36import java.util.Map;37import java.util.function.Predicate;38import java.util.function.Supplier;39/**40 *41 * @author pthomas342 */43public interface Driver extends Plugin {44 public static Driver start(String browserType) {45 return start(Collections.singletonMap("type", browserType));46 }47 public static Driver start(Map<String, Object> options) {48 ScenarioRuntime runtime = FeatureRuntime.forTempUse().scenarios.next();49 ScenarioEngine.set(runtime.engine);50 return DriverOptions.start(options, runtime);51 }52 @AutoDef53 void activate();54 @AutoDef55 void refresh();56 @AutoDef57 void reload();58 @AutoDef59 void back();60 @AutoDef61 void forward();62 @AutoDef63 void maximize();64 @AutoDef65 void minimize();66 @AutoDef67 void fullscreen();68 @AutoDef69 void close();70 @AutoDef71 void quit();72 @AutoDef73 void switchPage(String titleOrUrl);74 @AutoDef75 void switchPage(int index);76 @AutoDef77 void switchFrame(int index);78 @AutoDef79 void switchFrame(String locator);80 String getUrl(); // getter81 void setUrl(String url); // setter 82 Map<String, Object> getDimensions(); // getter83 void setDimensions(Map<String, Object> map); // setter84 String getTitle(); // getter85 List<String> getPages(); // getter86 String getDialogText(); // getter87 @AutoDef88 byte[] screenshot(boolean embed);89 @AutoDef90 default byte[] screenshot() {91 return screenshot(true);92 }93 @AutoDef94 Map<String, Object> cookie(String name);95 @AutoDef96 default void setCookies(List<Map<String, Object>> cookies) {97 System.out.println("got this cookie: " + cookies);98 cookies.forEach(c -> cookie(c));99 }100 @AutoDef101 void cookie(Map<String, Object> cookie);102 @AutoDef103 void deleteCookie(String name);104 @AutoDef105 void clearCookies();106 List<Map> getCookies(); // getter 107 @AutoDef108 void dialog(boolean accept);109 @AutoDef110 void dialog(boolean accept, String input);111 @AutoDef112 Object script(String expression);113 @AutoDef114 boolean waitUntil(String expression);115 @AutoDef116 Driver submit();117 @AutoDef118 default Driver retry() {119 return retry(null, null);120 }121 @AutoDef122 default Driver retry(int count) {123 return retry(count, null);124 }125 @AutoDef126 default Driver retry(Integer count, Integer interval) {127 getOptions().enableRetry(count, interval);128 return this;129 }130 @AutoDef131 default Driver delay(int millis) {132 getOptions().sleep(millis);133 return this;134 }135 @AutoDef136 Driver timeout(Integer millis);137 @AutoDef138 Driver timeout();139 // element actions =========================================================140 //141 @AutoDef142 Element focus(String locator);143 @AutoDef144 Element clear(String locator);145 @AutoDef146 Element click(String locator);147 @AutoDef148 Element input(String locator, String value);149 @AutoDef150 default Element input(String locator, String[] values) {151 return input(locator, values, 0);152 }153 @AutoDef154 default Element input(String locator, String chars, int delay) {155 String[] array = new String[chars.length()];156 for (int i = 0; i < array.length; i++) {157 array[i] = Character.toString(chars.charAt(i));158 }159 return input(locator, array, delay);160 }161 @AutoDef162 default Element input(String locator, String[] values, int delay) {163 Element element = DriverElement.locatorUnknown(this, locator);164 for (String value : values) {165 if (delay > 0) {166 delay(delay);167 }168 element = input(locator, value);169 }170 return element;171 }172 @AutoDef173 Element select(String locator, String text);174 @AutoDef175 Element select(String locator, int index);176 @AutoDef177 Element value(String locator, String value);178 @AutoDef179 default Element waitFor(String locator) {180 return getOptions().waitForAny(this, locator);181 }182 @AutoDef183 default String waitForUrl(String expected) {184 return getOptions().waitForUrl(this, expected);185 }186 @AutoDef187 default Element waitForText(String locator, String expected) {188 return waitUntil(locator, "_.textContent.includes('" + expected + "')");189 }190 @AutoDef191 default Element waitForEnabled(String locator) {192 return waitUntil(locator, "!_.disabled");193 }194 @AutoDef195 default List<Element> waitForResultCount(String locator, int count) {196 return (List) waitUntil(() -> {197 List<Element> list = locateAll(locator);198 return list.size() == count ? list : null;199 });200 }201 @AutoDef202 default List waitForResultCount(String locator, int count, String expression) {203 return (List) waitUntil(() -> {204 List list = scriptAll(locator, expression);205 return list.size() == count ? list : null;206 });207 }208 @AutoDef209 default Element waitForAny(String locator1, String locator2) {210 return getOptions().waitForAny(this, new String[]{locator1, locator2});211 }212 @AutoDef213 default Element waitForAny(String[] locators) {214 return getOptions().waitForAny(this, locators);215 }216 @AutoDef217 default Element waitUntil(String locator, String expression) {218 return getOptions().waitUntil(this, locator, expression);219 }220 @AutoDef221 default Object waitUntil(Supplier<Object> condition) {222 return getOptions().retry(() -> condition.get(), o -> o != null, "waitUntil (function)", true);223 }224 @AutoDef225 default Element locate(String locator) {226 Element e = DriverElement.locatorUnknown(this, locator);227 if (e.isPresent()) {228 return e;229 }230 throw new RuntimeException("cannot find locator: " + locator);231 }232 @AutoDef233 default List<Element> locateAll(String locator) {234 return getOptions().findAll(this, locator);235 }236 @AutoDef237 default List<Element> locateAll(String locator, Predicate predicate) {238 List before = locateAll(locator);239 List after = new ArrayList(before.size());240 for (Object o : before) {241 if (predicate.test(o)) {242 after.add(o);243 }244 }245 return after;246 }247 @AutoDef248 default Element scroll(String locator) {249 script(locator, DriverOptions.SCROLL_JS_FUNCTION);250 return DriverElement.locatorExists(this, locator);251 }252 @AutoDef253 default Element highlight(String locator) {254 return highlight(locator, Config.DEFAULT_HIGHLIGHT_DURATION);255 }256 default Element highlight(String locator, int millis) {257 script(getOptions().highlight(locator, millis));258 delay(millis);259 return DriverElement.locatorExists(this, locator);260 }261 @AutoDef262 default void highlightAll(String locator) {263 highlightAll(locator, Config.DEFAULT_HIGHLIGHT_DURATION);264 }265 default void highlightAll(String locator, int millis) {266 script(getOptions().highlightAll(locator, millis));267 delay(millis);268 }269 // friendly locators =======================================================270 //271 @AutoDef272 default Finder rightOf(String locator) {273 return new ElementFinder(this, locator, ElementFinder.Type.RIGHT);274 }275 @AutoDef276 default Finder leftOf(String locator) {277 return new ElementFinder(this, locator, ElementFinder.Type.LEFT);278 }279 @AutoDef280 default Finder above(String locator) {281 return new ElementFinder(this, locator, ElementFinder.Type.ABOVE);282 }283 @AutoDef284 default Finder below(String locator) {285 return new ElementFinder(this, locator, ElementFinder.Type.BELOW);286 }287 @AutoDef288 default Finder near(String locator) {289 return new ElementFinder(this, locator, ElementFinder.Type.NEAR);290 }291 // mouse and keys ==========================================================292 //293 @AutoDef294 default Mouse mouse() {295 return new DriverMouse(this);296 }297 @AutoDef298 default Mouse mouse(String locator) {299 return new DriverMouse(this).move(locator);300 }301 @AutoDef302 default Mouse mouse(int x, int y) {303 return new DriverMouse(this).move(x, y);304 }305 @AutoDef306 default Keys keys() {307 return new Keys(this);308 }309 @AutoDef310 void actions(List<Map<String, Object>> actions);311 // element state ===========================================================312 //313 @AutoDef314 String html(String locator);315 @AutoDef316 String text(String locator);317 @AutoDef318 String value(String locator);319 @AutoDef320 String attribute(String locator, String name);321 @AutoDef322 String property(String locator, String name);323 @AutoDef324 boolean enabled(String locator);325 @AutoDef326 default boolean exists(String locator) {327 return getOptions().optional(this, locator).isPresent();328 }329 @AutoDef330 default Element optional(String locator) {331 return getOptions().optional(this, locator);332 }333 @AutoDef334 Map<String, Object> position(String locator);335 @AutoDef336 Map<String, Object> position(String locator, boolean relative);337 @AutoDef338 byte[] screenshot(String locator, boolean embed);339 @AutoDef340 default byte[] screenshot(String locator) {341 return screenshot(locator, true);342 }343 @AutoDef344 default Object script(String locator, String expression) {345 String js = getOptions().scriptSelector(locator, expression);346 return script(js);347 }348 @AutoDef349 default List scriptAll(String locator, String expression) {350 String js = getOptions().scriptAllSelector(locator, expression);351 return (List) script(js);352 }353 @AutoDef354 default List scriptAll(String locator, String expression, Predicate predicate) {355 List before = scriptAll(locator, expression);356 List after = new ArrayList(before.size());357 for (Object o : before) {358 if (predicate.test(o)) {359 after.add(o);360 }361 }362 return after;363 }364 @AutoDef365 public byte[] pdf(Map<String, Object> options);366 // for internal use ========================================================367 // 368 boolean isTerminated();369 DriverOptions getOptions();370 Object elementId(String locator);371 List elementIds(String locator);372 static final List<String> METHOD_NAMES = Plugin.methodNames(Driver.class);373 @Override374 default List<String> methodNames() {375 return METHOD_NAMES;376 }377 @Override378 default Map<String, Object> afterScenario() {379 return Collections.EMPTY_MAP; // TODO380 }381 @Override382 public default void onFailure(StepResult stepResult) {383 if (getOptions().screenshotOnFailure && !stepResult.isWithCallResults()) {384 byte[] bytes = screenshot(false);385 getRuntime().embed(bytes, ResourceType.PNG);386 }387 }388}...
autoDef
Using AI Code Generation
1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.core.Feature3import com.intuit.karate.core.FeatureRuntime4import com.intuit.karate.core.Scenario5import com.intuit.karate.core.ScenarioContext6import com.intuit.karate.core.ScenarioRuntime7import com.intuit.karate.core.FeatureContext8import com.intuit.karate.core.FeatureRuntime9import com.intuit.karate.core.Feature10import com.intuit.karate.core.FeatureContext11import com.intuit.karate.core.FeatureRuntime12ScenarioEngine autoDef = ScenarioEngine.autoDef()13Feature feature = Feature.read("classpath:com/intuit/karate/core/feature.feature")14FeatureContext featureContext = new FeatureContext(feature, autoDef)15FeatureRuntime featureRuntime = new FeatureRuntime(featureContext)16Scenario scenario = feature.getScenario(0)17ScenarioContext scenarioContext = new ScenarioContext(scenario, featureContext)18ScenarioRuntime scenarioRuntime = new ScenarioRuntime(scenarioContext)19scenarioRuntime.run()20import com.intuit.karate.core.ScenarioEngine21import com.intuit.karate.core.Feature22import com.intuit.karate.core.FeatureRuntime23import com.intuit.karate.core.Scenario24import com.intuit.karate.core.ScenarioContext25import com.intuit.karate.core.ScenarioRuntime26import com.intuit.karate.core.FeatureContext27import com.intuit.karate.core.FeatureRuntime28import com.intuit.karate.core.Feature29import com.intuit.karate.core.FeatureContext30import com.intuit.karate.core.FeatureRuntime31Feature feature = Feature.read("classpath:com/intuit/karate/core/feature.feature")32FeatureRuntime featureRuntime = new FeatureRuntime(feature, ScenarioEngine.autoDef())33featureRuntime.run()34import com.intuit.karate.core.ScenarioEngine35import com.intuit.karate.core.Feature36import com.intuit.karate.core.FeatureRuntime37import com.intuit.karate.core.Scenario38import com.intuit.karate.core.ScenarioContext39import com.intuit.karate.core.ScenarioRuntime40import com.intuit.karate.core.FeatureContext41import com.intuit.kar
autoDef
Using AI Code Generation
1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.core.FeatureContext3import com.intuit.karate.core.FeatureRuntime4import com.intuit.karate.core.Feature5import com.intuit.karate.core.FeatureRuntimeOptions6 * def engine = new ScenarioEngine()7 * def options = new FeatureRuntimeOptions()8 * options.setFeatureContext(new FeatureContext())9 * options.setFeatureRuntime(new FeatureRuntime())10 * options.setFeature(new Feature())11 * options.getFeatureContext().setFeature(options.getFeature())12 * options.getFeatureRuntime().setOptions(options)13 * options.getFeatureRuntime().setEngine(engine)14 * options.getFeatureRuntime().setFeatureContext(options.getFeatureContext())15 * options.getFeatureRuntime().setFeature(options.getFeature())16 * options.getFeatureRuntime().setScriptBindings(options.getFeatureContext().getBindings())17 * engine.setOptions(options)18 * def d = engine.autoDef(c)
autoDef
Using AI Code Generation
1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.core.Feature3import com.intuit.karate.core.FeatureRuntime4import com.intuit.karate.core.FeatureRuntimeOptions5import com.intuit.karate.core.FeatureRuntimeOptionsBuilder6import com.intuit.karate.core.FeatureRuntimeOptionsBuilder$FeatureRuntimeOptionsBuilderImpl7import com.intuit.karate.core.FeatureRuntimeOptionsBuilder$FeatureRuntimeOptionsBuilderImpl$FeatureRuntimeOptionsImpl8import com.intuit.karate.core.FeatureRuntimeOptionsBuilder$FeatureRuntimeOptionsBuilderImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl9import com.intuit.karate.core.FeatureRuntimeOptionsBuilder$FeatureRuntimeOptionsBuilderImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl10import com.intuit.karate.core.FeatureRuntimeOptionsBuilder$FeatureRuntimeOptionsBuilderImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl11import com.intuit.karate.core.FeatureRuntimeOptionsBuilder$FeatureRuntimeOptionsBuilderImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl12import com.intuit.karate.core.FeatureRuntimeOptionsBuilder$FeatureRuntimeOptionsBuilderImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl$FeatureRuntimeOptionsImpl13import com.intuit.karate.core.FeatureRuntimeOptionsBuilder$FeatureRuntimeOptionsBuilderImpl$FeatureRuntimeOptionsImpl$F
autoDef
Using AI Code Generation
1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.core.Feature3import com.intuit.karate.core.FeatureRuntime4Feature feature = Feature.read("classpath:my.feature")5ScenarioEngine engine = new ScenarioEngine(feature, null)6FeatureRuntime runtime = engine.autoDef("my.feature", "my.feature")7Feature feature = Feature.read("classpath:my.feature")8FeatureRuntime runtime = FeatureRuntime.autoDef(feature, "my.feature")
autoDef
Using AI Code Generation
1engine.autoDef('def foo = { -> 42 }')2assert foo() == 423ScenarioRuntime runtime = ScenarioRuntime.of(engine, null)4runtime.autoDef('def bar = { -> 43 }')5assert bar() == 436FeatureRuntime featureRuntime = FeatureRuntime.of(engine, null)7featureRuntime.autoDef('def baz = { -> 44 }')8assert baz() == 449FeatureRuntime featureRuntime = FeatureRuntime.of(engine, null)10featureRuntime.autoDef('def qux = { -> 45 }')11assert qux() == 4512FeatureRuntime featureRuntime = FeatureRuntime.of(engine, null)13featureRuntime.autoDef('def quux = { -> 46 }')14assert quux() == 4615FeatureRuntime featureRuntime = FeatureRuntime.of(engine, null)16featureRuntime.autoDef('def quuz = { -> 47 }')17assert quuz() == 4718FeatureRuntime featureRuntime = FeatureRuntime.of(engine, null)19featureRuntime.autoDef('def corge = { -> 48 }')20assert corge() == 4821FeatureRuntime featureRuntime = FeatureRuntime.of(engine, null)22featureRuntime.autoDef('def grault = { -> 49 }')23assert grault() == 4924FeatureRuntime featureRuntime = FeatureRuntime.of(engine, null)25featureRuntime.autoDef('def garply = { -> 50 }')26assert garply() == 5027FeatureRuntime featureRuntime = FeatureRuntime.of(engine, null)28featureRuntime.autoDef('def waldo = { ->
autoDef
Using AI Code Generation
1* def result = autoDef('value', 'default')2* def result = autoDef('value', 'default')3* def result = autoDef('value', 'default')4* def result = autoDef('value', 'default')5* def result = autoDef('value', 'default')6* def value = { 'key': 'value' }7* def result = autoDef('value', 'default')8* match result == { 'key': 'value' }9* def result = autoDef('value', 'default')10* def result = autoDef('value', 'default')11* def result = autoDef('value', 'default')12* def result = autoDef('value', 'default')
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!!