Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the Playwright API that matches what “load a local resource” means: use page.setContent() for HTML held in a string, page.addScriptTag() or page.addStyleTag() for a local JavaScript or CSS file, route.fulfill({ path }) to answer a browser request with a file from disk, or a local web server to test an application through normal URLs. For a file-upload test, use locator.setInputFiles() instead; that selects a file in a file input rather than loading an asset into the page.

The key distinction is whether the test needs ordinary browser URL and network behavior. Injected markup and files are useful for focused fixtures; a local server is usually the better fit when testing how the application works as a website.

Choose the right way to load the resource

What you need Playwright approach Best fit
HTML markup held in memory page.setContent(html) A small, isolated page or fixture.
A local JavaScript or CSS file page.addScriptTag({ path }) or page.addStyleTag({ path }) Adding one file to a page for a focused test.
A browser request should return a file from disk page.route() or context.route() with route.fulfill({ path }) Serving a fixture for a URL requested by the page.
A complete local web app Start a local server; configure Playwright Test’s webServer and baseURL Testing ordinary navigation and application behavior.
A user selects a file to upload locator.setInputFiles() Testing a file input, not loading a page asset.

The file-path APIs and route.fulfill({ path }) resolve relative paths from the process’s current working directory. That is not necessarily the directory containing the test file. Use a deliberate absolute path, or build one from the project or test-file location if that is your intended base.

Set the page to HTML held in a string

For a self-contained HTML fixture, pass the markup to page.setContent(). It is not a file-reading API: read or construct the markup in your test, then provide the resulting string.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
  • Easily store and access 2TB to content on the go with the Seagate Portable Drive, a USB external hard drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.
import { test, expect } from '@playwright/test';

 test('renders an in-memory HTML fixture', async ({ page }) => {
  const html = `
    <!doctype html>
    <html>
      <head><title>Fixture</title></head>
      <body><main><h1>Local fixture</h1></main></body>
    </html>
  `;

  await page.setContent(html);
  await expect(page.getByRole('heading', { name: 'Local fixture' })).toBeVisible();
});

page.setContent() internally calls document.write(), so it inherits that method’s characteristics and behaviors. Use it when the test is about markup itself, not as an assumption that the browser has navigated to a real page URL or loaded an app as it would from a server.

Inject a local JavaScript or CSS file

Use page.addScriptTag({ path }) to add a JavaScript file and page.addStyleTag({ path }) to add a stylesheet. Both APIs accept filesystem paths; relative paths are resolved from the current working directory.

import { test, expect } from '@playwright/test';

 test('adds local script and stylesheet', async ({ page }) => {
  await page.setContent('<!doctype html><html><head></head><body><h1>Fixture</h1></body></html>');

  await page.addStyleTag({ path: 'tests/fixtures/site.css' });
  await page.addScriptTag({ path: 'tests/fixtures/site.js' });

  await expect(page.locator('body')).toHaveCSS('color', 'rgb(0, 0, 0)');
});

Replace the fixture paths and assertion with files and behavior in your project. If the code must run before scripts on the page, consider page.addInitScript({ path }) instead of adding a script after the page has been set up. Choose the injection point based on what the test is proving: adding a script later does not demonstrate that it was present before the page’s own scripts ran.

Rank #2
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
  • Easily store and access 5TB of content on the go with the Seagate portable drive, a USB external hard Drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

Serve a local file as a browser response

If the application requests a URL such as /fixture.json and the test should supply that request from disk, register a route and fulfill it with a local file. Playwright supports page-level and context-level routing. The content type is inferred from the file extension.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import { test, expect } from '@playwright/test';

 test('serves a fixture for a request', async ({ page }) => {
  await page.route('**/fixture.json', route =>
    route.fulfill({ path: 'tests/fixtures/fixture.json' })
  );

  await page.goto('http://127.0.0.1:4173/');
  await expect(page.getByText('Fixture loaded')).toBeVisible();
});

This example assumes the application is available at the shown local address and makes a request matching **/fixture.json. Change the route pattern, file path, and navigation URL to suit your test. If the fixture is requested by multiple pages in a context, use context-level routing; if it only belongs to one page, page-level routing keeps the interception narrower.

Routing is request handling, not a general-purpose way to expose a directory as a website. Register a route for the requests the test should control. Also account for a documented side effect: enabling routing disables HTTP cache, so a routed test is not suitable as-is for measuring or verifying HTTP caching behavior.

Rank #3
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
  • Easily store and access 1TB to content on the go with the Seagate Portable Drive, a USB external hard drive.Specific uses: Personal
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop. Reformatting may be required for Mac
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

Use a local server for a whole application

When the test should exercise your app as a website, start its development server and navigate normally. Playwright Test’s webServer setting can start the server for the test run, and baseURL lets a relative navigation such as page.goto('/settings') resolve against the configured origin.

// playwright.config.js
import { defineConfig } from '@playwright/test';

export default defineConfig({
  use: {
    baseURL: 'http://127.0.0.1:4173',
  },
  webServer: {
    command: 'npm run dev -- --host 127.0.0.1',
    url: 'http://127.0.0.1:4173',
    reuseExistingServer: !process.env.CI,
  },
});
// tests/settings.spec.js
import { test, expect } from '@playwright/test';

test('opens settings through the local app', async ({ page }) => {
  await page.goto('/settings');
  await expect(page.getByRole('heading', { name: 'Settings' })).toBeVisible();
});

