Playwright Test Fixtures, Browser Context and Page

Share this video with your friends

Social Share Links

Send Tweet

In this lesson we look at the Playwright Test concept of Fixtures. We demo the creation of various fixtures and explain the idea behind browser, context and page.

Instructor: [0:00] It is useful to be aware of the concept of fixtures when working with Playwright tests. These are the objects that you destructure from the argument that is passed to your test function. Right now, we are not destructuring anything from the past in argument.

[0:14] Therefore, Playwright is smart enough not to instantiate any of those values. For example, because we're not taking a page, a page will not be created. If you run this test as it stands right now, you can see that it is pretty darn fast, taking only a few milliseconds.

[0:30] This is great news because it means that you can use Playwright tests for testing things that are not browser-based as well, and they will run pretty fast. Looking at some of the built-in fixtures that are provided, we've already seen page a number of times.

[0:43] As fixtures go, this is probably one of the most expensive ones to create. If I run it a few times, you can see that it takes around half a second on my machine. Another fixture that you can use is the browser name.

[0:55] Of course, if you're only going to be using the browser name, it's not going to actually launch the browser for you. This is going to run pretty darn fast as well. Now, the next thing that I want to demo is the layer that actually gets you to a page.

[1:07] When we ask for a page, that essentially first creates a browser. Now, this is not particularly expensive to create, as you can see, from my protector machine, it's done in under 100 milliseconds.

[1:18] The browser will be something like Chromium, or Firefox or WebKit. The next thing that playwright will have to create is what is known as a context.

[1:25] You can think of a context as an isolated browser window. This is going to be a bit slower than creating the browser because for this, Playwright will first have to launch the browser and then create the context.

[1:36] This brings us to using the page. When we request a page, Playwright will first launch the browser, then create a context and then create a new page and you can think of a page as a tab within a particular browser window.

[1:48] Now, it might seem like a big jump in time to execute going from a context to a page, but the good news is that, as we create more and more tests, it becomes cheaper and cheaper to create new pages.

[1:59] Right now, the first page is taking around 350 milliseconds, and the second page is taking only around 130 milliseconds on my machine.