跳至主要內容

測試執行時間

預設情況下,此模組會檢查您的機器/管線中是否已在本機安裝 Tesseract。如果您沒有在本機安裝,它將自動使用 NodeJS 版本。這可能會導致一些緩慢,因為影像處理將由 Node.js 完成。NodeJS 不是執行大量處理的最佳系統。

但是......,有一些方法可以優化執行時間。讓我們看看以下測試腳本

import { browser } from "@wdio/globals";

describe("Search", () => {
it("be able to search for a value", async () => {
await browser.url("https://webbrowser.io");
await browser.ocrClickOnText({
text: "Search",
});
await browser.ocrSetValue({
text: "docs",
value: "specfileretries",
});
await browser.ocrWaitForTextDisplayed({
text: "specFileRetries",
});
});
});

當您第一次執行此操作時,您可能會看到以下結果,其中完成測試耗時 5.9 秒。

npm run wdio -- --logLevel=silent

> ocr-demo@1.0.0 wdio
> wdio run ./wdio.conf.ts --logLevel=silent


Execution of 1 workers started at 2024-05-26T04:52:53.405Z

[0-0] RUNNING in chrome - file:///test/specs/test.e2e.ts
[0-0] Estimating resolution as 182
[0-0] Estimating resolution as 124
[0-0] Estimating resolution as 126
[0-0] PASSED in chrome - file:///test/specs/test.e2e.ts

"spec" Reporter:
------------------------------------------------------------------
[chrome 125.0.6422.78 mac #0-0] Running: chrome (v125.0.6422.78) on mac
[chrome 125.0.6422.78 mac #0-0] Session ID: d281dcdc43962b95835aea8f64cab6c7
[chrome 125.0.6422.78 mac #0-0]
[chrome 125.0.6422.78 mac #0-0] » /test/specs/test.e2e.ts
[chrome 125.0.6422.78 mac #0-0] Search
[chrome 125.0.6422.78 mac #0-0] ✓ be able to search for a value
[chrome 125.0.6422.78 mac #0-0]
[chrome 125.0.6422.78 mac #0-0] 1 passing (5.9s)


Spec Files: 1 passed, 1 total (100% completed) in 00:00:08

裁切螢幕的搜尋區域

您可以透過提供裁切區域來執行 OCR 來優化執行時間。

如果您然後將腳本變更為此

import { browser } from "@wdio/globals";

describe("Search", () => {
it("be able to search for a value", async () => {
await browser.url("https://webdriverio.dev.org.tw");
await driver.ocrClickOnText({
haystack: $(".DocSearch"),
text: "Search",
});
await driver.ocrSetValue({
haystack: $(".DocSearch-Form"),
text: "docs",
value: "specfileretries",
});
await driver.ocrWaitForTextDisplayed({
haystack: $(".DocSearch-Dropdown"),
text: "specFileRetries",
});
});
});

那麼您將會看到不同的執行時間。

npm run wdio -- --logLevel=silent

> ocr-demo@1.0.0 wdio
> wdio run ./wdio.conf.ts --logLevel=silent


Execution of 1 workers started at 2024-05-26T04:56:55.326Z

[0-0] RUNNING in chrome - file:///test/specs/test.e2e.ts
[0-0] Estimating resolution as 182
[0-0] Estimating resolution as 124
[0-0] Estimating resolution as 124
[0-0] PASSED in chrome - file:///test/specs/test.e2e.ts

"spec" Reporter:
------------------------------------------------------------------
[chrome 125.0.6422.78 mac #0-0] Running: chrome (v125.0.6422.78) on mac
[chrome 125.0.6422.78 mac #0-0] Session ID: c6cb1843535bda3ee3af07920ce232b8
[chrome 125.0.6422.78 mac #0-0]
[chrome 125.0.6422.78 mac #0-0] » /test/specs/test.e2e.ts
[chrome 125.0.6422.78 mac #0-0] Search
[chrome 125.0.6422.78 mac #0-0] ✓ be able to search for a value
[chrome 125.0.6422.78 mac #0-0]
[chrome 125.0.6422.78 mac #0-0] 1 passing (4.8s)


Spec Files: 1 passed, 1 total (100% completed) in 00:00:08
裁切影像

這將本機執行時間從 5.9 減少到 4.8 秒。這是減少了近 19%。想像一下,它對於具有更多資料的較大腳本可以做些什麼。

使用 Tesseract 的本機安裝

如果您的本機和/或管線中有 Tessarect 的本機安裝,您可以將執行時間加速到甚至不到一分鐘(關於在本機系統上安裝 Tesseract 的更多資訊可以在此處找到)。您可以在下方找到使用 Tesseract 本機安裝的相同腳本的執行時間。

npm run wdio -- --logLevel=silent

> ocr-demo@1.0.0 wdio
> wdio run ./wdio.conf.ts --logLevel=silent


Execution of 1 workers started at 2024-05-26T04:59:11.620Z

[0-0] RUNNING in chrome - file:///test/specs/test.e2e.ts
[0-0] PASSED in chrome - file:///test/specs/test.e2e.ts

"spec" Reporter:
------------------------------------------------------------------
[chrome 125.0.6422.78 mac #0-0] Running: chrome (v125.0.6422.78) on mac
[chrome 125.0.6422.78 mac #0-0] Session ID: 87f8c1e949e15a383b902e4d59b1f738
[chrome 125.0.6422.78 mac #0-0]
[chrome 125.0.6422.78 mac #0-0] » /test/specs/test.e2e.ts
[chrome 125.0.6422.78 mac #0-0] Search
[chrome 125.0.6422.78 mac #0-0] ✓ be able to search for a value
[chrome 125.0.6422.78 mac #0-0]
[chrome 125.0.6422.78 mac #0-0] 1 passing (3.9s)


Spec Files: 1 passed, 1 total (100% completed) in 00:00:06
本機安裝

這將本機執行時間從 5.9 減少到 3.9 秒。這是減少了近 34%。想像一下,它對於具有更多資料的較大腳本可以做些什麼。

歡迎!我能幫您什麼嗎?

WebdriverIO AI Copilot