Best Karate code snippet using com.intuit.karate.robot.RobotBase.locateImageOrElement
Source:RobotBase.java
...434 // note that currentWindow will NOT point to the new window located435 return window;436 }437 protected Element optional(Element searchRoot, String locator) {438 Element found = locateImageOrElement(searchRoot, locator);439 if (found == null) {440 logger.warn("element does not exist: {}", locator);441 return new MissingElement(this);442 }443 if (highlight) {444 found.highlight();445 }446 return found;447 }448 protected Element locate(int duration, Element searchRoot, String locator) {449 Element found;450 if (retryEnabled) {451 found = retryForAny(true, searchRoot, locator);452 } else {453 found = locateImageOrElement(searchRoot, locator);454 if (found == null) {455 String message = "cannot locate: '" + locator + "' (" + searchRoot.getDebugString() + ")";456 logger.error(message);457 throw new RuntimeException(message);458 }459 if (duration > 0) {460 found.getRegion().highlight(duration);461 }462 }463 return found;464 }465 protected List<Element> locateAll(int duration, Element searchRoot, String locator) {466 List<Element> found;467 if (locator.endsWith(".png")) {468 found = locateAllImages(searchRoot, locator);469 } else if (locator.startsWith("{")) {470 found = locateAllText(searchRoot, locator);471 } else {472 found = locateAllInternal(searchRoot, locator);473 }474 if (duration > 0) {475 RobotUtils.highlightAll(searchRoot.getRegion(), found, duration, false);476 }477 return found;478 }479 @Override480 public Element move(String locator) {481 return locate(getHighlightDuration(), getSearchRoot(), locator).move();482 }483 @Override484 public Element click(String locator) {485 return locate(getHighlightDuration(), getSearchRoot(), locator).click();486 }487 @Override488 public Element select(String locator) {489 return locate(getHighlightDuration(), getSearchRoot(), locator).select();490 }491 @Override492 public Element press(String locator) {493 return locate(getHighlightDuration(), getSearchRoot(), locator).press();494 }495 @Override496 public Element release(String locator) {497 return locate(getHighlightDuration(), getSearchRoot(), locator).release();498 }499 private StringUtils.Pair parseOcr(String raw) { // TODO make object500 int pos = raw.indexOf('}');501 String lang = raw.substring(1, pos);502 if (lang.length() < 2) {503 lang = lang + tessLang;504 }505 String text = raw.substring(pos + 1);506 return StringUtils.pair(lang, text);507 }508 public List<Element> locateAllText(Element searchRoot, String path) {509 StringUtils.Pair pair = parseOcr(path);510 String lang = pair.left;511 boolean negative = lang.charAt(0) == '-';512 if (negative) {513 lang = lang.substring(1);514 }515 String text = pair.right;516 return Tesseract.findAll(this, lang, searchRoot.getRegion(), text, negative);517 }518 public Element locateText(Element searchRoot, String path) {519 StringUtils.Pair pair = parseOcr(path);520 String lang = pair.left;521 boolean negative = lang.charAt(0) == '-';522 if (negative) {523 lang = lang.substring(1);524 }525 String text = pair.right;526 return Tesseract.find(this, lang, searchRoot.getRegion(), text, negative);527 }528 private static class PathAndStrict {529 final int strictness;530 final String path;531 public PathAndStrict(String path) {532 int pos = path.indexOf(':');533 if (pos > 0 && pos < 3) {534 strictness = Integer.valueOf(path.substring(0, pos));535 this.path = path.substring(pos + 1);536 } else {537 strictness = 10;538 this.path = path;539 }540 }541 }542 public List<Element> locateAllImages(Element searchRoot, String path) {543 PathAndStrict ps = new PathAndStrict(path);544 List<Region> found = OpenCvUtils.findAll(ps.strictness, this, searchRoot.getRegion(), readBytes(ps.path), true);545 List<Element> list = new ArrayList(found.size());546 for (Region region : found) {547 list.add(new ImageElement(region));548 }549 return list;550 }551 public Element locateImage(Region region, String path) {552 PathAndStrict ps = new PathAndStrict(path);553 return locateImage(region, ps.strictness, readBytes(ps.path));554 }555 public Element locateImage(Region searchRegion, int strictness, byte[] bytes) {556 Region region = OpenCvUtils.find(strictness, this, searchRegion, bytes, true);557 if (region == null) {558 return null;559 }560 return new ImageElement(region);561 }562 @Override563 public Element window(String title) {564 return window(title, true, true);565 }566 private Element window(String title, boolean retry, boolean failWithException) {567 return window(new StringMatcher(title), retry, failWithException);568 }569 @Override570 public Element window(Predicate<String> condition) {571 return window(condition, true, true);572 }573 private Element window(Predicate<String> condition, boolean retry, boolean failWithException) {574 try {575 currentWindow = retry ? retry(() -> windowInternal(condition), w -> w != null, "find window", failWithException) : windowInternal(condition);576 } catch (Exception e) {577 if (failWithException) {578 throw e;579 }580 logger.warn("failed to find window: {}", e.getMessage());581 currentWindow = null;582 }583 if (currentWindow != null && highlight) { // currentWindow can be null584 currentWindow.highlight(getHighlightDuration());585 }586 return currentWindow;587 }588 protected Element getSearchRoot() {589 if (currentWindow == null) {590 logger.warn("using desktop as search root, activate a window or parent element for better performance");591 return getRoot();592 }593 return currentWindow;594 }595 @Override596 public Object waitUntil(Supplier<Object> condition) {597 return waitUntil(condition, true);598 }599 @Override600 public Object waitUntilOptional(Supplier<Object> condition) {601 return waitUntil(condition, false);602 }603 protected Object waitUntil(Supplier<Object> condition, boolean failWithException) {604 return retry(() -> condition.get(), o -> o != null, "waitUntil (function)", failWithException);605 }606 @Override607 public Element waitFor(String locator) {608 return retryForAny(true, getSearchRoot(), locator);609 }610 @Override611 public Element waitForOptional(String locator) {612 return retryForAny(false, getSearchRoot(), locator);613 }614 @Override615 public Element waitForAny(String locator1, String locator2) {616 return retryForAny(true, getSearchRoot(), locator1, locator2);617 }618 @Override619 public Element waitForAny(String[] locators) {620 return retryForAny(true, getSearchRoot(), locators);621 }622 protected Element retryForAny(boolean failWithException, Element searchRoot, String... locators) {623 Element found = retry(() -> waitForAny(searchRoot, locators), r -> r != null, "find by locator(s): " + Arrays.asList(locators), failWithException);624 return found == null ? new MissingElement(this) : found;625 }626 private Element waitForAny(Element searchRoot, String... locators) {627 for (String locator : locators) {628 Element found = locateImageOrElement(searchRoot, locator);629 if (found != null) {630 if (highlight) {631 found.getRegion().highlight(highlightDuration);632 }633 return found;634 }635 }636 return null;637 }638 private Element locateImageOrElement(Element searchRoot, String locator) {639 if (locator.endsWith(".png")) {640 return locateImage(searchRoot.getRegion(), locator);641 } else if (locator.startsWith("{")) {642 return locateText(searchRoot, locator);643 } else if (searchRoot.isImage()) {644 // TODO645 throw new RuntimeException("todo find non-image elements within region");646 } else {647 return locateInternal(searchRoot, locator);648 }649 }650 @Override651 public Element activate(String locator) {652 return locate(locator).activate();...
locateImageOrElement
Using AI Code Generation
1def robot = new com.intuit.karate.robot.RobotBase()2def image = robot.locateImageOrElement('images/element.png', 1, 1, 1, 1)3if (image != null) {4 robot.mouseMove(image.x, image.y)5 robot.mouseClick()6}7def robot = new com.intuit.karate.robot.Robot()8def image = robot.locateImageOrElement('images/element.png', 1, 1, 1, 1)9if (image != null) {10 robot.mouseMove(image.x, image.y)11 robot.mouseClick()12}13def robot = new com.intuit.karate.robot.Robot()14def image = robot.locateImageOrElement('images/element.png', 1, 1, 1, 1)15if (image != null) {16 robot.mouseMove(image.x, image.y)17 robot.mouseClick()18}19def robot = new com.intuit.karate.robot.Robot()20def image = robot.locateImageOrElement('images/element.png', 1, 1, 1, 1)21if (image != null) {22 robot.mouseMove(image.x, image.y)23 robot.mouseClick()24}25def robot = new com.intuit.karate.robot.Robot()26def image = robot.locateImageOrElement('images/element.png', 1, 1, 1, 1)27if (image != null) {28 robot.mouseMove(image.x, image.y)29 robot.mouseClick()30}31def robot = new com.intuit.karate.robot.Robot()32def image = robot.locateImageOrElement('images/element.png', 1, 1, 1, 1)33if (image != null) {34 robot.mouseMove(image.x, image.y)35 robot.mouseClick()36}
locateImageOrElement
Using AI Code Generation
1*def driver = com.intuit.karate.driver.DriverFactory.getDriver()2*def robot = new com.intuit.karate.robot.RobotBase(driver)3*def image = robot.locateImageOrElement("images/element.png", 0.8)4*def driver = com.intuit.karate.driver.DriverFactory.getDriver()5*def robot = new com.intuit.karate.robot.RobotBase(driver)6*def image = robot.locateImageOrElement("images/element.png", 0.8)7*def driver = com.intuit.karate.driver.DriverFactory.getDriver()8*def robot = new com.intuit.karate.robot.RobotBase(driver)9*def image = robot.locateImageOrElement("images/element.png", 0.8)10*def driver = com.intuit.karate.driver.DriverFactory.getDriver()11*def robot = new com.intuit.karate.robot.RobotBase(driver)12*def image = robot.locateImageOrElement("images/element.png", 0.8)13*def driver = com.intuit.karate.driver.DriverFactory.getDriver()14*def robot = new com.intuit.karate.robot.RobotBase(driver)15*def image = robot.locateImageOrElement("images/element.png", 0.8)16*def driver = com.intuit.karate.driver.DriverFactory.getDriver()17*def robot = new com.intuit.karate.robot.RobotBase(driver)18*def image = robot.locateImageOrElement("images/element.png", 0.8)19*def driver = com.intuit.karate.driver.DriverFactory.getDriver()20*def robot = new com.intuit.karate.robot.RobotBase(driver)21*def image = robot.locateImageOrElement("images/element.png", 0.8)22*def driver = com.intuit.karate.driver.DriverFactory.getDriver()23*def robot = new com.intuit.karate.robot.RobotBase(driver)24*def image = robot.locateImageOrElement("images/element.png", 0.8)25*def driver = com.intuit.karate.driver.DriverFactory.getDriver()26*def robot = new com.intuit.karate.robot.RobotBase(driver)27*def image = robot.locateImageOrElement("
locateImageOrElement
Using AI Code Generation
1def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)2if (image) {3 image.click()4}5def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)6if (image) {7 image.click()8}9def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)10if (image) {11 image.click()12}13def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)14if (image) {15 image.click()16}17def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)18if (image) {19 image.click()20}21def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)22if (image) {23 image.click()24}25def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)26if (image) {27 image.click()28}29def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)30if (image) {31 image.click()32}33def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)34if (image) {35 image.click()36}37def image = locateImageOrElement('file:src/test/resources/search.png', 0.9)
locateImageOrElement
Using AI Code Generation
1* def robot = com.intuit.karate.robot.RobotFactory.getRobot()2* def location = robot.locateImageOrElement('images/youtube.png', 'Search or type URL', 10000)3* if (location) {4* robot.click(location)5* } else {6* robot.type('{ENTER}')7* }8* def robot = com.intuit.karate.robot.RobotFactory.getRobot()9* def location = robot.locateImageOrElement(null, 'Search or type URL', 10000)10* if (location) {11* robot.click(location)12* } else {13* robot.type('{ENTER}')14* }15* def robot = com.intuit.karate.robot.RobotFactory.getRobot()16* def location = robot.locateImageOrElement('images/youtube.png', null, 10000)17* if (location) {18* robot.click(location)19* } else {20* robot.type('{ENTER}')21* }
locateImageOrElement
Using AI Code Generation
1def robot = com.intuit.karate.robot.RobotBase()2def img = robot.locateImageOrElement('src/test/resources/robot/robot.png')3logger.info('img: {}', img)4img = robot.locateImageOrElement('src/test/resources/robot/robot.png', 0.95)5logger.info('img: {}', img)6img = robot.locateImageOrElement('src/test/resources/robot/robot.png', 0.9)7logger.info('img: {}', img)8img = robot.locateImageOrElement('src/test/resources/robot/robot.png', 0.8)9logger.info('img: {}', img)10img = robot.locateImageOrElement('src/test/resources/robot/robot.png', 0.7)11logger.info('img: {}', img)12img = robot.locateImageOrElement('src/test/resources/robot/robot.png', 0.6)13logger.info('img: {}', img)14img = robot.locateImageOrElement('src/test/resources/robot/robot.png', 0.9, 0.9, 0.9, 0.9)15logger.info('img: {}', img)16img = robot.locateImageOrElement('src/test/resources/robot/robot.png', 0.9, 0.9, 0.9, 0.9, 0.9)17logger.info('img: {}', img)
locateImageOrElement
Using AI Code Generation
1def image = locateImageOrElement("test.png", 1, 1, 1, 1)2if (image) {3}4def image = locateImageOrElement("test.png", 1, 1, 1, 1)5if (image) {6}7def image = locateImageOrElement("test.png", 1, 1, 1, 1)8if (image) {9}10def image = locateImageOrElement("test.png", 1, 1, 1, 1)11if (image) {12}13def image = locateImageOrElement("test.png", 1, 1, 1, 1)14if (image) {15}16def image = locateImageOrElement("test.png", 1, 1, 1, 1)
locateImageOrElement
Using AI Code Generation
1def robot = com.intuit.karate.robot.RobotBase.get()2def image = robot.locateImageOrElement('target.png')3if (image) {4 robot.click(image)5}6def robot = com.intuit.karate.robot.RobotBase.get()7def image = robot.locateImageOrElement('target.png')8if (image) {9 robot.click(image)10}11def robot = com.intuit.karate.robot.RobotBase.get()12def image = robot.locateImageOrElement('target.png')13if (image) {14 robot.click(image)15}16def robot = com.intuit.karate.robot.RobotBase.get()17def image = robot.locateImageOrElement('target.png')18if (image) {19 robot.click(image)20}21def robot = com.intuit.karate.robot.RobotBase.get()22def image = robot.locateImageOrElement('target.png')23if (image) {24 robot.click(image)25}26def robot = com.intuit.karate.robot.RobotBase.get()27def image = robot.locateImageOrElement('target.png')28if (image) {29 robot.click(image)30}
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!!