> ## Documentation Index
> Fetch the complete documentation index at: https://e2e.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Web

> Test a web app in Chromium, Firefox, or WebKit with @e2e-dev/web, pointed at your URL or at a dev server the runner starts, at several viewport sizes.

`@e2e-dev/web` runs tests in a browser against your app's URL. For
[routes](https://e2e.com/docs/reference/web#route), [cookies](https://e2e.com/docs/reference/web#setcookies),
[dialogs](https://e2e.com/docs/reference/web#ondialog), and
[downloads](https://e2e.com/docs/reference/web#waitfordownload), see the
[web engine reference](https://e2e.com/docs/reference/web).

## Starting your app

Set your app's URL in the target's `app`. `app.open('/path')` resolves
against it:

```ts title="Target fragment"
engine: web(),
app: { url: process.env.APP_URL ?? 'http://localhost:3000' },
```

A URL without a scheme uses HTTPS, or HTTP for a loopback host. Plain HTTP
works only on a loopback host. A `file:` URL opens
[one file from disk](https://e2e.com/docs/reference/config#a-file-as-the-app), with no server.

### Let the runner start it

Add `app.command` and e2e starts your dev server, waits until `app.url`
responds, and shuts it down when the run ends (even on failures or Ctrl-C):

```ts title="e2e.config.ts"

export default {
  targets: [{
    engine: web(),
    app: {
      url: process.env.APP_URL ?? 'http://localhost:3000',
      command: {
        executable: 'npm',
        args: ['run', 'dev'],
        reuseExisting: true,
        log: '.e2e/logs/app.log',
      },
    },
  }],
} satisfies E2EConfig;
```

```text
 ✓ target "web" command at http://localhost:3000/ ready 1.20s
```

The command runs directly without a shell (`executable` is resolved from
`PATH`, and `args` pass as-is). The child process only receives `PATH`,
`HOME`, temp-directory variables, `inheritEnv`, and `env`.

| Option | Purpose |
| --- | --- |
| `reuseExisting: true` | Reuse a server that's already running (for example, one started by an [MCP session](https://e2e.com/docs/reference/mcp#running-tests-beside-a-session)). Ignored in CI, where an already-running server fails the run. |
| `log` | Save the app's stdout and stderr to a project-relative file. If the server fails to start or crashes mid-run, e2e prints the relevant lines from this log (`env` and `inheritEnv` values of 4+ characters are redacted in the quote). The log file itself isn't redacted. |
| `env` | Environment variables for the app, or a function that returns them from the config's [services](https://e2e.com/docs/services). |
| `inheritEnv` | Host variables to pass through when set, by name or prefix: `['DATABASE_URL', 'STRIPE_*']`. `env` wins over them. |
| `startupTimeout` | How long to wait for the app to respond. Default 60 seconds. |
| `readyUrl` | Set beside `command` (not inside it) to probe a health endpoint instead of `app.url`. |

See the [command reference](https://e2e.com/docs/reference/config#command) for all options and
log truncation details.

<Note>
To start a database, an API mock, or another dependency before the app, add
it as a [service](https://e2e.com/docs/services).
</Note>

### Let the runner pick the port

Use port `0` to run multiple checkouts without port conflicts. e2e picks an
open port and substitutes `{port}` in `args` and `env`:

```ts title="Target fragment"
engine: web(),
app: {
  url: 'http://127.0.0.1:0',
  command: { executable: 'pnpm', args: ['dev', '--port', '{port}'], env: { PORT: '{port}' } },
},
```

- Write `127.0.0.1` or `[::1]`, the address the command binds.
  `localhost:0` is rejected.
- Tests read the URL with the real port from `app.baseUrl`.
- Cache entries stay valid when the port changes.
- `reuseExisting` doesn't work with port `0`.
- A Next.js dev server needs an `allowedDevOrigins` entry for the host. See
  [Next.js](https://e2e.com/docs/frameworks/nextjs#fix-next-js-problems).

See [free ports](https://e2e.com/docs/reference/config#free-ports).

## Protected previews

Use `headers` to open a protected preview or skip a tunnel's warning page.
For example, a Vercel preview takes a protection bypass header:

```ts title="Target fragment"
engine: web({
  headers: {
    'x-vercel-protection-bypass': process.env.VERCEL_AUTOMATION_BYPASS_SECRET ?? '',
  },
}),
app: {
  url: process.env.PREVIEW_URL ?? 'http://localhost:3000',
  identity: 'storefront',
},
```

The browser sends `headers` only on requests that start at `app.url`'s
origin, and keeps them on redirects to any origin. To include another
origin, like an API host behind the same gate, list it in
`credentialOrigins` (see
[request credentials](https://e2e.com/docs/reference/security#request-credentials)). For ngrok,
use `'ngrok-skip-browser-warning': '1'`. Setting `app.identity` keeps replay
cache keys stable when preview URLs change.

<Note>
Setting `headers` disables the browser's HTTP cache and blocks service
workers.
</Note>

For HTTP basic auth, use `basicAuth`. It answers a `401` from `app.url` and
any origin in `credentialOrigins` (on WebKit, from any origin if one of them
uses a default port or an IPv6 host). Declare the password under `secrets`
and pass `secrets.get(name)` so e2e keeps it out of reports, logs, and model
input:

```ts title="e2e.config.ts"

export default {
  targets: [
    {
      engine: web({
        basicAuth: { username: 'preview', password: secrets.get('previewPassword') },
      }),
      app: { url: 'https://staging.example.com' },
    },
  ],
  secrets: { previewPassword: () => process.env.PREVIEW_PASSWORD ?? '' },
} satisfies E2EConfig;
```

Plain string passwords also work, though only `secrets.get()` passwords are
redacted.

## Viewports

Set a viewport on the target to run a suite at that size, or call
`browser.setViewport()` during a test. The agent resizes with its
[`resize_viewport`](https://e2e.com/docs/reference/agent#action-tools) tool.

### One size per target

`viewport` sets the initial size of each attempt, in pixels. The default is
1280 by 720. Add one target for each size:

```ts title="e2e.config.ts"

const app = { url: 'http://localhost:3000' };

export default {
  targets: [
    { name: 'desktop', engine: web(), app },
    { name: 'tablet', engine: web({ viewport: { width: 820, height: 1180 } }), app },
    { name: 'phone', engine: web({ browser: 'webkit', viewport: { width: 390, height: 844 } }), app },
  ],
} satisfies E2EConfig;
```

Every test runs on every target, with a separate result per target name.
Every retry starts at the target's size.

- Run one size with `npx e2e run --target phone`. List the test-target
  pairs with `npx e2e list`.
- Targets that declare the same `app.command` share one dev server.
- A device target can sit in the same list. See the
  [Mobile engine reference](https://e2e.com/docs/reference/mobile).

### High-density screens

`deviceScaleFactor` sets how many device pixels draw one CSS pixel. At 2 the
page renders as on a Retina display, and saved screenshots are twice the
viewport's size on each side:

```ts title="Target fragment"
{ name: 'retina', engine: web({ deviceScaleFactor: 2 }), app }
```

The factor does not change the image the model reads or the one
`toHaveScreenshot` compares.

### Fill the window

`viewport: null` sets no size, so the page fills the browser window, and the
agent, screenshots, and videos use that size. Use it in a headed run, or
with a hosted browser whose live view would show a 1280 by 720 page in one
corner:

```ts title="Target fragment"
{ name: 'window', engine: web({ viewport: null }), app }
```

A local headed launch opens a small window. A hosted browser uses the
window size you requested from the service. `browser.setViewport` still
works.

### Resize inside a test

`browser.setViewport` resizes the page for the rest of the attempt, also
after `app.restart()` and `app.clearState()`. Use it when one test checks a
layout before and after a resize:

```ts

test('the menu collapses on a narrow screen', async ({ app, screen, browser }) => {
  await app.open('/');
  await expect(screen.getByRole('navigation')).toBeVisible();

  await browser.setViewport({ width: 390, height: 844 });
  await expect(screen.getByRole('button', 'Menu')).toBeVisible();
  await expect(screen.getByRole('navigation')).toBeHidden();
});
```

Call it before `app.open` to load the first page at that size, with no
desktop render first.

### What the agent sees

The agent receives the current viewport size with each observation, so
resize before calling `agent.act`. Screenshots capture the visible viewport,
while off-screen elements can still appear in the accessibility tree (taps
automatically scroll them into view, and the agent can scroll to trigger
lazy loading).

Styled checkboxes and radios whose native input is visually hidden under a
label still show up for the agent (and tapping them clicks the label),
unless that label also wraps a link or another control. For locators, check
those with `toBeChecked()` instead of `toBeVisible()`.

### Scoping a test to one size

`platforms` can't select a viewport, because every browser target has
platform `web`. To skip a test at some sizes, read the width in
the test:

```ts
test('the sidebar stays open', async ({ app, screen, browser }) => {
  await app.open('/');
  test.skip((await browser.evaluate(() => window.innerWidth)) < 768, 'desktop layout only');
  await expect(screen.getByRole('complementary')).toBeVisible();
});
```

To keep a group of size-specific tests apart, give them their own config
and run it with `--config`.

### Limits

A narrow viewport tests responsive layout only. The browser is still a
desktop browser: scale factor 1, desktop pointer, and its usual user agent.
It doesn't emulate touch. To test on a simulator or emulator, use the
[mobile engine](https://e2e.com/docs/mobile). For dark mode and reduced motion, see
[media emulation](https://e2e.com/docs/reference/web#media-emulation).

A video keeps the size the page had when recording started. Set
`web({ screencast: { size } })` for one fixed size. A browser provider that
records, such as [Kernel](https://e2e.com/docs/integrations/kernel#recordings), records the
whole screen instead.

## Granting browser permissions

Headless Chromium refuses a permission the target doesn't grant, so
`navigator.clipboard.writeText` fails even after a click. To let
the app use the clipboard, grant it on the target:

```ts
{
  engine: web(),
  app: {
    url: 'http://localhost:3000',
    permissions: { 'clipboard-read': 'grant', 'clipboard-write': 'grant' },
  },
}
```

- Permissions apply to every page and origin for that target.
- Web targets only accept `'grant'` (leave a permission out instead of
  passing `'deny'` or `'reset'`).
- Permission names follow Playwright's list and vary by browser. See
  [`permissions`](https://e2e.com/docs/reference/config#permissions).
- `permissions: 'all'` grants every permission that the target browser
  supports.

## Next

<CardGroup cols={2}>
  <Card title="Web engine reference" icon="masks-theater" href="/reference/web">
    Every `web()` option and every `browser` method: routes, cookies, dialogs, and downloads.
  </Card>
  <Card title="Hosted browsers" icon="globe" href="/reference/web#hosted-browsers">
    Lease browsers from a service instead of launching them locally.
  </Card>
</CardGroup>
