Skip to main content

TypeScript

介绍

Introduction

Playwright 开箱即用地支持 TypeScript。你只需用 TypeScript 编写测试,Playwright 就会读取它们,转换为 JavaScript 并运行。

Playwright supports TypeScript out of the box. You just write tests in TypeScript, and Playwright will read them, transform to JavaScript and run.

tsconfig.json

Playwright 将为它加载的每个源文件选取 tsconfig.json。请注意,Playwright 仅支持以下 tsconfig 选项:pathsbaseUrl

Playwright will pick up tsconfig.json for each source file it loads. Note that Playwright only supports the following tsconfig options: paths and baseUrl.

我们建议在测试目录中设置一个单独的 tsconfig.json,以便你可以专门针对测试更改一些首选项。这是一个示例目录结构。

We recommend setting up a separate tsconfig.json in the tests directory so that you can change some preferences specifically for the tests. Here is an example directory structure.

src/
source.ts

tests/
tsconfig.json # test-specific tsconfig
example.spec.ts

tsconfig.json # generic tsconfig for all typescript sources

playwright.config.ts

tsconfig 路径映射

tsconfig path mapping

Playwright 支持 tsconfig.json 中声明的 路径映射。确保 baseUrl 也已设置。

Playwright supports path mapping declared in the tsconfig.json. Make sure that baseUrl is also set.

以下是适用于 Playwright Test 的 tsconfig.json 示例:

Here is an example tsconfig.json that works with Playwright Test:

{
"compilerOptions": {
"baseUrl": ".", // This must be specified if "paths" is.
"paths": {
"@myhelper/*": ["packages/myhelper/*"] // This mapping is relative to "baseUrl".
}
}
}

你现在可以使用映射路径导入:

You can now import using the mapped paths:

example.spec.ts
import { test, expect } from '@playwright/test';
import { username, password } from '@myhelper/credentials';

test('example', async ({ page }) => {
await page.getByLabel('User Name').fill(username);
await page.getByLabel('Password').fill(password);
});

使用 TypeScript 手动编译测试

Manually compile tests with TypeScript

有时,Playwright Test 将无法正确转换你的 TypeScript 代码,例如当你使用 TypeScript 的实验性或最新功能(通常在 tsconfig.json 中配置)时。

Sometimes, Playwright Test will not be able to transform your TypeScript code correctly, for example when you are using experimental or very recent features of TypeScript, usually configured in tsconfig.json.

在这种情况下,你可以在将测试发送给 Playwright 之前执行自己的 TypeScript 编译。

In this case, you can perform your own TypeScript compilation before sending the tests to Playwright.

首先在测试目录中添加一个 tsconfig.json 文件:

First add a tsconfig.json file inside the tests directory:

{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"moduleResolution": "Node",
"sourceMap": true,
"outDir": "../tests-out",
}
}

package.json 中,添加两个脚本:

In package.json, add two scripts:

{
"scripts": {
"pretest": "tsc --incremental -p tests/tsconfig.json",
"test": "playwright test -c tests-out"
}
}

pretest 脚本在测试中运行 typescript。test 将运行已生成到 tests-out 目录的测试。-c 参数将测试运行器配置为在 tests-out 目录中查找测试。

The pretest script runs typescript on the tests. test will run the tests that have been generated to the tests-out directory. The -c argument configures the test runner to look for tests inside the tests-out directory.

然后 npm run test 将构建测试并运行它们。

Then npm run test will build the tests and run them.

evaluate() 内使用 import

Using import inside evaluate()

不支持在传递给各种 evaluate() 方法的函数内使用动态导入。这是因为 Playwright 使用 Function.prototype.toString() 来序列化函数,而编译器有时会用 require() 调用替换动态导入,这在网页内无效。

Using dynamic imports inside a function passed to various evaluate() methods is not supported. This is because Playwright uses Function.prototype.toString() to serialize functions, and transpiler will sometimes replace dynamic imports with require() calls, which are not valid inside the web page.

要解决此问题,请使用字符串模板而不是函数:

To work around this issue, use a string template instead of a function:

await page.evaluate(`(async () => {
const { value } = await import('some-module');
console.log(value);
})()`);