Run Tests with Command Line | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Getting Started with Playwright
Instructor: Artem Bondar
Lesson Summary
In this lesson, you will learn how to run Playwright tests using the command-line interface (CLI). The key topics covered include: Running Tests To run all tests, use the command: npx playwright test . This runs tests in headless mode by default. Playwright runs tests across multiple browsers defined in config.ts , such as Chromium , Firefox , and WebKit . To view the test report, use: npx playwright show-report . Running Tests in Specific Browsers To run tests for a specific browser, specify the project: npx playwright test --project=Chromium . To run tests in headed mode (visible browser), add the --headed flag. Running Specific Test Files To run a specific test file: npx playwright test example.spec.ts --project=Chromium . To run a specific test by name, use: npx playwright test -g "has title" . Handling Test Failures and Skips To intentionally fail a test, modify the expected outcome in the test code. To skip a test, use test.skip . The report will show which tests passed and which were skipped. To run only a specific test, use test.only to focus on that test while ignoring others. These commands are particularly useful for running tests on a CI/CD server or for quick local testing. The next lesson will cover running tests in UI mode .
Video Transcript
In this lesson, I will show you how to run playwright tests using command-line interface or just terminal. For example, how to run all tests or how to run tests for the specific browser or how to run tests in headed mode or headless mode, how to run the playwright report using command-line interface as well. So those commands are very useful when you need to trigger tests, for example in CI-CD server. This is the way how you can trigger different tests that way. So in this lesson, you will learn how to do that. Let's get into it. So in the previous lesson, we installed the framework from scratch and we have example-spec.ts with two tests as an example. So let's run those tests. How to do that? So we go to terminal and type in npx playwright test and hit enter and playwright will run those tests for us automatically, headlessly. You see, all of them passed and we see six tests passed. Why is that? Because by default, playwright will run those tests in three browsers. Browsers are defined in the config.ts. If we scroll this down, we see the projects. Project Chromium, Firefox and WebKit. So in all three browsers in parallel, those tests, these two tests will be executed. That's why we have six tests. And we see that if we run npx playwright show report, we can check the report. So let's do this. npx playwright show-report and report is opened as HTML page. We can see that two tests executed in Chromium, two in Firefox and two in WebKit. And if we click on any of those tests, we have a before hook. Then we have two steps of our test that shows the piece of code that was executed. And the after hook is like a teardown method. So that's it. Okay, how to run the test for this specific browser? So you call the same command npx playwright test, but specify a project that we want to use. So let's say project equals to Chromium. Chromium is a Chrome browser. Run it again. And we see that just two tests are executed and they are executed headlessly, as you may notice. So how to execute test headed? So we will see the actual browser. So we will open this command. And in this example, we need to add a command dash dash headed. So during the execution, playwright will run a browser and we will see the actual execution. So you see it happened really, really quick. As I mentioned, playwright works very quick and those tests are pretty short. I can run it one more time. But you saw the browser showed up. It executed two tests and teardown. So what else you can do through the command line? Let's say that you have several test files under the test folder. For example, we have examplespec.ts and let's say we will have a few more files. But if you want to execute just one specific file, this is how you should do it. So you type npx playwright test, then space, and then just provide the name of the spec file that you want to run. Example.spec.ts. Space. Project is Chromium. Let's run it headless this time. I run it and we can see two tests are executed because we call this particular spec file. And also you can call a specific name of the test. For example, let's say we want to run just has title test. How to do that? So we use the same command, but I remove the name of the spec file and I replace it with this –g double quotes. And then inside of the double quotes, I provide the name of the test, has title. Click Enter. And we can see one test is executed. Let me open the report, npx playwright show report. Here's our report. Has title is executed. And let me show you, by the way, what happens if we introduce a failure into our test. So here at line number seven, I expect the page to have title playwright, but I change it playwright one, right? So I want to fail it on purpose. And if I will run this test right now. It is running and test is failed. So, and I go to the report and here we go what I see in the report. That expected pattern was a playwright, but the actual was this. So our pattern playwright one was not as part of the stream. That's why it failed. And it's showing the part of the line of code in the report where exactly it failed. All right, so let me fix this back. And two more things I want to show you. Let's say we have a test has title that we want to ignore from the execution. How to do that? We can use the tag test dot skip. And if I run this spec file now. You can see in the report one test passed and one is skipped. And let me show you how it's displayed in the reporter. So if you look in the reporter, we see that two tests, but one was skipped. And we can see here in the filter skipped one and passed one. Passed test has a check mark, skip test doesn't have anything. If we open the skip test, we see the skip message. This is how it displayed in the reporter. And if you want, for example, run just a specific test as part of your test suite. Let's say you work on the debugging and working on this particular test. And when you run the test, you want to run only this particular test. How to do this? Same way, very easy. You add test dot only. And if you run the command to execute this spec file. Only one test was executed. One test passed. So we ran only this one and the remaining of the test. Either this test or if we have more tests than this, they would be just ignored. We would run only this test. So that's it. That's the main commands that you can use in the terminal to run your playwright test, specify the spec files, skip the test, run specific tests and so on. These commands are mostly useful when you want to run your tests on continuous integration server. Or if you want to quickly run certain tests headlessly on your computer. When you work on test development, more comfortably work with the UI mode. And how to run your test in UI mode, I will show you in the next lesson. So see you in the next lesson.