{"id":10809,"date":"2025-05-12T15:30:00","date_gmt":"2025-05-12T15:30:00","guid":{"rendered":"https:\/\/testgrid.io\/blog\/?p=10809"},"modified":"2025-10-15T15:45:03","modified_gmt":"2025-10-15T15:45:03","slug":"how-to-run-cypress-tests-in-headless-mode","status":"publish","type":"post","link":"https:\/\/testgrid.io\/blog\/how-to-run-cypress-tests-in-headless-mode\/","title":{"rendered":"How to Run Cypress Tests in Headless Mode"},"content":{"rendered":"\n<p>Cypress is a powerful open-source end-to-end testing framework designed for web applications. It simplifies the process of writing and executing tests, providing developers with a robust tool for ensuring the quality and reliability of their web applications.<\/p>\n\n\n\n<p>Running Cypress tests in headless mode means executing the tests without displaying the graphical user interface of the browser. This can be useful in certain scenarios, such as when running tests in a continuous integration (CI) environment or when you want to perform tests without the visual distraction of the browser window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Setup Cypress Headless<\/h2>\n\n\n\n<p>Setting up Cypress to run tests in headless mode involves a few steps. Ensure that Node.js and npm (Node Package Manager) are installed on your machine. If not, you can download and install them from the official <a href=\"https:\/\/nodejs.org\/en\/download\" target=\"_blank\" rel=\"noopener\">Node.js website<\/a>. <\/p>\n\n\n\n<p>Below is a general guide on how to set up Cypress in headless mode:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initiate the Project<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Create a folder in your system and open it using any JavaScript-enabled IDE. E.g.. VS Code, IntelliJ, Eclipse, etc. Then initiate the project by using the command below:<\/p>\n\n\n\n<p><em><strong>npm init command<\/strong><\/em><\/p>\n\n\n\n<p>It will create a package.json file in the project directory.&nbsp;<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Install Cypress<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Once you have your project folder created, navigate to your project directory in the terminal and run the following command to install Cypress:<\/p>\n\n\n\n<p><em>npm install cypress &#8211;save-dev<\/em><\/p>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Open Cypress<\/strong><\/li>\n<\/ol>\n\n\n\n<p>After the installation is complete, open Cypress for the first time to set up the initial project structure. Run the following command:<\/p>\n\n\n\n<p>npx cypress open<\/p>\n\n\n\n<p>This command opens the Cypress Test Runner<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1366\" height=\"730\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/02\/cypress1.jpg\" alt=\"cypress Headless\" class=\"wp-image-10810\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>Here, we will select the Testing type. For <a href=\"https:\/\/testgrid.io\/blog\/end-to-end-testing-a-detailed-guide\/\">end-to-end testing<\/a>, select the E2E testing option, and for testing individual components or units of a web application, select component testing. Here in this example, we have selected E2E testing.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1366\" height=\"728\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/02\/cypress2.jpg\" alt=\"cypress headless for end-to-ending testing\" class=\"wp-image-10811\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>On pressing continue, Cypress will create a \u2018cypress\u2019 directory with default files. After selecting your preferred browser, the browser will be opened automatically.<\/p>\n\n\n\n<p>4. Create your first specs<\/p>\n\n\n\n<p>In the opened browser, select the option whether you want a few already created example specs or want to start with a new blank specs file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1366\" height=\"728\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/02\/cypress3.jpg\" alt=\"\" class=\"wp-image-10812\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>On selecting the Create new spec option, a new spec file will automatically be generated in the \u2018e2e\u2019 subdirectory of the \u2018cypress\u2019 directory. Now the project structure will look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"401\" height=\"534\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/02\/image.png\" alt=\"\" class=\"wp-image-10814\" style=\"width:315px;height:420px\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>5. Configure Headless Mode<\/strong><\/p>\n\n\n\n<p>By default, Cypress runs tests in headless mode during the command line execution (cypress run headless). If you want to customize headless options, you can update your npm scripts in package.json. <\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>{\n  \"name\": \"cypress-headless\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"test\": \"cypress run --headless\"\n  },\n  \"author\": \"Arjun Shrivastva\",\n  \"license\": \"ISC\",\n  \"devDependencies\": {\n    \"cypress\": \"^13.6.0\"\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>This script will run Cypress tests in headless mode when you execute <strong><em>npm run test<\/em>.<\/strong><\/p>\n\n\n\n<p>6. Customize Headless Options<\/p>\n\n\n\n<p>You can customize headless options by adding additional flags to the cypress run command. For example, to run tests in Cypress headless Chrome, you can use:<\/p>\n\n\n\n<p><em>npm run test &#8211;browser chrome<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example for Testing with Cypress in Headless Mode<\/h2>\n\n\n\n<p>For example, you will log in to a Demo Website, and after that, we will verify that the login is successful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Test Steps<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to the URL.(<a href=\"https:\/\/demo.guru99.com\/test\/login.html\" target=\"_blank\" rel=\"noopener\">https:\/\/demo.guru99.com\/test\/login.html<\/a>)<\/li>\n\n\n\n<li>Fill in the \u2018Email address\u2019 field<\/li>\n\n\n\n<li>Fill in the \u2018Password\u2019 field<\/li>\n\n\n\n<li>Click the \u2018Sign in\u2019 Button<\/li>\n\n\n\n<li>Verify Login is successful.<\/li>\n<\/ol>\n\n\n\n<p>Test Script<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>describe('Login', () =&gt; {\n  it('Log in successful', () =&gt; {\n    cy.visit('https:\/\/demo.guru99.com\/test\/login.html')\n    cy.get('#email').click().type('test@test.com')\n    cy.get('#passwd').click().type('test')\n    cy.get('#SubmitLogin').click()\n    cy.get('.error-copy').should('be.visible')\n  })\n})\n<\/code><\/pre>\n\n\n\n<p>To execute the test script, just run the command below:<\/p>\n\n\n\n<p><strong>npm run test<\/strong><\/p>\n\n\n\n<p><strong>Test Result<\/strong><\/p>\n\n\n\n<p>After the execution, you can see the test result in the terminal.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1042\" height=\"280\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/02\/cypress4.jpg\" alt=\"cypress test results - headless mode\" class=\"wp-image-10813\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Advantages of Cypress Headless<\/h1>\n\n\n\n<p>Cypress headless mode, where tests run without a visible browser, provides several key advantages in the realm of automated testing:<\/p>\n\n\n\n<p><strong>1. Resource Optimization<\/strong><\/p>\n\n\n\n<p>Headless browsers consume far fewer computational resources than their headed counterparts. This means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduced memory and CPU usage on your testing machine.<\/li>\n\n\n\n<li>Potential to run more tests in parallel,&nbsp;further increasing efficiency.<\/li>\n<\/ul>\n\n\n\n<p><strong>2. Parallel Execution for Speed<\/strong><\/p>\n\n\n\n<p>Headless mode supports parallel test execution, enabling multiple tests to run simultaneously without the overhead of managing visible browser windows. This results in significant time savings and improved overall test suite performance.<\/p>\n\n\n\n<p>3. <strong>Reduced Flakiness:<\/strong> <\/p>\n\n\n\n<p>Some test failures in headed mode might result from inconsistencies in browser rendering or other visual factors. Headless execution can help minimize these, focusing on functionality rather than rendering quirks.<\/p>\n\n\n\n<p><strong>4. Consistent Testing Environment<\/strong><\/p>\n\n\n\n<p>Headless mode ensures a consistent testing environment by eliminating variations related to browser window sizes and visibility. This consistency enhances the reliability and predictability of test results across different runs and environments.<\/p>\n\n\n\n<p><strong>5. Seamless Integration with CI\/CD Tools:<\/strong><\/p>\n\n\n\n<p>Cypress headless mode seamlessly integrates with popular CI\/CD tools such as Jenkins, Travis CI, and GitLab CI. This allows for the automatic triggering and execution of tests as an integral part of the development and deployment workflows.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Conclusion<\/h1>\n\n\n\n<p>In conclusion, running Cypress tests in headless mode is a strategic choice for modern software development. The efficiency gains in CI\/CD pipelines, resource optimization, and seamless integration with development workflows make it a compelling approach. Headless mode not only accelerates test execution but also ensures a consistent testing environment, fostering reliability and scalability. As teams strive for faster, more automated testing practices, Cypress headless mode stands out as a valuable tool, facilitating agile development and enhancing the overall quality of software products.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cypress is a powerful open-source end-to-end testing framework designed for web applications. It simplifies the process of writing and executing tests, providing developers with a robust tool for ensuring the quality and reliability of their web applications. Running Cypress tests in headless mode means executing the tests without displaying the graphical user interface of the [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":11516,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[209],"tags":[],"class_list":["post-10809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-test-automation"],"acf":[],"images":{"medium":"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/02\/How-to-Run-Cypress-Tests-in-Headless-Mode.jpg","large":"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/02\/How-to-Run-Cypress-Tests-in-Headless-Mode.jpg"},"_links":{"self":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/10809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/comments?post=10809"}],"version-history":[{"count":5,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/10809\/revisions"}],"predecessor-version":[{"id":15559,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/10809\/revisions\/15559"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/media\/11516"}],"wp:attachment":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/media?parent=10809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/categories?post=10809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/tags?post=10809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}