setup
sidebar_position: 1
Setup示例
本示例应用程序仅关注在现有项目中添加 WebDriver 测试。为了在下面两节中测试一个项目,我们将设置一个极简的 Tauri 应用程序用于测试。我们不会使用 Tauri CLI、任何前端依赖项或构建步骤,也不会在之后捆绑应用程序。这只是为了展示在现有应用程序中添加 WebDriver 测试的最小套件。
如果您只想查看利用本示例指南中展示的内容完成的示例项目,请访问 https://github.com/chippers/hello_tauri。
初始化Cargo项目
我们要创建一个新的二进制 Cargo 项目来容纳这个示例应用程序。我们可以在命令行中使用 cargo new hello-tauri-webdriver --bin
,轻松实现这一操作,它将为我们搭建一个最小的二进制 Cargo 项目。该目录将作为本指南其余部分的工作目录,因此请确保运行的命令位于 hello-tauri-webdriver/
目录中。
创建极简前端
我们将创建一个最小的 HTML 文件作为示例应用程序的前端。稍后,我们还将在 WebDriver 测试中使用该前端的一些功能。
首先,让我们创建 Tauri distDir
,我们知道在构建应用程序的 Tauri 部分时会用到它。mkdir dist
将创建一个名为 dist/
的新目录,我们将在其中放置以下 index.html
文件。
dist/index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Hello Tauri!</title>
<style>
body {
/* Add a nice colorscheme */
background-color: #222831;
color: #ececec;
/* Make the body the exact size of the window */
margin: 0;
height: 100vh;
width: 100vw;
/* Vertically and horizontally center children of the body tag */
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<h1>Hello, Tauri!</h1>
</body>
</html>
将Tauri添加到Cargo项目
接下来,我们将添加必要的项目,将 Cargo 项目转化为 Tauri 项目。首先,在 Cargo Manifest(Cargo.toml
)中添加依赖项,这样 Cargo 就能在构建时调入我们的依赖项。
Cargo.toml
:
[package]
name = "hello-tauri-webdriver"
version = "0.1.0"
edition = "2021"
rust-version = "1.56"
# Needed to set up some things for Tauri at build time
[build-dependencies]
tauri-build = "1"
# The actual Tauri dependency, along with `custom-protocol` to serve the pages.
[dependencies]
tauri = { version = "1", features = ["custom-protocol"] }
# Make --release build a binary that is small (opt-level = "s") and fast (lto = true).
# This is completely optional, but shows that testing the application as close to the
# typical release settings is possible. Note: this will slow down compilation.
[profile.release]
incremental = false
codegen-units = 1
panic = "abort"
opt-level = "s"
lto = true
大家可能已经注意到,我们添加了一个 [build-dependency]
。要使用构建依赖关系,我们必须在构建脚本中使用它。现在我们将在 build.rs
中创建一个。
build.rs
:
fn main() {
// Only watch the `dist/` directory for recompiling, preventing unnecessary
// changes when we change files in other project subdirectories.
println!("cargo:rerun-if-changed=dist");
// Run the Tauri build-time helpers
tauri_build::build()
}
有了这些设置,我们的 Cargo 项目现在就知道如何调入和构建我们的 Tauri 依赖项了。最后,让我们在实际项目代码中设置 Tauri,使这个最小示例成为一个 Tauri 应用程序。我们将编辑 src/main.rs
文件以添加 Tauri 功能。
src/main.rs
:
fn main() {
tauri::Builder::default()
.run(tauri::generate_context!())
.expect("unable to run Tauri application");
}
很简单,对吧?
Tauri配置
要成功创建应用程序,我们需要两样东西。首先,我们需要一个图标文件。您可以使用任何 PNG 文件,然后将其复制到 icon.png
。通常情况下,在使用 Tauri CLI 创建项目时,脚手架会提供这个文件。要获取默认的Tauri图标,我们可以使用 curl -L "https://github.com/chippers/hello_tauri/raw/main/icon.png" --output icon.png
命令下载 Hello Tauri 示例库使用的图标。
我们需要一个 tauri.conf.json
,为 Tauri 设置一些重要的配置值。同样,这通常来自 tauri init
脚手架命令,但我们将在这里创建自己的最小配置。
tauri.conf.json
:
{
"build": {
"distDir": "dist"
},
"tauri": {
"bundle": {
"identifier": "studio.tauri.hello_tauri_webdriver",
"icon": ["icon.png"]
},
"allowlist": {
"all": false
},
"windows": [
{
"width": 800,
"height": 600,
"resizable": true,
"fullscreen": false
}
]
}
}
下面我将介绍其中的一些。你可以看到我们之前创建的 dist/
目录被指定为 distDir
属性。我们设置了一个捆绑标识符,以便构建的应用程序有一个唯一的 ID,并将 icon.png
设置为唯一的图标。我们不使用任何 Tauri API 或功能,因此在 allowlist
中设置 "all":false
,禁用它们。窗口值只是用一些合理的默认值创建一个窗口。
至此,我们就有了一个基本的 Hello World 应用程序,运行时会显示一个简单的问候语。
允许示例应用
为了确保我们做得正确,让我们构建这个应用程序!我们将以--release
应用程序的形式运行此应用程序,因为我们还将使用发布配置文件运行 WebDriver 测试。运行 cargo run --release
,经过编译后,我们会看到以下应用程序弹出。
注意:如果您正在修改应用程序并希望使用 Devtools,那么在不使用 --release
的情况下运行它,右键菜单中就会出现 "检查元素"。
现在,我们应该可以开始使用一些 WebDriver 框架来测试此应用程序了。本指南将依次介绍 WebdriverIO 和 Selenium。