Set command to the command your project uses to run its server and set url and baseURL to the address it actually serves. This example assumes a server on port 4173; that port is an example, not a Playwright requirement. The webServer URL gives Playwright Test a server address to wait for, while baseURL is the origin used to resolve relative navigations.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A server-based test preserves ordinary URL navigation and gives your app the environment to request its own scripts, styles, images, and data. It is generally a better match than injecting markup when your question concerns routing, server responses, or how the app works when loaded normally.

Rank #4
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue
  • Easily store and access 4TB of content on the go with the Seagate Portable Drive, a USB external hard drive.Specific uses: Personal
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

Test file uploads separately

To simulate selecting a file in a file input, target that input with setInputFiles(). This is an upload interaction, not a method for loading a local stylesheet or other page asset.

import { test, expect } from '@playwright/test';

test('selects a file for upload', async ({ page }) => {
  await page.goto('http://127.0.0.1:4173/upload');
  await page.locator('input[type="file"]').setInputFiles('tests/fixtures/example.pdf');
  await expect(page.getByText('example.pdf')).toBeVisible();
});

The API also supports passing file data in memory instead of a path. Use that when the test needs to define the file contents itself rather than rely on a fixture on disk.

Wait for the state your test actually needs

page.goto() waits for the page’s load event. That event covers dependent stylesheets, scripts, iframes, and images, but modern pages can fetch or load resources lazily afterward. A completed navigation therefore does not prove that every resource or delayed UI state your test cares about is ready.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Sale
UnionSine 500GB Ultra Slim Portable External Hard Drive HDD-USB 3.0
  • [Upgraded Version] - This external hard drive features a mirrored logo stripe combined with a striped anti-slip design, and the rounded corners of the casing make it easier to grip. The stripes also have a heat dissipation function, ensuring stable and fast data transfer.
  • 【Ultra-thin and quiet】 - The motherboard adopts JMicron 578 noise-free solution, giving you a quiet working environment. Lightweight and portable size designed to fit in your pocket for easy portability.
  • 【Ultra-Fast Data Transfers】 - Pairing this external hard drive with JMicron 578 solution USB 3.0 and USB 2.0 interfaces enables blazing-fast data transfer. It boasts theoretical read speeds of up to 125MB/s and write speeds of up to 103MB/s.
  • 【Plug and Play】 - With no software to install, just plug it in and the drive is ready to use.The hard disk chip is wrapped with an aluminum anti-interference layer to increase heat dissipation and protect data.
  • 【What You Get】 - 1 x Portable Hard Drive, 1 x USB 3.0 Cable, 1 x User Manual, Gift-type shell packaging ,Three-year manufacturer's warranty and free technical support services.

Wait for an observable condition tied to the test: a particular element becoming visible, expected text appearing, or a specific result being rendered. Prefer that targeted assertion to adding a generic delay, which can slow tests without establishing that the required state has been reached.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common failures

  • “File not found” for a path that exists beside the test: the path may be resolved from the process current working directory, not the test file’s directory. Use an absolute path or construct the intended path explicitly.
  • The page loads but a fixture request is not intercepted: check that the route pattern matches the actual requested URL and that the route is registered before the page makes that request. Confirm that you are routing at the page or context level used by the request.
  • The response has an unexpected content type: route.fulfill({ path }) infers content type from the file extension. Check that the fixture has the expected extension; the documented behavior does not establish that an arbitrary extension will receive the type your app requires.
  • The test says navigation finished, but content is missing: the page may load that content lazily after the load event. Wait for the specific element or result required by the test.
  • A cache test behaves differently after adding a route: Playwright routing disables HTTP cache. Do not use a routed test as evidence of normal cache behavior.
  • Upload code does not load a page resource: setInputFiles() selects files for an input[type=file]. To provide a response to a page request, use routing; to add a script or stylesheet, use the injection APIs.
  • Direct file:// navigation is uncertain: the documented guidance covered here establishes that goto takes a URL with a scheme, but does not establish comparable local-file behavior across browser engines for loading web assets. If the test depends on how a local website loads resources, prefer a local server rather than assuming file-URL behavior.

Or skip the browser setup

If your goal is a screenshot of a publicly reachable page rather than testing files on your machine, ScreenshotNeo can return an image or PDF through one GET request. This does not load your local files or replace a Playwright test. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed; an MCP server lets AI agents take screenshots; and 1,000 screenshots a month are free with no card, with paid plans starting at $5 for 3,000.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. To try it, sign up for 1,000 free screenshots a month with no card.

Which method should you choose?

Use setContent() when the input is markup in memory, inject a local script or stylesheet when a fixture needs one extra file, route a request when the page should receive a disk-backed response, and use a local server when the test is meant to exercise the app as a website. Keep file selection with setInputFiles() for upload tests. The most reliable choice is the one that matches the browser behavior the test is intended to verify.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Do relative Playwright file paths start from the test file?

Not by default for the documented path options: relative paths resolve from the process current working directory. Use an explicit absolute path or construct a path from the test-file location when needed.

Can I use Playwright to upload a file created in memory?

Yes. locator.setInputFiles() accepts file data as well as paths, so an upload test need not depend on a fixture file on disk.

Does a successful page.goto() mean every resource has loaded?

It waits for the load event, but resources may be loaded lazily after that. Assert the specific content or state the test requires.

Quick Recap

SaleBestseller No. 1
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$129.99
Bestseller No. 2
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$180.19
Bestseller No. 3
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$119.80
Bestseller No. 4
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$189.90

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.