ocrSetValue
Send a sequence of key strokes to an element. It will:
- automatically detect the element
- put focus on the field by clicking on it
- set the value in the field
The command will search for the provided text and try 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
await brower.ocrSetValue({
text: "docs",
value: "specfileretries",
});
Output
Logs
[0-0] 2024-05-26T04:17:51.355Z INFO webdriver: COMMAND ocrSetValue(<object>)
......................
[0-0] 2024-05-26T04:17:52.356Z INFO @wdio/ocr-service:ocrGetElementPositionByText: We searched for the word "docs" and found one match "docs" with score "100%"
Options
text
- Type:
string
- Mandatory: yes
The text you want to search for to click on.
Example
await browser.ocrSetValue({
text: "WebdriverIO",
value: "The Value",
});
value
- Type:
string
- Mandatory: yes
Value to be added.
Example
await browser.ocrSetValue({
text: "WebdriverIO",
value: "The Value",
});
submitValue
- Type:
boolean
- Mandatory: no
- Default:
false
If the value also needs to be submitted into the input field. This means an "ENTER" will be send at the end of the string.
Example
await browser.ocrSetValue({
text: "WebdriverIO",
value: "The Value",
submitValue: true,
});
clickDuration
- Type:
number
- Mandatory: no
- Default:
500
milliseconds
This is the duration of the click. If you want you can also create a "long click" by increasing the time.
Example
await browser.ocrSetValue({
text: "WebdriverIO",
value: "The Value",
clickDuration: 3000, // This is 3 seconds
});
contrast
- Type:
number
- Mandatory: no
- Default:
0.25
The 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
.
Example
await browser.ocrSetValue({
text: "WebdriverIO",
value: "The Value",
contrast: 0.5,
});
haystack
- Type:
number
- Mandatory:
WebdriverIO.Element | ChainablePromiseElement | Rectangle
This 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
Example
await browser.ocrSetValue({
text: "WebdriverIO",
value: "The Value",
haystack: $("elementSelector"),
});
// OR
await browser.ocrSetValue({
text: "WebdriverIO",
value: "The Value",
haystack: await $("elementSelector"),
});
// OR
await browser.ocrSetValue({
text: "WebdriverIO",
value: "The Value",
haystack: {
x: 10,
y: 50,
width: 300,
height: 75,
},
});