feat(tests): add basic key-management e2e test suite (#23477)

This commit is contained in:
Bernd Schoolmann
2026-10-15 01:07:02 +09:00
committed by GitHub
parent cfa2c3bb2c
commit 3637a20c93
28 changed files with 934 additions and 1 deletions

1
.github/CODEOWNERS vendored
View File

@@ -224,6 +224,7 @@ apps/browser/src/popup/components/extension-anon-layout-wrapper @bitwarden/team
apps/web/src/app/layouts @bitwarden/team-ui-foundation
## Key management team files ##
e2e @bitwarden/team-key-management-dev
apps/desktop/src/key-management @bitwarden/team-key-management-dev
apps/web/src/app/key-management @bitwarden/team-key-management-dev
apps/browser/src/key-management @bitwarden/team-key-management-dev

2
e2e/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
test-results/
playwright-report/

67
e2e/README.md Normal file
View File

@@ -0,0 +1,67 @@
# E2e
```
NOTE: This package is currently a key-management team internal experiment. Please do not
yet contribute to it, or place your tests here. We may in the future standardize this via
an ADR at which point other teams may use it.
```
Playwright end-to-end suites for the web, browser extension and desktop clients.
Install the browser once:
```
npx playwright install chromium
```
```
npm run test:e2e:web # webpack dev server + chromium
npm run test:e2e:browser # builds the chrome extension, loads it unpacked
npm run test:e2e:desktop # builds electron main/renderer/preload, launches the app
npm run test:e2e:all # all three, sequentially
```
Failing tests keep a video and a trace (`npx playwright show-trace <trace.zip>`).
Set `E2E_VIDEO=1` to record every test; both land in `e2e/test-results/<test>/`.
## Credentials
Accounts live in `.debug/credentials.txt` (gitignored), an INI-style file:
```
[local-web]
email=e2e-web@example.com
password=...
server=https://localhost:8080
```
Since these are on-disk credentials, only use test accounts.
`server` is the account's web vault URL, and the self-hosted base URL desktop and
browser log into. A bare host means `https://<host>`.
Every suite defaults to `local-web`, because the web client's API URLs are baked
into its build; desktop and browser point their self-hosted setting at it.
Override with `E2E_ACCOUNT=<section>`.
The web suite builds and serves the client itself when `server` is the dev server,
`https://localhost:8080`. Any other `server` must be a deployed vault:
```
E2E_ACCOUNT=usdev-e2e npm run test:e2e:web
```
## Headless
Only the web suite is headless. The other two open a window:
- Chromium's new headless mode is the only one that loads extensions, and it
restarts the extension's service worker repeatedly.
- Electron has no headless mode.
On a Linux CI runner, wrap both in a virtual display:
```
xvfb-run -a npm run test:e2e:browser
xvfb-run -a npm run test:e2e:desktop
```

15
e2e/package.json Normal file
View File

@@ -0,0 +1,15 @@
{
"name": "@bitwarden/e2e",
"version": "0.0.0",
"private": true,
"description": "Playwright end-to-end suites for the web, browser extension and desktop clients.",
"license": "GPL-3.0",
"scripts": {
"test:web": "playwright test -c playwright.web.config.ts",
"test:browser": "playwright test -c playwright.browser.config.ts",
"test:desktop": "playwright test -c playwright.desktop.config.ts"
},
"devDependencies": {
"@playwright/test": "1.59.1"
}
}

48
e2e/playwright.base.ts Normal file
View File

@@ -0,0 +1,48 @@
import { PlaywrightTestConfig } from "@playwright/test";
import { VIDEO_MODE } from "./src/artifacts";
import { IS_DEV_SERVER, WEB_VAULT_URL } from "./src/server";
/** Tests are named `.e2e.ts` so the repo's jest projects never pick them up. */
const TEST_MATCH = "**/*.e2e.ts";
const CI = !!process.env.CI;
/**
* Settings every client suite shares. A client suite adds its own `testDir`, and
* whatever it needs to get a build of that client running.
*/
export const baseConfig: PlaywrightTestConfig = {
testMatch: TEST_MATCH,
outputDir: "test-results",
fullyParallel: false,
// A logged-in vault is shared, mutable state; a retried login is fine, a parallel one is not.
workers: 1,
forbidOnly: CI,
retries: CI ? 1 : 0,
timeout: 120_000,
expect: { timeout: 15_000 },
reporter: CI ? [["github"], ["html", { open: "never" }]] : [["list"]],
use: {
trace: "retain-on-failure",
screenshot: "only-on-failure",
video: VIDEO_MODE,
},
};
const SERVE_COMMAND = "npm run build:bit:watch --workspace @bitwarden/web-vault";
const BUILD_TIMEOUT_MS = 600_000;
/** Builds and serves the web vault for suites that drive it, unless one is deployed. */
export const webVaultServer: PlaywrightTestConfig["webServer"] = IS_DEV_SERVER
? {
command: SERVE_COMMAND,
url: WEB_VAULT_URL,
cwd: "..",
ignoreHTTPSErrors: true,
reuseExistingServer: !process.env.CI,
timeout: BUILD_TIMEOUT_MS,
stdout: "ignore",
stderr: "pipe",
}
: undefined;

View File

@@ -0,0 +1,10 @@
import { defineConfig } from "@playwright/test";
import { baseConfig, webVaultServer } from "./playwright.base";
export default defineConfig({
...baseConfig,
testDir: "./tests/browser",
// Logs into the web vault's server, so it must be running too.
webServer: webVaultServer,
});

View File

@@ -0,0 +1,10 @@
import { defineConfig } from "@playwright/test";
import { baseConfig, webVaultServer } from "./playwright.base";
export default defineConfig({
...baseConfig,
testDir: "./tests/desktop",
// Logs into the web vault's server, so it must be running too.
webServer: webVaultServer,
});

View File

@@ -0,0 +1,15 @@
import { defineConfig } from "@playwright/test";
import { baseConfig, webVaultServer } from "./playwright.base";
import { IS_DEV_SERVER, WEB_VAULT_URL } from "./src/server";
export default defineConfig({
...baseConfig,
testDir: "./tests/web",
use: {
...baseConfig.use,
baseURL: WEB_VAULT_URL,
ignoreHTTPSErrors: IS_DEV_SERVER,
},
webServer: webVaultServer,
});

50
e2e/src/artifacts.ts Normal file
View File

@@ -0,0 +1,50 @@
import { rmSync } from "node:fs";
import { join } from "node:path";
import { BrowserContext, TestInfo } from "@playwright/test";
/** Records every test instead of only failing ones, e.g. `E2E_VIDEO=1`. */
const RECORD_ALL = process.env.E2E_VIDEO != null;
const VIDEO_SUBDIR = "video";
const TRACE_FILE = "trace.zip";
/** Playwright's `video` option for the contexts it creates itself. */
export const VIDEO_MODE = RECORD_ALL ? "on" : "retain-on-failure";
function failed(testInfo: TestInfo): boolean {
return testInfo.status !== testInfo.expectedStatus;
}
/**
* Where a self-launched context records. Playwright's `video`, `trace` and `screenshot`
* options only reach the contexts it creates itself, so the browser and desktop
* fixtures record and trace on their own.
*/
export function videoDir(testInfo: TestInfo): string {
return join(testInfo.outputDir, VIDEO_SUBDIR);
}
/** Drops a passing test's video, once its context closed and the file is complete. */
export function pruneVideo(testInfo: TestInfo) {
if (RECORD_ALL || failed(testInfo)) {
return;
}
rmSync(videoDir(testInfo), { recursive: true, force: true });
}
/** Screenshots and DOM snapshots per action, like Playwright's `trace` option. */
export async function startTrace(context: BrowserContext) {
await context.tracing.start({ screenshots: true, snapshots: true });
}
/** Keeps the trace of a failing test only, like `retain-on-failure`. */
export async function stopTrace(context: BrowserContext, testInfo: TestInfo) {
if (!failed(testInfo)) {
await context.tracing.stop();
return;
}
await context.tracing.stop({ path: join(testInfo.outputDir, TRACE_FILE) });
}

View File

@@ -0,0 +1,25 @@
import { BrowserContext, Page } from "@playwright/test";
import { extensionShutdown, popupUrl, reopenPopup } from "./fixtures";
// The popup header avatar carries the account as screen-reader text, e.g. "Bitwarden account a@b.c".
const CURRENT_ACCOUNT = /Bitwarden account/;
/** Locks the active account from the popup's account switcher. */
export async function lockNow(popup: Page) {
await popup.getByRole("button", { name: CURRENT_ACCOUNT }).click();
await popup.getByRole("button", { name: "Lock now" }).click();
}
/**
* Locks, then reopens the popup, as a user clicking the toolbar icon again would: locking
* closes every popup and restarts the extension to wipe it from memory.
*/
export async function lockAndReopen(context: BrowserContext, popup: Page): Promise<Page> {
const url = await popupUrl(context);
const shutdown = extensionShutdown(context);
await lockNow(popup);
await shutdown;
return reopenPopup(context, url);
}

181
e2e/src/browser/fixtures.ts Normal file
View File

@@ -0,0 +1,181 @@
import { readFileSync, rmSync } from "node:fs";
import { join, resolve } from "node:path";
import { BrowserContext, Page, chromium, expect, test as base } from "@playwright/test";
import { pruneVideo, startTrace, stopTrace, videoDir } from "../artifacts";
import { BROWSER_BUILD_DIR, E2E_STATE_DIR } from "../paths";
import { IS_DEV_SERVER } from "../server";
const PROFILE_DIR = resolve(E2E_STATE_DIR, "chrome-profile");
const EXTENSION_SCHEME = "chrome-extension://";
const SERVICE_WORKER_TIMEOUT_MS = 30_000;
const ONBOARDING_TIMEOUT_MS = 60_000;
const EXTENSIONS_PAGE = "chrome://extensions";
const DEVELOPER_MODE_TOGGLE = "#devMode";
const SKIP_DEFAULT_MANAGER_BUTTON = "#default-password-manager-prompt_button_skip-for-now";
/**
* Lock restarts the extension (chrome.runtime.reload), and Chromium turns a reloaded
* unpacked extension off unless developer mode is on. Chromium rewrites its preferences
* on start, so flip the toggle like a developer would.
*/
async function enableDeveloperMode(context: BrowserContext) {
const page = await context.newPage();
await page.goto(EXTENSIONS_PAGE);
// Playwright's CSS engine pierces the page's open shadow roots.
const toggle = page.locator(DEVELOPER_MODE_TOGGLE);
if ((await toggle.getAttribute("aria-pressed")) !== "true") {
await toggle.click();
}
await expect(toggle).toHaveAttribute("aria-pressed", "true");
await page.close();
}
async function launch(
baseURL: string | undefined,
recordVideoDir: string,
): Promise<BrowserContext> {
rmSync(PROFILE_DIR, { recursive: true, force: true });
return chromium.launchPersistentContext(PROFILE_DIR, {
channel: "chromium",
headless: false,
// The local dev server serves a self-signed certificate.
ignoreHTTPSErrors: IS_DEV_SERVER,
// Lets suites that also drive the web vault use relative URLs in this context.
baseURL,
recordVideo: { dir: recordVideoDir },
args: [
`--load-extension=${BROWSER_BUILD_DIR}`,
`--disable-extensions-except=${BROWSER_BUILD_DIR}`,
],
});
}
/** The id is assigned at load time, and only observable once the worker registers. */
async function extensionId(context: BrowserContext): Promise<string> {
const worker =
context.serviceWorkers().find((w) => w.url().startsWith(EXTENSION_SCHEME)) ??
(await context.waitForEvent("serviceworker", { timeout: SERVICE_WORKER_TIMEOUT_MS }));
return new URL(worker.url()).hostname;
}
function popupPath(): string {
const manifest = JSON.parse(readFileSync(join(BROWSER_BUILD_DIR, "manifest.json"), "utf8"));
const popup = manifest.action?.default_popup;
if (popup == null) {
throw new Error("The extension manifest declares no action popup.");
}
return popup;
}
/** URL of the extension popup, which Playwright can open as a full page. */
export async function popupUrl(context: BrowserContext): Promise<string> {
return `${EXTENSION_SCHEME}${await extensionId(context)}/${popupPath()}`;
}
const REOPEN_TIMEOUT_MS = 30_000;
const REOPEN_RETRY_MS = 100;
/**
* Resolves once the running extension instance shut down, which lock does to wipe it from
* memory (BrowserProcessReloadService). Call before locking. The next instance starts
* lazily, e.g. when the popup opens; extension pages opened earlier die with the old one.
*/
export async function extensionShutdown(context: BrowserContext): Promise<void> {
const worker =
context.serviceWorkers().find((w) => w.url().startsWith(EXTENSION_SCHEME)) ??
(await context.waitForEvent("serviceworker", { timeout: SERVICE_WORKER_TIMEOUT_MS }));
await worker.waitForEvent("close", { timeout: SERVICE_WORKER_TIMEOUT_MS });
}
/**
* Opens the popup at `url` in `page` (a new tab by default) once the extension serves it
* again. Resolve `url` with {@link popupUrl} before locking: the id lookup needs a running
* extension.
* Lock restarts the whole extension (BrowserProcessReloadService), and its pages fail to
* load meanwhile.
*/
export async function reopenPopup(
context: BrowserContext,
url: string,
page?: Page,
): Promise<Page> {
const tab = page ?? (await context.newPage());
const deadline = Date.now() + REOPEN_TIMEOUT_MS;
while (Date.now() < deadline) {
const loaded = await tab
.goto(url)
.then(() => true)
.catch(() => false);
if (loaded) {
return tab;
}
await tab.waitForTimeout(REOPEN_RETRY_MS);
}
throw new Error("The extension popup never loaded again.");
}
/**
* A fresh install walks through onboarding — the "make Bitwarden your default password
* manager" prompt, then the intro carousel — before it offers the login page.
*/
export async function reachLoginPage(page: Page) {
const email = page.getByTestId("login-email-input");
const skipDefaultPrompt = page.locator(SKIP_DEFAULT_MANAGER_BUTTON);
// Scoped to the carousel: the login page has a "Log in" button of its own.
const carouselLogIn = page
.locator("app-intro-carousel")
.getByRole("button", { name: "Log in", exact: true });
const deadline = Date.now() + ONBOARDING_TIMEOUT_MS;
while (Date.now() < deadline) {
// Bootstrapping the popup runs state migrations, so each step takes a while to appear.
await expect(email.or(skipDefaultPrompt).or(carouselLogIn).first()).toBeVisible({
timeout: ONBOARDING_TIMEOUT_MS,
});
if (await email.isVisible()) {
return;
}
await ((await skipDefaultPrompt.isVisible()) ? skipDefaultPrompt : carouselLogIn).click();
}
throw new Error("The extension popup never reached the login page.");
}
/** `popup` is the extension popup opened as a full page, which is how Playwright can drive it. */
export const test = base.extend<{ context: BrowserContext; popup: Page }>({
context: async ({ baseURL }, use, testInfo) => {
const context = await launch(baseURL, videoDir(testInfo));
await startTrace(context);
await enableDeveloperMode(context);
await use(context);
await stopTrace(context, testInfo);
await context.close();
pruneVideo(testInfo);
},
popup: async ({ context }, use) => {
const page = await context.newPage();
await page.goto(await popupUrl(context));
await reachLoginPage(page);
await use(page);
},
});
export { expect } from "@playwright/test";

86
e2e/src/credentials.ts Normal file
View File

@@ -0,0 +1,86 @@
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { DEBUG_DIR } from "./paths";
const CREDENTIALS_FILE = resolve(DEBUG_DIR, "credentials.txt");
/**
* Section of `.debug/credentials.txt` every suite defaults to.
*/
const DEFAULT_ACCOUNT = "local-web";
export type Account = {
email: string;
password: string;
/** Web vault URL, which is also the self-hosted base URL, e.g. `https://localhost:8080`. */
server: string;
};
/**
* Parses the INI-style `.debug/credentials.txt`:
*
* # comment
* [local-web]
* email=e2e-web@example.com
* password=...
* server=https://localhost:8080
*/
function parse(contents: string): Map<string, Record<string, string>> {
const sections = new Map<string, Record<string, string>>();
let current: Record<string, string> | undefined;
for (const raw of contents.split(/\r?\n/)) {
const line = raw.trim();
if (line.length === 0 || line.startsWith("#")) {
continue;
}
const header = /^\[(.+)\]$/.exec(line);
if (header) {
current = {};
sections.set(header[1], current);
continue;
}
const separator = line.indexOf("=");
if (current == null || separator < 0) {
continue;
}
current[line.slice(0, separator).trim()] = line.slice(separator + 1).trim();
}
return sections;
}
/** Reads the account named by `E2E_ACCOUNT`, falling back to the default. */
export function account(): Account {
const name = process.env.E2E_ACCOUNT ?? DEFAULT_ACCOUNT;
let contents: string;
try {
contents = readFileSync(CREDENTIALS_FILE, "utf8");
} catch {
throw new Error(`No credentials at ${CREDENTIALS_FILE}. See e2e/README.md.`);
}
const section = parse(contents).get(name);
if (section == null) {
throw new Error(`No [${name}] section in ${CREDENTIALS_FILE}.`);
}
for (const key of ["email", "password", "server"] as const) {
if (!section[key]) {
throw new Error(`[${name}] in ${CREDENTIALS_FILE} is missing ${key}.`);
}
}
return { email: section.email, password: section.password, server: vaultUrl(section.server) };
}
/** Accepts a bare host, e.g. `vault.usdev.bitwarden.pw`, as its https URL. */
function vaultUrl(server: string): string {
return /^https?:\/\//.test(server) ? server : `https://${server}`;
}

View File

@@ -0,0 +1,25 @@
import { ElectronApplication } from "@playwright/test";
/** Ids of application menu items (apps/desktop/src/main/menu). */
export const MenuItem = Object.freeze({
LockAll: "lockAllNow",
Settings: "settings",
} as const);
export type MenuItem = (typeof MenuItem)[keyof typeof MenuItem];
/**
* Clicks an application menu item, exactly as a user would. Native menus are outside
* the renderer, so Playwright cannot reach them through the page.
*/
export async function clickMenuItem(app: ElectronApplication, id: MenuItem) {
await app.evaluate(({ Menu }, itemId) => {
const item = Menu.getApplicationMenu()?.getMenuItemById(itemId);
if (item == null) {
throw new Error(`No menu item ${itemId}`);
}
if (!item.enabled) {
throw new Error(`Menu item ${itemId} is disabled`);
}
item.click();
}, id);
}

View File

@@ -0,0 +1,67 @@
import { rmSync } from "node:fs";
import { resolve } from "node:path";
import { ElectronApplication, Page, _electron, test as base } from "@playwright/test";
import { pruneVideo, startTrace, stopTrace, videoDir } from "../artifacts";
import { DESKTOP_BUILD_DIR, E2E_STATE_DIR } from "../paths";
import { IS_DEV_SERVER } from "../server";
// The main process also talks to the local dev server, and its certificate is self-signed.
const TLS_ARGS = IS_DEV_SERVER ? ["--ignore-certificate-errors"] : [];
// Keeps the suite's vault, settings and logs away from a real installation's app data.
const APPDATA_DIR = resolve(E2E_STATE_DIR, "desktop-profile");
const IPC_SOCKET_DIR = E2E_STATE_DIR;
const WINDOW_TIMEOUT_MS = 60_000;
async function launch(
extraArgs: string[],
extraEnv: Record<string, string>,
recordVideoDir: string,
): Promise<ElectronApplication> {
rmSync(APPDATA_DIR, { recursive: true, force: true });
return _electron.launch({
args: [DESKTOP_BUILD_DIR, "--no-sandbox", ...TLS_ARGS, ...extraArgs],
env: {
...process.env,
BITWARDEN_APPDATA_DIR: APPDATA_DIR,
BITWARDEN_IPC_SOCKET_DIR: IPC_SOCKET_DIR,
NODE_ENV: "development",
...extraEnv,
},
recordVideo: { dir: recordVideoDir },
});
}
export const test = base.extend<{
/** Additional Electron command line switches, e.g. `--remote-debugging-port`. */
extraArgs: string[];
/** Additional environment for the Electron process, e.g. `USE_AUTOMATION_BIOMETRICS`. */
extraEnv: Record<string, string>;
app: ElectronApplication;
window: Page;
}>({
extraArgs: [[], { option: true }],
extraEnv: [{}, { option: true }],
app: async ({ extraArgs, extraEnv }, use, testInfo) => {
const app = await launch(extraArgs, extraEnv, videoDir(testInfo));
await startTrace(app.context());
await use(app);
await stopTrace(app.context(), testInfo);
await app.close();
pruneVideo(testInfo);
},
window: async ({ app }, use) => {
const window = await app.firstWindow({ timeout: WINDOW_TIMEOUT_MS });
await window.waitForLoadState("domcontentloaded");
await use(window);
},
});
export { expect } from "@playwright/test";

30
e2e/src/lock-screen.ts Normal file
View File

@@ -0,0 +1,30 @@
import { expect, Page } from "@playwright/test";
// Host of the shared lock screen (libs/key-management-ui), whatever the unlock method.
const LOCK_SCREEN = "bit-lock";
const MASTER_PASSWORD_INPUT = 'input[name="masterPassword"]';
const UNLOCK_TIMEOUT_MS = 60_000;
export class LockScreen {
constructor(private page: Page) {}
/** Not keyed on the master password field: users without one unlock another way. */
async waitUntilShown() {
await expect(this.page.locator(LOCK_SCREEN)).toBeAttached({
timeout: UNLOCK_TIMEOUT_MS,
});
}
async unlockWithMasterPassword(password: string) {
await this.page.locator(MASTER_PASSWORD_INPUT).fill(password);
await this.page.getByRole("button", { name: "Unlock", exact: true }).click();
await this.waitUntilGone();
}
/** Unlocking navigates away, which removes the lock screen. */
async waitUntilGone() {
await expect(this.page.locator(LOCK_SCREEN)).not.toBeAttached({
timeout: UNLOCK_TIMEOUT_MS,
});
}
}

61
e2e/src/login.ts Normal file
View File

@@ -0,0 +1,61 @@
import { expect, Page } from "@playwright/test";
import { account } from "./credentials";
// Desktop and browser share a button-triggered selector that offers self-hosted. Web's
// selector is a link that only switches region, since its server is baked into the build.
const ENV_MENU_TRIGGER = 'environment-selector button[aria-haspopup="menu"]';
const SELF_HOSTED_MENU_ITEM = /self-hosted/i;
const BASE_URL_INPUT = "#self_hosted_env_settings_form_input_base_url";
/**
* Drives the login screen of any client — the email/password pages are the same
* shared components (libs/auth/src/angular/login) everywhere.
*/
export class LoginPage {
constructor(private page: Page) {}
async logIn() {
const targetAccount = account();
// Wait for the page before probing for the selector, which renders alongside it.
const email = this.page.getByTestId("login-email-input");
await expect(email).toBeVisible();
// On desktop and browser, the self-hosted server is not the default; switch to it if needed.
if ((await this.page.locator(ENV_MENU_TRIGGER).count()) > 0) {
await this.useSelfHostedServer(targetAccount.server);
}
await email.fill(targetAccount.email);
await this.page.getByTestId("login-continue-button").click();
const password = this.page.getByTestId("login-master-password-input");
await expect(password).toBeVisible();
await password.fill(targetAccount.password);
await this.page.getByTestId("login-submit-button").click();
}
/** Starts a passkey login; an authenticator must be ready to answer the assertion. */
async logInWithPasskey() {
await this.page.getByTestId("login-with-passkey-button").click();
}
/** Vault route the client lands on once the master password is accepted. */
async waitForVault(vaultUrl: RegExp) {
await this.page.waitForURL(vaultUrl, { timeout: 60_000 });
}
private async useSelfHostedServer(baseUrl: string) {
await this.page.locator(ENV_MENU_TRIGGER).click();
await this.page.getByRole("menuitem", { name: SELF_HOSTED_MENU_ITEM }).click();
const input = this.page.locator(BASE_URL_INPUT);
await expect(input).toBeVisible();
await input.fill(baseUrl);
await this.page.getByRole("button", { name: "Save", exact: true }).click();
await expect(input).toBeHidden();
}
}

12
e2e/src/paths.ts Normal file
View File

@@ -0,0 +1,12 @@
import { resolve } from "node:path";
// Playwright transpiles these files to CommonJS, so `__dirname` is what resolves here.
const E2E_DIR = resolve(__dirname, "..");
export const REPO_ROOT = resolve(E2E_DIR, "..");
export const DEBUG_DIR = resolve(REPO_ROOT, ".debug");
export const E2E_STATE_DIR = resolve(DEBUG_DIR, "e2e");
export const BROWSER_BUILD_DIR = resolve(REPO_ROOT, "apps/browser/build");
export const DESKTOP_BUILD_DIR = resolve(REPO_ROOT, "apps/desktop/build");

13
e2e/src/server.ts Normal file
View File

@@ -0,0 +1,13 @@
import { account } from "./credentials";
/** Where the dev server listens — see apps/web/config/base.json. */
const DEV_SERVER_URL = "https://localhost:8080";
/** The account's server; an account on a deployed vault skips the local build. */
export const WEB_VAULT_URL = account().server;
/**
* Only the dev server's certificate is self-signed. A deployed vault keeps TLS
* verification on, since the suite sends real credentials to it.
*/
export const IS_DEV_SERVER = new URL(WEB_VAULT_URL).origin === DEV_SERVER_URL;

View File

@@ -0,0 +1,16 @@
import { Page } from "@playwright/test";
// The menu trigger is the avatar button in the navigation, which has no accessible name.
const MENU_TRIGGER = "button:has(dynamic-avatar)";
/** Logs out through the account menu in the vault navigation. */
export async function logOut(page: Page) {
await page.locator(MENU_TRIGGER).first().click();
await page.getByRole("menuitem", { name: "Log out" }).click();
}
/** Locks through the account menu; only offered when the account has an unlock method. */
export async function lockNow(page: Page) {
await page.locator(MENU_TRIGGER).first().click();
await page.getByRole("menuitem", { name: "Lock now" }).click();
}

45
e2e/src/web/onboarding.ts Normal file
View File

@@ -0,0 +1,45 @@
import { expect, Page } from "@playwright/test";
/** Post-login route offering the browser extension, shown until it is dismissed once. */
export const SETUP_EXTENSION_URL = /#\/setup-extension/;
/** Onboarding modals that pop up over the vault after login, each dismissed with "Skip". */
const ONBOARDING_DIALOGS = [
"You're in! Welcome to Bitwarden",
"Get the extension for easy vault access",
];
const ADD_IT_LATER_BUTTON = "Add it later";
const SKIP_TO_WEB_APP_LINK = "Skip to web app";
/** Dismisses the extension pitch, which otherwise stands between login and the vault. */
export async function skipExtensionSetup(page: Page) {
if (!SETUP_EXTENSION_URL.test(page.url())) {
return;
}
// With the extension installed, the page links straight to the vault. Otherwise
// "Add it later" opens a dialog carrying that link.
const addItLater = page.getByRole("button", { name: ADD_IT_LATER_BUTTON });
const skip = page.getByRole("link", { name: SKIP_TO_WEB_APP_LINK });
await expect(addItLater.or(skip).first()).toBeVisible();
if (await addItLater.isVisible()) {
await addItLater.click();
}
await skip.click();
}
/**
* Skips onboarding modals whenever one shows up. They open asynchronously after
* login and on navigation, so a handler beats a one-off check.
*/
export async function skipOnboardingDialogs(page: Page) {
for (const heading of ONBOARDING_DIALOGS) {
const dialog = page.getByRole("dialog").filter({ hasText: heading });
await page.addLocatorHandler(dialog, async () => {
await dialog.getByRole("button", { name: "Skip" }).click();
});
}
}

View File

@@ -0,0 +1,16 @@
import { test } from "../../../src/browser/fixtures";
import { LoginPage } from "../../../src/login";
// The popup redirects to a tab route (/tabs/vault or /tabs/current) once unlocked.
const VAULT_URL = /#\/tabs\//;
test("logs into the vault with a master password", async ({ popup }) => {
// Given the user is on the login page
const login = new LoginPage(popup);
// When the user logs in with a master password
await login.logIn();
// Then the user should be redirected to the vault page
await login.waitForVault(VAULT_URL);
});

View File

@@ -0,0 +1,15 @@
import { test } from "../../../src/desktop/fixtures";
import { LoginPage } from "../../../src/login";
const VAULT_URL = /#\/vault/;
test("logs into the vault with a master password", async ({ window }) => {
// Given the user is on the login page
const login = new LoginPage(window);
// When the user logs in with a master password
await login.logIn();
// Then the user should be redirected to the vault page
await login.waitForVault(VAULT_URL);
});

View File

@@ -0,0 +1,22 @@
import { test } from "@playwright/test";
import { LoginPage } from "../../../src/login";
import { SETUP_EXTENSION_URL, skipExtensionSetup } from "../../../src/web/onboarding";
const VAULT_URL = /#\/vault/;
const POST_LOGIN_URL = new RegExp(`${VAULT_URL.source}|${SETUP_EXTENSION_URL.source}`);
const POST_LOGIN_TIMEOUT_MS = 60_000;
test("logs into the vault with a master password", async ({ page }) => {
// Given the user is on the login page
await page.goto("/");
const login = new LoginPage(page);
// When the user logs in with a master password
await login.logIn();
// Then the user should be redirected to the vault page, past the extension setup prompt
await page.waitForURL(POST_LOGIN_URL, { timeout: POST_LOGIN_TIMEOUT_MS });
await skipExtensionSetup(page);
await login.waitForVault(VAULT_URL);
});

10
e2e/tsconfig.json Normal file
View File

@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"noEmit": true,
"types": ["node"]
},
"include": ["**/*.ts"]
}

