Parallel Test Execution | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Advanced Tricks and Techniques
Instructor: Artem Bondar
Lesson Summary
Parallel Execution in PlayWrite In this lesson, we explore how PlayWrite handles parallel execution of tests. By default, PlayWrite runs spec files in parallel, creating a new instance of the web browser, known as a Worker , for each spec file. Key Concepts Workers: Each spec file is executed in a separate Worker. For example, with five spec files, PlayWrite creates five Workers to run tests in parallel. Fully Parallel: This setting determines if tests within a single spec file run in parallel. If set to true , tests run concurrently; if false , they run sequentially. Configuration Settings Two main settings in PlayWriteConfig.ts are: Workers: Configured to control the number of Workers. Setting it to 1 disables parallel execution. Fully Parallel: Set to true to enable parallel execution within a spec file. Execution Examples When executing tests: With five Workers and fully parallel set to true , tests completed in 10.3 seconds. With one Worker, tests took 29 seconds, running sequentially. Spec Level Configuration You can configure parallel execution at the spec level: Use test.describe.parallel to run specific tests in parallel. Use test.describe.configure({ mode: 'serial' }) for tests that depend on each other. In summary, PlayWrite allows flexible configurations for parallel execution, enhancing test efficiency while providing control over execution order and dependencies.
Video Transcript
In this lesson, we will talk about Parallel Execution. PlayWrite, by default, run your spec files in Parallel, but if you want, you can run your test files within the spec also in Parallel. And if needed, you can run your test sequentially. So all those different settings, you can configure in the PlayWrite. And in this lesson, we will talk about it. So let's get into it. So previously, I already mentioned that Worker is the new instance of the web browser. And by default, PlayWrite creates a Worker for each spec file and executes them in Parallel. So for example, currently we have five spec files in our test project, auto-weighting, drag and drop, and so on. And if we run the test right now, PlayWrite will create five Workers and will execute five Parallel threads or five web browsers in Parallel. And this is maximum default value, at least for me. If I would have more than five spec files, PlayWrite would not run more than five Workers by default. And inside of each Worker, tests will be executed sequentially, one by one. So let me quickly demonstrate this. We are looking into the PlayWriteConfig.ts and main two settings we are looking for in this one is Workers. Right now, it's configured as if we want to run in CI, we want to use just one Worker. If we're running on locally, it's undefined. So PlayWrite will decide by himself how many Workers to trigger. And technically he will run one new Worker per spec file. So in our example, PlayWrite should create five Workers if we try to run all those tests. And the second parameter is this, fully parallel, which can be either true or false. Fully parallel means that do we want to run tests in parallel within the single spec file? When fully parallel is false, it means the test within the spec file will be executed one by one. If true, tests will be executed in parallel as well. And each test will be executed in the independent Worker. This is how it works architecturally and this is how PlayWrite behave. So let me run some demo for you. We need to prepare a little bit our existing test. So I want to skip temporarily auto-weighting spec.ts because these two steps are pretty slow and I want them to skip from this demo. And I put this one test is skip and this one dot skip as well. So we will not run them. And also we'll modify this test a little bit. I will remove this delay for typing into the input field as well to not creating a bottleneck of this particular test and to better execute this demo. And I guess that's it. Right now we have this configuration. Workers is undefined. So PlayWrite will create five workers for each of the spec file and execute in five threads in other words. And fully parallel means that all tests will be executed sequentially within single spec file. npx PlayWrite test project Chromium So you can see the PlayWrite right now running five workers 22 tests total and each worker for spec file. And all tests passed within 13 seconds. Now let me enable fully parallel true. So in this case, PlayWrite will run tests PlayWrite will run tests also in five workers but within each worker, tests will be executed in parallel as well within each spec file. Let's run this test one more time. The same five workers. But right now execution should be a little bit faster. And we can see the result 10.3 seconds. So a little bit faster. Our tests overall are pretty quick so we don't have a significant increase in the performance in this case. If you want to completely disable a parallel execution you can reduce the number of workers for the execution. So here for the workers instead of undefined we can put one. In this case, PlayWrite will use just a single worker and will execute all tests including the specs files just sequentially. So let me show you that as well. So right now time of execution should increase significantly. And it took 29 seconds to execute all the tests just in the single worker running tests sequentially. So keep in mind when PlayWrite runs tests in parallel it randomly selects the spec file for the order of the execution. If you want to specify the exact order of the spec file that you want workers to be assigned if you use, for example, a single worker like that you can change the name of the file. For example, we want usePageObjects to be executed the first one. So you can rename this file and put the prefix 001-usePageObjects. And in this case, when you run this test that way you can see that usePageObjects was selected as the first spec file to be executed and so on. So if you put the indexes like 00123 and so on this is the way how you can structure the order of the execution of the spec file in case you need this. So let me put everything back. I put undefined right here and parallel true. I put it as false how it was before. So let's say you want to run the test in parallel only within the certain spec file but the rest of the spec should be executed sequentially. How you can do this? So let's say open this UI components. In this case, you have several options. In this particular file, we have testDescribe block and just other tests itself. So let's say if you want to run these two tests in parallel and the rest of the framework you want to run only sequentially. So you can put testDescribe.parallel and only these two tests, input fields and radio buttons will be executed in parallel within this spec file. The rest of the files will be executed sequentially. If you want to run the entire spec file to be executed in parallel you can put configuration something like that. test.describe.configure and then select the mode parallel. With this simple configuration, you tell Playwright that you want to run tests within the spec file in parallel assigning a new independent worker for each test. So the global config is configured to false but this particular file will be executed in parallel. Also, sometimes you may need to create a sequential execution of the test and make sure that the test after that, for example, radio buttons is not executed. This can be useful if you have a test dependency between the test. If the state of this test can impact the state of this test. This is by the way, a not recommended practice. You should avoid a test dependency as much as possible but just in case, if you need this kind of feature you can configure this as well. And you can do it like this. test.describe.configure and you type mode as serial. In this example, these two tests, input field and radio buttons will be executed one by one. And in case input field test fails the radio buttons test will not be executed. It will be just skipped as a dependent on the input fields tests. And one last thing I want to mention when you configure a parallel execution inside of the config file you can also do the same pair projects. So here Chromium project, Firefox project. If you want to run tests in parallel only for Chromium for example you can configure the settings over here and let's say, remove it from here. Such an option also available. So when you run tests in Chromium project it will be executed in parallel. When for Firefox, it will not. So that's the second settings that you can use inside of the framework. All right guys. So let's quickly summarize what we did in this lesson. Playwright by default runs tests in parallel assigning one independent worker per spec files. If you want to reduce number of workers or disable parallel execution completely you need to put numbers of workers to one. Also, you can improve the speed of execution by providing fully parallel true. In this case, tests within a single spec file will be executed in parallel as well. You can independently configure parallel execution on the spec level. If you want to particular spec file to execute tests in parallel you can provide configure mode parallel or provide a parallel attribute to the describe block or you can configure run the test in serial if you have a test dependency, for example and want the dependent test to be skipped if the previous test is failed. All right, that's it guys. And see you in the next lesson.