@dfinity/internet-identity-playwright

中文文档
3.0.0发布于 2026/02/11Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 7541

A Playwright library to simplify the integration of Internet Identity authentication in E2E tests.

原始使用文档
文档目录14 节
  1. 🚀 Introduction
  2. 🖥️ Installation
  3. ✍️ Usage
  4. 1. Import the Fixtures
  5. 2. Write Your Tests
  6. 3. Wait for Internet Identity (optional)
  7. 4. Run Your Tests
  8. 💁‍♂️️ Tips & tricks
  9. Example Test
  10. Running Tests Locally
  11. Running Captcha Tests Locally
  12. Running Required Passkey Tests Locally
  13. 🚧 Limitations
  14. 🧑‍🤝‍🧑 Community

🔐 Internet Identity Playwright

A Playwright library to simplify the integration of Internet Identity authentication in E2E tests.


Internet Computer portal GitHub CI Checks Workflow Status GitHub CI Tests Workflow Status

🚀 Introduction

This repository offers Playwright fixtures designed to assist developers in creating end-to-end (E2E) tests for dApps utilizing Internet Identity. These pre-built scenarios allow developers to seamlessly integrate authentication flows, including the creation and reuse of identities, without needing to implement the flows themselves.

🖥️ Installation

# with npm
npm install --save-dev @dfinity/internet-identity-playwright
# with pnpm
pnpm add --save-dev @dfinity/internet-identity-playwright
# with yarn
yarn add -D @dfinity/internet-identity-playwright

✍️ Usage

To use the Internet Identity Playwright fixtures, follow these steps:

1. Import the Fixtures

In your Playwright test file, import the fixtures provided by this library.

import {testWithII} from '@dfinity/internet-identity-playwright';

2. Write Your Tests

Use the extended fixtures in your tests to perform authentication flows.

[!NOTE] The signIn() method automatically detects whether this is a first-time passkey flow or an existing passkey flow, handling the complexity for you. It is also worth noting that it always tries to reuse the same default identity that was created within a Playwright session to ensure the provided identity is reproducible.

testWithII('should sign-in with a user', async ({page, iiPage}) => {
  await page.goto('/');

  await iiPage.signIn();
});

The iiPage object represents the page of your application that contains the call to action to start the authentication flow with Internet Identity.

By default, the fixture will search for a button identified by the attribute [data-tid=login-button]. You can customize this behavior by providing your own selector.

const loginSelector = '#login';

testWithII('should sign-in with a user', async ({page, iiPage}) => {
  await page.goto('/');

  await iiPage.signIn({passkey: {selector: loginSelector}});
});

When creating a new passkey for the first time, the identity name is set by default to "Test". You can customize this behavior or create other accounts with the parameter account.

[!NOTE] Creating an account requires having signed in a first time within a Playwright session.

const loginSelector = '#login';

testWithII('should sign-in with a user', async ({page, iiPage}) => {
  await page.goto('/');

  await iiPage.signIn();

  await page.locator('#logout').click();

  await iiPage.signIn({passkey: {account: 'Hello World'}});
});

3. Wait for Internet Identity (optional)

You might encounter scenarios where you perform tests against a local replica started in parallel with your tests, commonly when automating the tests in a CI environment. The library also exposes a fixture that lets you wait for Internet Identity to be ready.

For example, you can provide the local replica URL and the canister ID on which Internet Identity has been deployed:

testWithII.beforeEach(async ({iiPage, browser}) => {
  const url = 'http://127.0.0.1:4943';
  const canisterId = 'rdmx6-jaaaa-aaaaa-aaadq-cai';

  await iiPage.waitReady({url, canisterId});
});

The function also accepts an optional timeout parameter to specify how long the function should wait for Internet Identity to be mounted, with a default value of 60000 milliseconds.

testWithII.beforeEach(async ({iiPage, browser}) => {
  const url = 'http://127.0.0.1:4943';
  const canisterId = 'rdmx6-jaaaa-aaaaa-aaadq-cai';
  const timeout = 30000;

  await iiPage.waitReady({url, canisterId, timeout});
});

4. Run Your Tests

Run your Playwright tests as usual.

npx playwright test

💁‍♂️️ Tips & tricks

Example Test

You can find an example test in the following file: login.spec.ts.

Running Tests Locally

To run these tests locally, follow the steps below:

  1. Install a container runtime:

Make sure you have an up-to-date container runtime installed on your machine, such as Docker or Podman.

The suite uses Docker by default. If you're using Podman instead, update the emulator config in demo/juno.config.mjs:

emulator: {
  runner: {
    type: "podman"
  },
  satellite: {}
}
  1. Install Juno's CLI:
npm i -g @junobuild/cli
  1. Start the emulator:

Navigate to the demo directory and start the environment:

cd demo
juno emulator start
  1. Setup the environment:

If this is the first time you're running the tests locally, you need to configure the backend of the demo application by authorizing your CLI to operate the demo's Satellite (canister) and applying its backend configuration:

juno login --emulator --mode development
juno config apply --mode development
  1. Run the Tests:

Return to the root directory and execute the tests:

npm run e2e

Running Captcha Tests Locally

The default test suite validates the use of Internet Identity without captcha requirements. To test a flow with captcha, run the following command in the demo directory:

docker compose -f docker-compose.captcha.yml up

Then, navigate to the root directory and run the dedicated test:

npm run e2e:captcha

Running Required Passkey Tests Locally

The default test suite validates the use of the latest Internet Identity, which does not require the user to complete a specific step to create a passkey. To test a flow where passkey creation is required, run the following command from the demo directory:

docker compose -f docker-compose.passkey.yml up

Then, navigate to the root directory and run the dedicated test:

npm run e2e:passkey

🚧 Limitations

Currently, the library's fixtures cannot be implemented with Playwright's ability to load existing authenticated state. Playwright currently does not support IndexedDB for such features. This limitation is tracked in their GitHub issue #11164.

While it is technically possible to use local storage instead of IndexedDB, this approach is generally discouraged as it does not reflect how identities are stored in the browser. We prefer to adhere to best practices for testing to ensure the most accurate simulation of real-world scenarios.

🧑‍🤝‍🧑 Community

版本与发布记录

@dfinity/internet-identity-playwright 共发布 10 个版本,首次发布于 2024/06/24;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/02/118 个月前
3.0.0-next-2026-02-02next2026/02/029 个月前
3.0.0-next-2026-01-312026/01/319 个月前
2.0.02025/05/141.5 年前
0.0.52025/02/171.7 年前
0.0.42024/10/252 年前

关于 @dfinity/internet-identity-playwright 的常见问题

@dfinity/internet-identity-playwright 是什么?

A Playwright library to simplify the integration of Internet Identity authentication in E2E tests.

如何安装 @dfinity/internet-identity-playwright?

在项目目录运行 npm install @dfinity/internet-identity-playwright,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@playwright/test(^1.52.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dfinity/internet-identity-playwright 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dfinity/internet-identity-playwright 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dfinity/internet-identity-playwright 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dfinity/internet-identity-playwright 可以商用吗?

@dfinity/internet-identity-playwright 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dfinity/internet-identity-playwright 的下载量有多少?

根据 npm 官方下载统计,@dfinity/internet-identity-playwright 在 2026/10/12 – 2026/11/10 这 30 天内被下载 7,541 次,日均约 251 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dfinity/internet-identity-playwright 还在维护吗?

最新版本 3.0.0 发布于 2026/02/11(8 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 3 个;首次发布于 2024/06/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。