Test Execution with CLI | Bondar Academy
Course: Cypress UI Testing with JavaScript
Module: Cypress Hands-On Overview
Instructor: Artem Bondar
Lesson Summary
In this lesson, we learn how to run tests using the Command Line Interface (CLI) without the Cypress runner. Here’s a summary of the key points: Running Tests Open the terminal in Visual Studio Code . Use the command: npx cypress run to execute all tests in the E2E folder in headless mode . Execution results will display in the command line, showing the number of tests passed and failed. Handling Test Failures After a test fails, Cypress generates screenshots. For example, a failure occurred due to a missing base URL property in the configuration file. Running Specific Tests To run a specific test, use: npx cypress run --spec followed by the relative path to the spec file. To run tests in headed mode , add the flag: --headed . To specify a browser, use: --browser followed by the browser name (e.g., chrome or firefox ). Additional Options For more command options, refer to the Cypress documentation under the command line section. Some useful flags include: --parallel for running tests in parallel (Cypress Cloud required). --no-exit to keep the browser open after test execution. This lesson provides a foundational understanding of running Cypress tests via the command line, which is particularly useful for continuous integration environments.
Video Transcript
Hey guys, in this lesson, I will show you how to run tests using CLI or command line interface without using Cypress runner at all. All right, so let's jump into it. So in Visual Studio Code, open the terminal and the default command to run all tasks that you have in E2E folder would look like this, npx cypress, and instead of open, you would type run. And hit Enter, and that's it. Cypress will use the default browser and execute in the headless mode without running the visually browser, all tasks that you have currently in the E2E folder. So it's currently executed one by one, spec by spec, and this is what is displayed in the command line. All right, that's execution is completed. At the end, you will see the report printed in the command line. So totally, we executed 123 tests, 122 passed, and one failed. Total execution time is 56 seconds, so let's see. So this is the test that has failed, and let's see what's going on in the command line. 56 seconds, so let's see. So this is the test that has failed. And after the test failed, it's also generated for us as screenshots. So let's see what happened over here. And so validation failed to have property base URL or null but got playgroundbonderacademy.com. Yes, because we updated configuration file with the base URL property. And it looks like this test was just validating if this property was set or not. That's why this assertion failed, and this is pretty much expected. All right, let me delete this screenshot folder that was generated automatically. So how to run a specific test, for example. So I use the same command npx cypress run, but we'll use a flag dash dash spec. And then I need to provide the relative path to the spec file that I want to run. Let's say I want to run this todo cy.gs. So I make a right click, copy relative path, and paste it right here. That's it, and hit Enter. And right now, only this file will be executed, so we have six tests total. Now, if you want to run your test headed mode to see in the browser what actually happened, you type dash dash headed like this, hit Enter. And right now, the browser, oops, it's opened on a different screen like this. So let me try to run it one more time, running it again. Yeah, it's opening on the different screen for me again. So you see the browser was open. If you want to run a specific browser, you can set another flag, dash dash browser. And provide the browser name, for example, browser chrome. And run it one more time. Will it open the different window for me? No, right now it's opened on this one. See, the chrome is opened and test is executed. Or I have also Firefox, so you just provide the Firefox and run it. Now, and it should launch the Firefox browser. It's also on the different screen. So here's the Firefox, and the same test is executed in the Firefox. So those are the pretty much main basic commands that you might use in the command line, and those commands normally used on the continuous integration server when you run your test, not on your local computer, but somewhere on the server. So you provide those commands as part of your script. Where to find more options, what is available? So in the Cypress documentation under the references, you go to the command line right here. Scroll a little bit down, and here are all available options. So this is the options that we use. We use the browser, we used headed mode, and headless mode is a default value, and some other options. For example, you can provide the additional configuration file. We're gonna talk about it a little bit later, if you have several config files for your project. Or environment variables, or what else? If you want browser to stay open after the test execution, so you provide no exit and the browser will stay open. Some of those commands are related to execution with the Cypress Cloud. For example, if you want to record your session, or if you want to try to run tests in, where is it, parallel. Where is it, parallel flag, dash dash parallel. Yeah, this flag will work only if you use Cypress Cloud, or this CI build ID also. This flag gonna work only if you use on CI CD environment. Explore those options, some of the commands that you might use in your current environment locally, I just showed you, and this is pretty much how you run tests through the command line, pretty simple. All right guys, so see you in the next lesson.