← Back to Discord Forum

If I have 10000 tests running on 1 worker do they all start new browser instance?

Hello, I have for example 10000 tests and they run on 1 worker. The tests are quite simple they just call a page and have some expects on that page. Now I was wondering could I speed the process up by using the same context and browser instance? Would such optimization make sense? Does playwright already do such optimizations ootb?

This thread is trying to answer question "Could I speed up the process of running a large number of tests on a single worker by using the same context and browser instance?"

12 replies

Why would you want to run such big amount of tests on a single worker?

That question aside, by default Playwright creates new context and page for each test. Browser is re-used unless some test fails, in that case a new worker (= new browser) is created.

Context creation takes ~20ms, page ~250 ms. You can re-use them but in that case you'll have to do clean-up yourself (like clearing cookies after each test and so on, if that matters to you).

Plus, contexts are in-memory and memory is released on context teardown. Reusing same context for such amount of tests means that you may see enormous memory consumption but not necessarily.

The real world case right now is we are building a web scraper that should scrape tousand of product detail pages from our ecom site. We want to build some statistic about the data that is visible to end customers. Where possible we are already using the api to get the information directly but we still have many pages where this is not the case

Of course we will split this work up between more machines but each machine would still handle large amount of products

And we dont care about cookies or anything like that because we are just calling the page and checking the data on that page. there is no login step or anything else required.

Right now we are evaluating what would be the best way to scrape this data and since we are using playwright for testing some of the pages already we were thinking about using that also for this use case

So if I can save one third of a second by reusing the page I think I might just do that. But I will do some benchmarks and see if its really needed or not.

Thanks for your informative answer

Check memory consumption as well if you will be reusing context, see this issue for reference https://github.com/microsoft/playwright/issues/6319

Playwright is not for webscraping 😭

Says who? 🙂

Everyone, it's for testing and not webscraping


Speed Up Your Tests with Playwright

Hey there! If you're running a large number of tests on a single worker with @playwright/test, you can definitely speed things up by reusing the same context and browser instance.

Reusable Contexts

Playwright has this cool feature called "browser contexts". It lets you create isolated browser profiles for each test. So, each test gets its own fresh browser context, free from interference from other tests.

Here's how you can create a new context:

const { chromium } = require('@playwright/test');

const browser = await chromium.launch();
const context = await browser.newContext();

Save Time with Authentication

Playwright lets you save the authentication state of the context and reuse it in all tests. You just need to log in once and then reuse that authenticated state across all your tests. This saves you from having to log in for each test.

Parallel Execution and Sharding

Playwright supports running multiple tests in parallel by default. If you have many independent tests within a single file, consider running them in parallel.

If your test suite is large enough, Playwright also supports sharding. This allows you to split your test suite into multiple parts (shards) so they can be executed simultaneously on different machines or workers.

// playwright.config.ts
import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
  // ...
  workers: 2,
  shards: 2,
export default config;

So, by using the same context and browser instance, and leveraging features like reusable contexts and parallel execution, you can make your testing process more efficient and faster. Happy testing!

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.