View File

@@ -765,6 +765,16 @@ export default tseslint.config(
},
},
// Playwright fixtures
{
files: ["e2e/**/*.ts"],
rules: {
// Playwright requires a destructuring pattern as the first fixture argument, so a
// fixture that depends on nothing has to be declared as `async ({}, use)`.
"no-empty-pattern": "off",
},
},
// Keep ignores at the end
{
ignores: [

76
package-lock.json generated
View File

@@ -10,6 +10,7 @@
"license": "GPL-3.0",
"workspaces": [
"apps/*",
"e2e",
"apps/desktop/desktop_native/napi",
"libs/**/*"
],
@@ -709,6 +710,14 @@
"name": "@bitwarden/web-vault",
"version": "2026.10.0"
},
"e2e": {
"name": "@bitwarden/e2e",
"version": "0.0.0",
"license": "GPL-3.0",
"devDependencies": {
"@playwright/test": "1.59.1"
}
},
"libs/admin-console": {
"name": "@bitwarden/admin-console",
"version": "0.0.0",
@@ -5107,6 +5116,10 @@
"resolved": "libs/dirt/card",
"link": true
},
"node_modules/@bitwarden/e2e": {
"resolved": "e2e",
"link": true
},
"node_modules/@bitwarden/generator-components": {
"resolved": "libs/tools/generator/components",
"link": true
@@ -15104,6 +15117,69 @@
"url": "https://opencollective.com/pkgr"
}
},
"node_modules/@playwright/test": {
"version": "1.59.1",
"resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.59.1.tgz",
"integrity": "sha512-PG6q63nQg5c9rIi4/Z5lR5IVF7yU5MqmKaPOe0HSc0O2cX1fPi96sUQu5j7eo4gKCkB2AnNGoWt7y4/Xx3Kcqg==",
"dev": true,
"license": "Apache-2.0",
"dependencies": {
"playwright": "1.59.1"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=18"
}
},
"node_modules/@playwright/test/node_modules/fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/@playwright/test/node_modules/playwright": {
"version": "1.59.1",
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.59.1.tgz",
"integrity": "sha512-C8oWjPR3F81yljW9o5OxcWzfh6avkVwDD2VYdwIGqTkl+OGFISgypqzfu7dOe4QNLL2aqcWBmI3PMtLIK233lw==",
"dev": true,
"license": "Apache-2.0",
"dependencies": {
"playwright-core": "1.59.1"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=18"
},
"optionalDependencies": {
"fsevents": "2.3.2"
}
},
"node_modules/@playwright/test/node_modules/playwright-core": {
"version": "1.59.1",
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.59.1.tgz",
"integrity": "sha512-HBV/RJg81z5BiiZ9yPzIiClYV/QMsDCKUyogwH9p3MCP6IYjUFu/MActgYAvK0oWyV9NlwM3GLBjADyWgydVyg==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"playwright-core": "cli.js"
},
"engines": {
"node": ">=18"
}
},
"node_modules/@polka/send-type": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/@polka/send-type/-/send-type-0.5.2.tgz",

