Chrome DevTools for coding agents
test: make CLI e2e tests faster and run them in parallel (#3170)
Alex Rudenkoe9000381f414
diff --git a/tests/e2e/chrome-devtools-commands.test.ts b/tests/e2e/chrome-devtools-commands.test.tsindex d4698a4c..d7a135a6 100644--- a/tests/e2e/chrome-devtools-commands.test.ts+++ b/tests/e2e/chrome-devtools-commands.test.ts@@ -5,31 +5,22 @@*/import assert from 'node:assert';-import crypto from 'node:crypto';import fs from 'node:fs/promises';import path from 'node:path';-import {describe, it, afterEach, beforeEach} from 'node:test';+import {describe, it} from 'node:test';import {pathToFileURL} from 'node:url';import {assertDaemonIsRunning,+ createCliSession,createTempDir,runCli,- stopDaemonForCleanup,} from '../utils.js';-describe('chrome-devtools', () => {- let sessionId: string;-- beforeEach(() => {- sessionId = crypto.randomUUID();- });-- afterEach(async () => {- await stopDaemonForCleanup(sessionId);- });-- it('can invoke list_pages', async () => {+// Each test uses its own daemon session, so the tests run in parallel.+describe('chrome-devtools', {concurrency: true}, () => {+ it('can invoke list_pages', async t => {+ const sessionId = createCliSession(t);const startResult = await runCli(['start'], sessionId);assert.strictEqual(startResult.status,@@ -51,7 +42,8 @@ describe('chrome-devtools', () => {await assertDaemonIsRunning(sessionId);});- it('can take screenshot', async () => {+ it('can take screenshot', async t => {+ const sessionId = createCliSession(t);const startResult = await runCli(['start'], sessionId);assert.strictEqual(startResult.status,@@ -71,7 +63,8 @@ describe('chrome-devtools', () => {);});- it('can evaluate inline and local JavaScript', async () => {+ it('can evaluate inline and local JavaScript', async t => {+ const sessionId = createCliSession(t);using rootDirectory = createTempDir('evaluate-script-cli-');const daemonDirectory = path.join(rootDirectory.path, 'daemon');const clientDirectory = path.join(rootDirectory.path, 'client');diff --git a/tests/e2e/chrome-devtools-disabled-tools.test.ts b/tests/e2e/chrome-devtools-disabled-tools.test.tsindex 0f47d264..25afc092 100644--- a/tests/e2e/chrome-devtools-disabled-tools.test.ts+++ b/tests/e2e/chrome-devtools-disabled-tools.test.ts@@ -5,23 +5,14 @@*/import assert from 'node:assert';-import crypto from 'node:crypto';-import {describe, it, afterEach, beforeEach} from 'node:test';+import {describe, it} from 'node:test';-import {runCli, stopDaemonForCleanup} from '../utils.js';+import {createCliSession, runCli} from '../utils.js';-describe('chrome-devtools', () => {- let sessionId: string;-- beforeEach(() => {- sessionId = crypto.randomUUID();- });-- afterEach(async () => {- await stopDaemonForCleanup(sessionId);- });-- it('fails to invoke list_network_requests when categoryNetwork is disabled', async () => {+// Each test uses its own daemon session, so the tests run in parallel.+describe('chrome-devtools', {concurrency: true}, () => {+ it('fails to invoke list_network_requests when categoryNetwork is disabled', async t => {+ const sessionId = createCliSession(t);await runCli(['start', '--categoryNetwork=false'], sessionId);const result = await runCli(['list_network_requests', '1'], sessionId);@@ -39,7 +30,8 @@ describe('chrome-devtools', () => {);});- it('fails to invoke click_at when experimentalVision is disabled (default)', async () => {+ it('fails to invoke click_at when experimentalVision is disabled (default)', async t => {+ const sessionId = createCliSession(t);await runCli(['start'], sessionId);const result = await runCli(['click_at', '1', '100', '100'], sessionId);@@ -56,7 +48,8 @@ describe('chrome-devtools', () => {);});- it('fails to invoke evaluate_script when javascriptEvaluation is disabled', async () => {+ it('fails to invoke evaluate_script when javascriptEvaluation is disabled', async t => {+ const sessionId = createCliSession(t);await runCli(['start', '--no-javascript-evaluation'], sessionId);const result = await runCli(['evaluate_script', '() => 1'], sessionId);diff --git a/tests/e2e/chrome-devtools-start-stop.test.ts b/tests/e2e/chrome-devtools-start-stop.test.tsindex 100118f8..a0634521 100644--- a/tests/e2e/chrome-devtools-start-stop.test.ts+++ b/tests/e2e/chrome-devtools-start-stop.test.ts@@ -5,31 +5,22 @@*/import assert from 'node:assert';-import crypto from 'node:crypto';import path from 'node:path';-import {describe, it, afterEach, beforeEach} from 'node:test';+import {describe, it} from 'node:test';import {assertDaemonIsNotRunning,assertDaemonIsRunning,+ createCliSession,createTempDir,createTempFile,runCli,- stopDaemonForCleanup,} from '../utils.js';-describe('chrome-devtools', () => {- let sessionId: string;-- beforeEach(() => {- sessionId = crypto.randomUUID();- });-- afterEach(async () => {- await stopDaemonForCleanup(sessionId);- });-- it('can start and stop the daemon', async () => {+// Each test uses its own daemon session, so the tests run in parallel.+describe('chrome-devtools', {concurrency: true}, () => {+ it('can start and stop the daemon', async t => {+ const sessionId = createCliSession(t);await assertDaemonIsNotRunning(sessionId);const startResult = await runCli(['start'], sessionId);@@ -51,7 +42,8 @@ describe('chrome-devtools', () => {await assertDaemonIsNotRunning(sessionId);});- it('can start the daemon with userDataDir', async () => {+ it('can start the daemon with userDataDir', async t => {+ const sessionId = createCliSession(t);using userDataDir = createTempDir('chrome-devtools-test-');const startResult = await runCli(@@ -73,7 +65,8 @@ describe('chrome-devtools', () => {await assertDaemonIsRunning(sessionId);});- it('forwards an explicit headless=false option', async () => {+ it('forwards an explicit headless=false option', async t => {+ const sessionId = createCliSession(t);const startResult = await runCli(['start', '--headless=false'], sessionId);assert.strictEqual(startResult.status,@@ -89,7 +82,8 @@ describe('chrome-devtools', () => {);});- it('can start the daemon with a workspace', async () => {+ it('can start the daemon with a workspace', async t => {+ const sessionId = createCliSession(t);using workspace = createTempDir('chrome-devtools-workspace-');const startResult = await runCli(@@ -111,7 +105,8 @@ describe('chrome-devtools', () => {);});- it('can start the daemon with a config file', async () => {+ it('can start the daemon with a config file', async t => {+ const sessionId = createCliSession(t);using userDataDir = createTempDir('chrome-devtools-config-profile-');using configFile = createTempFile(JSON.stringify({diff --git a/tests/utils.ts b/tests/utils.tsindex 47a4f591..70a04a34 100644--- a/tests/utils.ts+++ b/tests/utils.ts@@ -6,9 +6,11 @@import assert from 'node:assert';import {spawn, type ChildProcess} from 'node:child_process';+import crypto from 'node:crypto';import fs from 'node:fs';import os from 'node:os';import path from 'node:path';+import type {TestContext} from 'node:test';import type {Browser} from 'puppeteer';import puppeteer, {Locator} from 'puppeteer';@@ -483,6 +485,18 @@ export async function stopDaemonForCleanup(sessionId: string) {assert.ok(!isDaemonRunning(sessionId), 'daemon is still running');}+/**+ * Creates a new CLI session ID for one test and stops its daemon after the+ * test. Each test gets its own session, so tests can run concurrently.+ */+export function createCliSession(t: TestContext): string {+ const sessionId = crypto.randomUUID();+ t.after(async () => {+ await stopDaemonForCleanup(sessionId);+ });+ return sessionId;+}+export async function assertDaemonIsRunning(sessionId?: string) {const result = await runCli(['status'], sessionId);assert.ok(