website/docs/ocr-testing/ocr-wait-for-text-displayed.md
Wait for a specific text to be displayed on the screen.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
});
[0-0] 2024-05-26T04:32:52.005Z INFO webdriver: COMMAND ocrWaitForTextDisplayed(<object>)
......................
# ocrWaitForTextDisplayed uses ocrGetElementPositionByText under the hood, that is why you see the command ocrGetElementPositionByText in the logs
[0-0] 2024-05-26T04:32:52.735Z INFO @wdio/ocr-service:ocrGetElementPositionByText: Multiple matches were found based on the word "specFileRetries". The match "specFileRetries" with score "100%" will be used.
textstringThe text you want to search for to click on.
await browser.ocrWaitForTextDisplayed({ text: "specFileRetries" });
timeoutnumberTime in milliseconds. Be aware that the OCR process can take some time, so don't set it too low.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries"
timeout: 25000 // wait for 25 seconds
});
timeoutMsgstringCould not find the text "{selector}" within the requested time.It overrides the default error message.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries"
timeoutMsg: "My new timeout message."
});
contrastnumber0.25The higher the contrast, the darker the image and vice versa. This can help to find text in an image. It accepts values between -1 and 1.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
contrast: 0.5,
});
haystacknumberWebdriverIO.Element | ChainablePromiseElement | RectangleThis is the search area in the screen where the OCR needs to look for text. This can be an element or a rectangle containing x, y, width and height
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
haystack: $("elementSelector"),
});
// OR
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
haystack: await $("elementSelector"),
});
// OR
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
haystack: {
x: 10,
y: 50,
width: 300,
height: 75,
},
});
languagestringengThe language that Tesseract will recognize. More info can be found here and the supported languages can be found here.
import { SUPPORTED_OCR_LANGUAGES } from "@wdio/ocr-service";
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
// Use Dutch as a language
language: SUPPORTED_OCR_LANGUAGES.DUTCH,
});
fuzzyFindOptionsYou can alter the fuzzy logic to find text with the following options. This might help find a better match
fuzzyFindOptions.distancenumberDetermines how close the match must be to the fuzzy location (specified by location). An exact letter match which is distance characters away from the fuzzy location would score as a complete mismatch. A distance of 0 requires the match to be at the exact location specified. A distance of 1000 would require a perfect match to be within 800 characters of the location to be found using a threshold of 0.8.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
distance: 20,
},
});
fuzzyFindOptions.locationnumberDetermines approximately where in the text is the pattern expected to be found.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
location: 20,
},
});
fuzzyFindOptions.thresholdnumberAt what point does the matching algorithm give up. A threshold of 0 requires a perfect match (of both letters and location), a threshold of 1.0 would match anything.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
threshold: 0.8,
},
});
fuzzyFindOptions.isCaseSensitivebooleanWhether the search should be case sensitive.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
isCaseSensitive: true,
},
});
fuzzyFindOptions.minMatchCharLengthnumberOnly the matches whose length exceeds this value will be returned. (For instance, if you want to ignore single character matches in the result, set it to 2)
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
minMatchCharLength: 5,
},
});
fuzzyFindOptions.findAllMatchesnumberWhen true, the matching function will continue to the end of a search pattern even if a perfect match has already been located in the string.
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
findAllMatches: 100,
},
});