Best Karate code snippet using com.intuit.karate.robot.RobotBase.locateImage
Source:RobotBase.java  
...429        // note that currentWindow will NOT point to the new window located430        return window;431    }432    protected Element optional(Element searchRoot, String locator) {433        Element found = locateImageOrElement(searchRoot, locator);434        if (found == null) {435            logger.warn("element does not exist: {}", locator);436            return new MissingElement(this);437        }438        if (highlight) {439            found.highlight();440        }441        return found;442    }443    protected Element locate(int duration, Element searchRoot, String locator) {444        Element found;445        if (retryEnabled) {446            found = retryForAny(true, searchRoot, locator);447        } else {448            found = locateImageOrElement(searchRoot, locator);449            if (found == null) {450                String message = "cannot locate: '" + locator + "' (" + searchRoot.getDebugString() + ")";451                logger.error(message);452                throw new RuntimeException(message);453            }454            if (duration > 0) {455                found.getRegion().highlight(duration);456            }457        }458        return found;459    }460    protected List<Element> locateAll(int duration, Element searchRoot, String locator) {461        List<Element> found;462        if (locator.endsWith(".png")) {463            found = locateAllImages(searchRoot, locator);464        } else if (locator.startsWith("{")) {465            found = locateAllText(searchRoot, locator);466        } else {467            found = locateAllInternal(searchRoot, locator);468        }469        if (duration > 0) {470            RobotUtils.highlightAll(searchRoot.getRegion(), found, duration, false);471        }472        return found;473    }474    @Override475    public Element move(String locator) {476        return locate(getHighlightDuration(), getSearchRoot(), locator).move();477    }478    @Override479    public Element click(String locator) {480        return locate(getHighlightDuration(), getSearchRoot(), locator).click();481    }482    @Override483    public Element select(String locator) {484        return locate(getHighlightDuration(), getSearchRoot(), locator).select();485    }486    @Override487    public Element press(String locator) {488        return locate(getHighlightDuration(), getSearchRoot(), locator).press();489    }490    @Override491    public Element release(String locator) {492        return locate(getHighlightDuration(), getSearchRoot(), locator).release();493    }494    private StringUtils.Pair parseOcr(String raw) { // TODO make object495        int pos = raw.indexOf('}');496        String lang = raw.substring(1, pos);497        if (lang.length() < 2) {498            lang = lang + tessLang;499        }500        String text = raw.substring(pos + 1);501        return StringUtils.pair(lang, text);502    }503    public List<Element> locateAllText(Element searchRoot, String path) {504        StringUtils.Pair pair = parseOcr(path);505        String lang = pair.left;506        boolean negative = lang.charAt(0) == '-';507        if (negative) {508            lang = lang.substring(1);509        }510        String text = pair.right;511        return Tesseract.findAll(this, lang, searchRoot.getRegion(), text, negative);512    }513    public Element locateText(Element searchRoot, String path) {514        StringUtils.Pair pair = parseOcr(path);515        String lang = pair.left;516        boolean negative = lang.charAt(0) == '-';517        if (negative) {518            lang = lang.substring(1);519        }520        String text = pair.right;521        return Tesseract.find(this, lang, searchRoot.getRegion(), text, negative);522    }523    private static class PathAndStrict {524        final int strictness;525        final String path;526        public PathAndStrict(String path) {527            int pos = path.indexOf(':');528            if (pos > 0 && pos < 3) {529                strictness = Integer.valueOf(path.substring(0, pos));530                this.path = path.substring(pos + 1);531            } else {532                strictness = 10;533                this.path = path;534            }535        }536    }537    public List<Element> locateAllImages(Element searchRoot, String path) {538        PathAndStrict ps = new PathAndStrict(path);539        List<Region> found = OpenCvUtils.findAll(ps.strictness, this, searchRoot.getRegion(), readBytes(ps.path), true);540        List<Element> list = new ArrayList(found.size());541        for (Region region : found) {542            list.add(new ImageElement(region));543        }544        return list;545    }546    public Element locateImage(Region region, String path) {547        PathAndStrict ps = new PathAndStrict(path);548        return locateImage(region, ps.strictness, readBytes(ps.path));549    }550    public Element locateImage(Region searchRegion, int strictness, byte[] bytes) {551        Region region = OpenCvUtils.find(strictness, this, searchRegion, bytes, true);552        if (region == null) {553            return null;554        }555        return new ImageElement(region);556    }557    @Override558    public Element window(String title) {559        return window(title, true, true);560    }561    private Element window(String title, boolean retry, boolean failWithException) {562        return window(new StringMatcher(title), retry, failWithException);563    }564    @Override565    public Element window(Predicate<String> condition) {566        return window(condition, true, true);567    }568    private Element window(Predicate<String> condition, boolean retry, boolean failWithException) {569        try {570            currentWindow = retry ? retry(() -> windowInternal(condition), w -> w != null, "find window", failWithException) : windowInternal(condition);571        } catch (Exception e) {572            if (failWithException) {573                throw e;574            }575            logger.warn("failed to find window: {}", e.getMessage());576            currentWindow = null;577        }578        if (currentWindow != null && highlight) { // currentWindow can be null579            currentWindow.highlight(getHighlightDuration());580        }581        return currentWindow;582    }583    protected Element getSearchRoot() {584        if (currentWindow == null) {585            logger.warn("using desktop as search root, activate a window or parent element for better performance");586            return getRoot();587        }588        return currentWindow;589    }590    @Override591    public Object waitUntil(Supplier<Object> condition) {592        return waitUntil(condition, true);593    }594    @Override595    public Object waitUntilOptional(Supplier<Object> condition) {596        return waitUntil(condition, false);597    }598    protected Object waitUntil(Supplier<Object> condition, boolean failWithException) {599        return retry(() -> condition.get(), o -> o != null, "waitUntil (function)", failWithException);600    }601    @Override602    public Element waitFor(String locator) {603        return retryForAny(true, getSearchRoot(), locator);604    }605    @Override606    public Element waitForOptional(String locator) {607        return retryForAny(false, getSearchRoot(), locator);608    }609    @Override610    public Element waitForAny(String locator1, String locator2) {611        return retryForAny(true, getSearchRoot(), locator1, locator2);612    }613    @Override614    public Element waitForAny(String[] locators) {615        return retryForAny(true, getSearchRoot(), locators);616    }617    protected Element retryForAny(boolean failWithException, Element searchRoot, String... locators) {618        Element found = retry(() -> waitForAny(searchRoot, locators), r -> r != null, "find by locator(s): " + Arrays.asList(locators), failWithException);619        return found == null ? new MissingElement(this) : found;620    }621    private Element waitForAny(Element searchRoot, String... locators) {622        for (String locator : locators) {623            Element found = locateImageOrElement(searchRoot, locator);624            if (found != null) {625                if (highlight) {626                    found.getRegion().highlight(highlightDuration);627                }628                return found;629            }630        }631        return null;632    }633    private Element locateImageOrElement(Element searchRoot, String locator) {634        if (locator.endsWith(".png")) {635            return locateImage(searchRoot.getRegion(), locator);636        } else if (locator.startsWith("{")) {637            return locateText(searchRoot, locator);638        } else if (searchRoot.isImage()) {639            // TODO640            throw new RuntimeException("todo find non-image elements within region");641        } else {642            return locateInternal(searchRoot, locator);643        }644    }645    @Override646    public Element activate(String locator) {647        return locate(locator).activate();648    }649    @Override...locateImage
Using AI Code Generation
1* def robot = new com.intuit.karate.robot.RobotBase()2* def image = robot.locateImage('src/test/resources/robot/locate-image.png')3* def robot = new com.intuit.karate.robot.RobotBase()4* def image = robot.locateImage('src/test/resources/robot/locate-image.png', 0.95)5* def robot = new com.intuit.karate.robot.RobotBase()6* def image = robot.locateImage('src/test/resources/robot/locate-image.png', 0.95)7* def robot = new com.intuit.karate.robot.RobotBase()8* def image = robot.locateImage('src/test/resources/robot/locate-image.png', 0.95)9* def robot = new com.intuit.karate.robot.RobotBase()10* def image = robot.locateImage('src/test/resources/robot/locate-image.png', 0.95)11* def robot = new com.intuit.karate.robot.RobotBase()locateImage
Using AI Code Generation
1import com.intuit.karate.robot.*2def robot = new RobotBase()3def image = robot.locateImage('src/test/resources/1.png')4if(image != null) {5    robot.click(image)6}locateImage
Using AI Code Generation
1* def robot = com.intuit.karate.robot.RobotBase()2* def image = robot.locateImage('karate.png')3* def robot = com.intuit.karate.robot.RobotBase()4* def image = robot.locateImage('karate.png')5* def robot = com.intuit.karate.robot.RobotBase()6* def image = robot.locateImage('karate.png')7* def robot = com.intuit.karate.robot.RobotBase()8* def image = robot.locateImage('karate.png')9* def robot = com.intuit.karate.robot.RobotBase()10* def image = robot.locateImage('karate.png')locateImage
Using AI Code Generation
1import com.intuit.karate.robot.RobotBase2import com.intuit.karate.robot.Screenshot3import com.intuit.karate.robot.Robot4def robot = new RobotBase()5def screenshot = new Screenshot()6def image = robot.locateImage(path, screenshot)7if (image != null) {8    println("Image found on screen")9} else {10    println("Image not found on screen")11}12import com.intuit.karate.robot.RobotBase13import com.intuit.karate.robot.Screenshot14import com.intuit.karate.robot.Robot15def robot = new RobotBase()16def screenshot = robot.getScreenshot()17def image = robot.locateImage(path, screenshot)18if (image != null) {19    println("Image found on screen")20} else {21    println("Image not found on screen")22}locateImage
Using AI Code Generation
1import com.intuit.karate.robot.RobotBase2def robot = new RobotBase()3def image = robot.locateImage('src/test/resources/locate-image.png')4if (image) {5  robot.mouseClick(image.x + image.width / 2, image.y + image.height / 2)6}7robot.mouseClick(100, 100)8robot.mouseClick(100, 100)9robot.keyPress('k')10robot.keyPress('a')11robot.keyPress('r')12robot.keyPress('a')13robot.keyPress('t')14robot.keyPress('e')15robot.mouseClick(100, 100)16robot.keyPress('k')17robot.keyPress('a')18robot.keyPress('r')19robot.keyPress('a')20robot.keyPress('t')21robot.keyPress('e')22robot.keyPress('enter')23robot.mouseClick(100, 100)24robot.keyPress('k')25robot.keyPress('a')26robot.keyPress('r')27robot.keyPress('a')28robot.keyPress('t')29robot.keyPress('e')30robot.keyPress('enter')31robot.wait(5)32robot.mouseClick(100, 100)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!!
