跳至主要內容

ocrWaitForTextDisplayed

等待特定文字顯示在螢幕上。

用法

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.

選項

text

  • 類型: string
  • 必要:

您要搜尋以點擊的文字。

範例

await browser.ocrWaitForTextDisplayed({ text: "specFileRetries" });

timeout

  • 類型: number
  • 必要:
  • 預設值: 18000 (18 秒)

以毫秒為單位的時間。請注意,OCR 流程可能需要一些時間,因此不要設定太低。

範例

await browser.ocrWaitForTextDisplayed({
text: "specFileRetries"
timeout: 25000 // wait for 25 seconds
});

timeoutMsg

  • 類型: string
  • 必要:
  • 預設值: Could not find the text "{selector}" within the requested time.

它會覆蓋預設錯誤訊息。

範例

await browser.ocrWaitForTextDisplayed({
text: "specFileRetries"
timeoutMsg: "My new timeout message."
});

contrast

  • 類型: number
  • 必要:
  • 預設值: 0.25

對比度越高,圖像越暗,反之亦然。這可以幫助在圖像中找到文字。它接受介於 -11 之間的值。

範例

await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
contrast: 0.5,
});

haystack

  • 類型: number
  • 必要: WebdriverIO.Element | ChainablePromiseElement | Rectangle

這是螢幕上的搜尋區域,OCR 需要在此處尋找文字。這可以是元素或包含 xywidthheight 的矩形

範例

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,
},
});

language

  • 類型: string
  • 必要:
  • 預設值: eng

Tesseract 將辨識的語言。更多資訊可以在此處找到,而支援的語言可以在此處找到。

範例

import { SUPPORTED_OCR_LANGUAGES } from "@wdio/ocr-service";
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
// Use Dutch as a language
language: SUPPORTED_OCR_LANGUAGES.DUTCH,
});

fuzzyFindOptions

您可以使用以下選項變更模糊邏輯來尋找文字。這可能有助於找到更好的匹配

fuzzyFindOptions.distance

  • 類型: number
  • 必要:
  • 預設值 100

決定匹配必須與模糊位置(由位置指定)有多接近。與模糊位置相距距離字元的確切字母匹配將被視為完全不匹配。距離為 0 表示匹配必須在指定位置。距離為 1000 表示完美匹配必須在位置的 800 個字元內才能以 0.8 的閾值找到。

範例
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
distance: 20,
},
});

fuzzyFindOptions.location

  • 類型: number
  • 必要:
  • 預設值 0

決定預期在文字中大約哪個位置找到模式。

範例
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
location: 20,
},
});

fuzzyFindOptions.threshold

  • 類型: number
  • 必要:
  • 預設值 0.6

匹配演算法在何時放棄。閾值為 0 表示需要完全匹配(包括字母和位置),閾值為 1.0 表示匹配任何內容。

範例
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
threshold: 0.8,
},
});

fuzzyFindOptions.isCaseSensitive

  • 類型: boolean
  • 必要:
  • 預設值: false

搜尋是否應區分大小寫。

範例
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
isCaseSensitive: true,
},
});

fuzzyFindOptions.minMatchCharLength

  • 類型: number
  • 必要:
  • 預設值 2

只會傳回長度超過此值的匹配。(例如,如果您想忽略結果中的單字元匹配,請將其設定為 2)

範例
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
minMatchCharLength: 5,
},
});

fuzzyFindOptions.findAllMatches

  • 類型: number
  • 必要:
  • 預設值: false

true 時,即使已在字串中找到完美匹配,匹配函式仍會繼續到搜尋模式的結尾。

範例
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
fuzzyFindOptions: {
findAllMatches: 100,
},
});

歡迎!我能如何協助您?

WebdriverIO AI Copilot