Skip to main content

ocrGetElementPositionByText

Get the position of a text on the screen. The command will search for the provided text and tries to find a match based on Fuzzy Logic from Fuse.js. This means that if you might provide a selector with a typo, or the found text might not be a 100% match it will still try to give you back an element. See the logs below.

Usage#

driver.ocrGetElementPositionByText('Username')

Logs#

# Still finding a match even though we searched for "Usernames" and the found text was "Username"
[0-0] 2021-04-07T09:51:07.806Z INFO webdriver: COMMAND ocrGetElementPositionByText("Usernames")
[0-0] 2021-04-07T09:51:07.807Z INFO webdriver: RESULT true
[0-0] 2021-04-07T09:51:07.811Z INFO wdio-ocr-service: We searched for the word "Usernames" and found one match "Username" with score "88.89%"

Options#

NameTypeDefaultDetails
selectorstringThe visual name of the field
options (optional)GetTextOptions{}command options
options.reuseOcr (optional)booleanfalseRe-use a previous OCR scan if it is available
options.androidRectangles (optional)RectanglesRectangles for Android to crop the search area for OCR
options.androidRectangles.topnumberStart position from the top of the screen to start cropping the search area for OCR
options.androidRectangles.leftnumberStart position from the left of the screen to start cropping the search area for OCR
options.androidRectangles.rightnumberStart position from the right of the screen to start cropping the search area for OCR
options.androidRectangles.bottomnumberStart position from the bottom of the screen to start cropping the search area for OCR
options.iOSRectangles (optional)RectanglesRectangles for Android to crop the search area for OCR
options.iOSRectangles.topnumberStart position from the top of the screen to start cropping the search area for OCR
options.iOSRectangles.leftnumberStart position from the left of the screen to start cropping the search area for OCR
options.iOSRectangles.rightnumberStart position from the right of the screen to start cropping the search area for OCR
options.iOSRectangles.bottomnumberStart position from the bottom of the screen to start cropping the search area for OCR

Returns#

Returns the search value, the matched text, and the position of the element on the screen

const result = {
searchValue: 'Username',
matchedString: 'Username',
originalPosition: { left: 83, top: 326, right: 248, bottom: 352 },
dprPosition: { left: 41.5, top: 163, right: 124, bottom: 176 },
score: 100
}

Example#

it('should be able to the position of the visble text on the screen', () => {
driver.ocrGetElementPositionByText('Username')
// OR with options
driver.ocrGetElementPositionByText(
'Username',
{
// Same as for iOSRectangles
androidRectangles: {
top: 200,
left: 0,
right: 800,
bottom: 400,
},
reuseOcr:true,
},
)
})