克拉尼布拉·陶里驾驶员
Tauri应用软件通过Webdriver进行端到端测试.
@crabnebula/tauri-driver作为Webdriver客户端定义集成测试与目标平台的Webdriver执行之间的桥梁.
目前支持的平台:
- 为Tauri通过CrabNebula网络司机的macOS
- Linux 通过 Webkit2gtk- driver 或 CrabNebula 网络driver 程序
- 通过嵌入器或CrabNebula Webdriver的窗口
先决条件
- 你的Tauri应用程序必须拥有 tauri-plugin-自定义 为运行 MacOS 的集成测试而安装的插件
- Linux: Webkit2gtk- driver 必须安装在您的系统中并在 PATH 中可用
- Windows: msedgriver.exe 必须安装在您的系统中,并在 PATH 中可用
或者对 Linux 和 Windows 来说,您也可以使用 Tauri- plugin- automation 并配置您的测试来执行测试跑取后端 .
安装
安装您最喜欢的软件包管理器 :
npm install --save-dev @crabnebula/tauri-driver
yarn install -D @crabnebula/tauri-driver
pnpm install -D @crabnebula/tauri-driver
此外,你应该安装 @ crabnebula/ test-runner-后端 如果你想在本地进行macOS测试 @crabnebula/webdriverio-cloud-reporter (英语) 上传测试结果到 蟹状云。 。 。 .
使用量
网络司机
让我们定义一个 WebdriverIO 配置 这样它就可以进行你的整合测试.
- 导入包
import path from"path";
import { spawn, spawnSync } from "child_process";
import { CrabNebulaCloudReporter } from "@crabnebula/webdriverio-cloud-reporter";
import { waitTauriDriverReady } from "@crabnebula/tauri-driver";
import { waitTestRunnerBackendReady } from "@crabnebula/test-runner-backend";
- 设置您的应用程序路径
对于macOS,你可以选择使用 .app 而不是仅仅你的应用程序可执行文件.
const applicationPath = process.platform === "darwin"
? "path/to/src-tauri/target/debug/bundle/macos/<app-name>.app"
: process.platform === "win32"
? "path/to/src-tauri/target/debug/<app-name>.exe"
: "path/to/src-tauri/target/debug/<app-name>"
注意你必须使用合适的 target 相对于您的 WebdriverIO 配置文件的文件夹路径
替换 <app-name> 与正确的值.
- 创建全局变量
// keep track of the `tauri-driver` child process
let tauriDriver;
let killedTauriDriver = false;
// keep track of the `test-runner-backend` child process
let testRunnerBackend;
let killedTestRunnerBackend = false;
- 定义 WebdriverIO 配置
注意此示例假设您正在使用 pnpm,如果使用不同的软件包管理器,请调整脚本.
exports.config = {
host: "127.0.0.1",
port: 4444,
specs: ["./test/specs/**/*.js"],
maxInstances: 1,
capabilities: [
{
maxInstances: 1,
"tauri:options": {
application: applicationPath,
},
},
],
reporters: [CrabNebulaCloudReporter],
framework: "mocha",
mochaOpts: {
ui: "bdd",
timeout: 60000,
},
connectionRetryCount: 0,
onPrepare: async () => {
// ensure the Tauri app is built since we expect this binary to exist for the webdriver sessions
spawnSync("pnpm", ["tauri", "build", "--debug"], {
cwd: path.resolve(__dirname, "path/to/src-tauri"), // TODO: use actual path to the Tauri app directory
stdio: "inherit",
shell: true,
});
// skip this if condition to use the CrabNebula Webdriver on all platforms
if (process.platform === "darwin") {
// CN_API_KEY is required to run macOS tests via CrabNebula Webdriver for Tauri
if (!process.env.CN_API_KEY) {
console.error(
"CN_API_KEY is not set, required for CrabNebula Webdriver"
);
process.exit(1);
}
testRunnerBackend = spawn("pnpm", ["test-runner-backend"], {
stdio: "inherit",
shell: true,
});
testRunnerBackend.on("error", (error) => {
console.error("test-runner-backend error:", error);
process.exit(1);
});
testRunnerBackend.on("exit", (code) => {
if (!killedTestRunnerBackend) {
console.error("test-runner-backend exited with code:", code);
process.exit(1);
}
});
await waitTestRunnerBackendReady();
// instruct tauri-driver to connect to the test-runner-backend
process.env.REMOTE_WEBDRIVER_URL = `http://127.0.0.1:3000`;
}
},
// ensure we are running `tauri-driver` before the session starts so that we can proxy the webdriver requests
beforeSession: async () => {
tauriDriver = spawn("pnpm", ["tauri-driver"], {
stdio: [null, process.stdout, process.stderr],
shell: true,
});
tauriDriver.on("error", (error) => {
console.error("tauri-driver error:", error);
process.exit(1);
});
tauriDriver.on("exit", (code) => {
if (!killedTauriDriver) {
console.error("tauri-driver exited with code:", code);
process.exit(1);
}
});
// wait for tauri-driver to initialize its proxy server
await waitTauriDriverReady();
},
// clean up the `tauri-driver` process we spawned at the start of the session
afterSession: () => {
closeTauriDriver();
},
onComplete: () => {
killedTestRunnerBackend = true;
testRunnerBackend?.kill();
},
};
function closeTauriDriver() {
killedTauriDriver = true;
tauriDriver?.kill();
killedTestRunnerBackend = true;
testRunnerBackend?.kill();
}
export function onShutdown(fn) {
const cleanup = () => {
try {
fn();
} finally {
process.exit();
}
};
process.on("exit", cleanup);
process.on("SIGINT", cleanup);
process.on("SIGTERM", cleanup);
process.on("SIGHUP", cleanup);
process.on("SIGBREAK", cleanup);
}
onShutdown(closeTauriDriver);