TypeScript 设置
您可以使用 TypeScript 编写测试以获得自动完成和类型安全。
您需要在 devDependencies
中安装 tsx
,通过:
- npm
- Yarn
- pnpm
- Bun
$ npm install tsx --save-dev
$ yarn add tsx --dev
$ pnpm add tsx --save-dev
$ bun add tsx --dev
WebdriverIO 会自动检测是否安装了这些依赖项,并为您编译配置和测试。确保在与 WDIO 配置相同的目录中有一个 tsconfig.json
。
自定义 TSConfig
如果您需要为 tsconfig.json
设置不同的路径,请使用 TSCONFIG_PATH 环境变量设置您想要的路径,或使用 wdio 配置的 tsConfigPath 设置。
或者,您可以使用 tsx
的环境变量。
类型检查
请注意,tsx
不支持类型检查 - 如果您希望检查类型,则需要在单独的步骤中使用 tsc
进行检查。
框架设置
您的 tsconfig.json
需要包含以下内容:
tsconfig.json
{
"compilerOptions": {
"types": ["node", "@wdio/globals/types"]
}
}
请避免显式导入 webdriverio
或 @wdio/sync
。
一旦在 tsconfig.json
的 types
中添加,WebdriverIO
和 WebDriver
类型将可从任何地方访问。如果您使用其他 WebdriverIO 服务、插件或 devtools
自动化包,请也将它们添加到 types
列表中,因为许多提供额外的类型定义。
框架类型
根据您使用的框架,您需要将该框架的类型添加到 tsconfig.json
的 types 属性中,并安装其类型定义。如果您想要内置断言库 expect-webdriverio
的类型支持,这一点尤为重要。
例如,如果您决定使用 Mocha 框架,您需要安装 @types/mocha
并像这样添加它,以使所有类型全局可用:
- Mocha
- Jasmine
- Cucumber
tsconfig.json
{
"compilerOptions": {
"types": ["node", "@wdio/globals/types", "@wdio/mocha-framework"]
}
}
tsconfig.json
{
"compilerOptions": {
"types": ["node", "@wdio/globals/types", "@wdio/jasmine-framework"]
}
}
tsconfig.json
{
"compilerOptions": {
"types": ["node", "@wdio/globals/types", "@wdio/cucumber-framework"]
}
}