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?"
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).
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
Check memory consumption as well if you will be reusing context, see this issue for reference https://github.com/microsoft/playwright/issues/6319
Related Ask AI answer for "What is the benefit of using the same context and browser instance in Playwright when running multiple tests on a single worker?".
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.
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();
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.
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!
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 [email protected].