View File

@@ -23,6 +23,10 @@
"test": "jest",
"test:watch": "jest --clearCache && jest --watch",
"test:watch:all": "jest --watchAll",
"test:e2e:web": "npm run test:web --workspace @bitwarden/e2e --",
"test:e2e:browser": "npm run build:prod:chrome --workspace @bitwarden/browser && npm run test:browser --workspace @bitwarden/e2e --",
"test:e2e:desktop": "npm run build-native --workspace @bitwarden/desktop && npm run build --workspace @bitwarden/desktop && npm run test:desktop --workspace @bitwarden/e2e --",
"test:e2e:all": "npm run test:e2e:web && npm run test:e2e:browser && npm run test:e2e:desktop",
"test:types": "node ./scripts/test-types.js",
"test:locales": "tsc --project ./scripts/tsconfig.json && node ./scripts/dist/test-locales.js",
"lint:dep-ownership": "tsc --project ./scripts/tsconfig.json && node ./scripts/dist/dep-ownership.js",
@@ -48,6 +52,7 @@
},
"workspaces": [
"apps/*",
"e2e",
"apps/desktop/desktop_native/napi",
"libs/**/*"
],

View File

@@ -6,6 +6,6 @@
".storybook/test-runner.ts",
"apps/browser/src/autofill/content/components/.lit-storybook/main.ts"
],
"include": ["apps/**/*", "libs/**/*", "bitwarden_license/**/*", "scripts/**/*"],
"include": ["apps/**/*", "e2e/**/*", "libs/**/*", "bitwarden_license/**/*", "scripts/**/*"],
"exclude": ["**/build", "**/dist"]
}