跳至主要内容

测试执行时间

默认情况下,此模块将检查您的机器/管道中是否安装了本地版本的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

> [email protected] 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.node.org.cn");
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

> [email protected] 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

> [email protected] 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