{"id":11245,"date":"2025-01-06T14:30:54","date_gmt":"2025-01-06T14:30:54","guid":{"rendered":"https:\/\/testgrid.io\/blog\/?p=11245"},"modified":"2026-03-09T16:17:02","modified_gmt":"2026-03-09T16:17:02","slug":"cypress-testing","status":"publish","type":"post","link":"https:\/\/testgrid.io\/blog\/cypress-testing\/","title":{"rendered":"Cypress For Test Automation: A Step-by-Step Guide"},"content":{"rendered":"\n<p>With the complexity of software applications growing every day, one of the most important things for every testing team to focus on is optimizing test coverage. Testers all over the world use testing tools and frameworks to conduct various kinds of testing. However, testing software systems from end-to-end along with all of their subsystems is an urgent necessity.<\/p>\n\n\n\n<p>Cypress is a modern JavaScript-based E2E (End-to-End) testing framework designed for modern web applications. As a Cypress tool for automation, it enables developers to write fast, reliable, and maintainable tests for web applications. It provides a powerful and easy-to-use way to automate tests. It is primarily used for testing web applications and can be integrated with various tools and frameworks like React, Angular, Vue, and more. Its unique features, like time travel and automatic waiting, can significantly improve the efficiency and reliability of your testing process.<\/p>\n\n\n\n<p>In this blog, we will deep dive into how to set up Cypress to perform End to End testing with Cypress locally and in the cloud using TestGrid<\/p>\n\n\n\n<p>Before explaining more about Cypress, let&#8217;s see the NPM trend for Cypress. Looking at the npm trends gives us some insights into the popularity and adoption rates of Cypress.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"898\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/NPM-trend-for-Cypress.jpg\" alt=\"NPM trend for Cypress\" class=\"wp-image-11270\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is Cypress Testing Framework<\/strong>?<\/h2>\n\n\n\n<p>Cypress is a popular open-source testing framework that is primarily used for web application testing. It is built using JavaScript, so if you are wondering what language Cypress uses, the answer is JavaScript, which makes it a great fit for front-end developers.<\/p>\n\n\n\n<p>Many new testers often ask, Is Cypress a framework or just a library? Cypress is indeed a complete end-to-end testing framework built on JavaScript for fast and interactive browser testing.<\/p>\n\n\n\n<p>One of the key advantages of Cypress over other testing frameworks like Selenium is that it doesn&#8217;t require the use of driver binaries. Unlike Selenium, which uses the WebDriver protocol, the Cypress WebDriver-free model interacts directly with the browser, resulting in faster and more stable execution.<\/p>\n\n\n\n<p>The screenshot below shows how tests are executed inside of the browser, which reduces test execution time and removes network latency.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1434\" height=\"1012\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image2.jpg\" alt=\"cypress testing - how tests are executed inside of the browser\" class=\"wp-image-11246\" loading=\"lazy\" title=\"\"><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/www.tutorialspoint.com\/cypress\/images\/cypress_architecture.jpg\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/figcaption><\/figure>\n\n\n\n<p>Cypress automation testing runs on a NodeJS server. To create a connection between the browser and the Node.js server, Cypress uses the WebSocket protocol. WebSockets provide full-duplex communication, which allows Cypress to send commands, such as navigating to URLs, interacting with elements, and making assertions, from the test code to the browser in real-time. It also enables Cypress to receive feedback, such as DOM snapshots, console logs, and other test-related information, from the browser back to the server.<\/p>\n\n\n\n<p><strong>Let&#8217;s break down the components and how they interact:<\/strong><\/p>\n\n\n\n<p>Understanding the Cypress architecture helps visualize how its runner, Node.js server, and browser communicate through WebSockets during real-time test execution.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>User Interaction<\/strong>: The process starts with a user interacting with the web application. This could involve actions like clicking buttons, selecting values from drop-downs, filling forms, or navigating through pages.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cypress Test Scripts<\/strong>: Developers create test scripts using JavaScript or Typescript. These scripts simulate user interactions and verify that the application behaves as expected.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cypress Runner:<\/strong> The Cypress Runner executes the test scripts. The Cypress test runner provides an interactive GUI that displays every command, assertion, and application state while the tests execute, making debugging far simpler. It interacts with the web application, capturing screenshots and videos during the test.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cypress Dashboard:<\/strong> Cloud-based service for test recording, parallelization, and analytics. Plugins extend Cypress&#8217;s functionality, offering custom commands and integrations to tailor Cypress to specific project requirements and workflows<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Proxy Server<\/strong>: A proxy server sits between the Cypress Runner and the web application. It intercepts requests and responses, allowing developers to manipulate them.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Node.js<\/strong>: Cypress runs on Node.js, providing a runtime environment for executing JavaScript\/or Typescript code.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Web Socket<\/strong>: The Web Socket protocol enables real-time communication between the Cypress Runner and the web application.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>HTTP Requests\/Responses:<\/strong> In the architecture diagram, you can see HTTP requests (e.g., GET, POST) and responses exchanged between the Cypress Runner and the application server.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Cypress Test Execution Works:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The user interacts with the web application.<\/li>\n\n\n\n<li>Cypress test scripts send commands to the Cypress Runner.<\/li>\n\n\n\n<li>The Cypress Runner communicates with the proxy server.<\/li>\n\n\n\n<li>The proxy server forwards requests to the application server.<\/li>\n\n\n\n<li>The application server processes requests and sends back responses.<\/li>\n\n\n\n<li>The Cypress Runner captures screenshots and videos during the test.<\/li>\n\n\n\n<li>Developers analyze the test results to ensure the application functions correctly.<\/li>\n<\/ul>\n\n\n\n<p>So far, we have seen how test cases are executed internally in Cypress. In the next section, you will see why Cypress is the best choice for automation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Cypress For Automation Testing<\/strong><\/h2>\n\n\n\n<p>Cypress has become very popular in the automation testing space due to several reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Easy Setup<\/strong>: Cypress has a simple installation process and does not require any additional dependencies or setup. It is simple to include in both new and current projects.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Automatic Waiting<\/strong>: Cypress automatically waits for the elements to appear on the page before performing any actions or assertions. It intelligently handles asynchronous actions, eliminating the need for explicit waits or sleep statements.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Real-Time Reloads<\/strong>: As you make changes to your test files or application code, Cypress automatically reloads the test runner, providing real-time feedback. Debugging and development are sped up by this functionality.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Travel<\/strong>: Cypress allows you to go back and forth in time during test execution. You can take snapshots at different points in your test and even manipulate the application&#8217;s state to debug failures more effectively.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Interactive Test Runner<\/strong>: Cypress provides a user-friendly test runner interface that displays the application being tested in real-time. You can interact with the application, inspect elements, and see assertions and command logs.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Debuggability<\/strong>: Cypress offers built-in debugging tools that allow you to pause test execution at any point and inspect the application&#8217;s state. You can also use the browser&#8217;s developer tools alongside Cypress for advanced debugging.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Network and XHR Control<\/strong>: Cypress provides full control over the network and XHR requests. You can stub or mock network requests, modify responses, and test various scenarios without making actual HTTP calls.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cross-Browser Testing<\/strong>: Cypress supports <a href=\"https:\/\/testgrid.io\/cross-browser-testing\" data-type=\"URL\" data-id=\"https:\/\/testgrid.io\/blog\/cross-browser-testing-guide\/\">cross-browser testing <\/a>and allows you to run tests on different browsers simultaneously. It also provides plugins for integration with popular Continuous Integration (CI) platforms and <a href=\"https:\/\/testgrid.io\/blog\/cloud-testing\/\" data-type=\"URL\" data-id=\"https:\/\/testgrid.io\/blog\/cloud-testing\/\">cloud-based testing<\/a> services.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Getting Started with Cypress Testing<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Install Cypress<\/strong><\/h3>\n\n\n\n<p>To start with Cypress testing, we need to set up Cypress first. To create a new project for Cypress automated testing, we have to follow the steps below<\/p>\n\n\n\n<p><strong>Install Node.js: <\/strong>Cypress requires Node.js to run. Download and <a href=\"https:\/\/www.carmatec.com\/blog\/how-to-install-node-js-and-npm-on-windows-macos-linux\/\" target=\"_blank\" rel=\"noopener\">install Node.js<\/a> from the official website (<a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noopener\">https:\/\/nodejs.org<\/a>) if you haven&#8217;t already. Ensure you have Node.js version 16 or above.<\/p>\n\n\n\n<p><strong>Create a new project:<\/strong> Open your terminal or command prompt and navigate to the directory where you want to set up your Cypress project.&nbsp;<\/p>\n\n\n\n<p>Create a project, let&#8217;s name it <strong>cypress_testgrid<\/strong><\/p>\n\n\n\n<p><strong>Initialize a new Node.js project:<\/strong> Start a new Node.js project by entering the commands below, and a <strong><em>package.json<\/em><\/strong> file will be generated.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>npm init -y\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1368\" height=\"628\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image13.jpg\" alt=\"Getting Start with Cypress Testing\" class=\"wp-image-11247\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>Install Cypress<\/strong>: Run the following command to install Cypress as a development dependency:<\/p>\n\n\n\n<p>npm install cypress &#8211;save-dev<\/p>\n\n\n\n<p>OR<\/p>\n\n\n\n<p>yarn add cypress &#8211;dev<\/p>\n\n\n\n<p>In the screenshot below, you can see that Cypress is installed successfully<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1342\" height=\"458\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image1.jpg\" alt=\"\" class=\"wp-image-11248\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>One Cypress is installed, you can see the latest version in package.json. As of today, when I am writing this article the latest version of Cypress is <a href=\"https:\/\/docs.cypress.io\/guides\/references\/changelog#13-7-1\" target=\"_blank\" rel=\"noopener\">13.7.1<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1306\" height=\"822\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image22.jpg\" alt=\"\" class=\"wp-image-11249\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>In the above screenshot, we can see Cypress is installed. Now run the below command to open the Cypress runner and execute the test cases.<\/p>\n\n\n\n<p>yarn cypress open<\/p>\n\n\n\n<p>OR<\/p>\n\n\n\n<p>npx cypress open&nbsp;<\/p>\n\n\n\n<p>You will see the screen below, select <strong><em>E2E<\/em><\/strong> Testing from the screenshot below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"1254\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/cypress-dashboard.jpg\" alt=\"Cypress Dashboard screenshot\" class=\"wp-image-11250\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>Follow the wizard, and finally, you will see the screenshot below with default test cases provided by Cypress.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"965\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image21.jpg\" alt=\"\" class=\"wp-image-11251\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Folder structure of the Cypress Framework<\/strong><\/h3>\n\n\n\n<p>The screenshots below demonstrate how Cypress creates a default folder hierarchy when it first launches. Below is a detailed description of each of these files and folders that the Cypress framework has.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1296\" height=\"800\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/Folder-structure-of-Cypress.jpg\" alt=\"Folder structure of Cypress Framework\" class=\"wp-image-11252\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>Below table summarizes the purpose of each directory and file within the Cypress project.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Directory\/File<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>cypress\/<\/td><td>Main directory containing all Cypress-related files.<\/td><\/tr><tr><td>fixtures\/<\/td><td>This directory contains JSON files that store static test data. For example, you can place sample data for API responses or form inputs in these files.<\/td><\/tr><tr><td>e2e\/<\/td><td>In the e2e directory, your test files (also known as spec files) reside. You can organize your tests into folders based on the application&#8217;s features or modules.<\/td><\/tr><tr><td>support\/<\/td><td>The support folder contains the files commands.js and e2e.js.<strong>command.js: <\/strong>The commands.js file is commonly used to define custom commands that you can reuse across different test files<strong>e2e.js:<\/strong> The e2e.js file is the entry point for loading various support files..<\/td><\/tr><tr><td>Cypress.config.js<\/td><td>Cypress uses a configuration file \u2018cypress.config.json\u2019&nbsp; to alter a project&#8217;s default setup settings.<\/td><\/tr><tr><td>node_modules<\/td><td>This directory contains the dependencies installed for your Cypress project. It is automatically generated when you run npm install or use a package manager to install Cypress.<\/td><\/tr><tr><td>package.json<\/td><td>Holds metadata about the project and its dependencies.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In the next section, you will see how we can create new test cases in Cypress<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Create Test File<\/strong> in Cypress<\/h3>\n\n\n\n<p>Create a new test file under cypress\/e2e\/&nbsp; to run an e2e test case, let&#8217;s give a name to the file, e.g, <em>loginAndTabFunctionality.cy.js<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1132\" height=\"1066\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/Test-File-creation-in-Cypress.jpg\" alt=\"Create Test File using Cypress\" class=\"wp-image-11253\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Write Your Test<\/strong><\/h3>\n\n\n\n<p>As we mentioned above, Cypress supports the Mocha framework. Therefore, prior to moving to our initial script, it is crucial to grasp certain key concepts.<\/p>\n\n\n\n<p><strong>Describe() Block :&nbsp;<\/strong><\/p>\n\n\n\n<p>describe() block is to define a suite of tests that are logically related to each other. These could be tests for a specific module, component, or functionality of an application.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1622\" height=\"446\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image4.jpg\" alt=\"Describe() Block in cypress\" class=\"wp-image-11255\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>it() Blocks: <\/strong>&nbsp;<\/p>\n\n\n\n<p>it() block defines a single, specific test case. This test case focuses on verifying a particular aspect of the functionality you&#8217;re testing.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1638\" height=\"660\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image6.jpg\" alt=\"it() block cypress\" class=\"wp-image-11254\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>Hooks:<\/strong><\/p>\n\n\n\n<p>Cypress leverages Mocha&#8217;s hooks (before(), beforeEach(), after(), and afterEach()) to provide a structured way to execute code before or after tests.<\/p>\n\n\n\n<p>The order of execution for these hooks is crucial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>before(): <\/strong>Runs once before all tests in a test file. Ideal for one-time setup tasks like connecting to a database or initializing global variables.<\/li>\n\n\n\n<li><strong>beforeEach()<\/strong>: Runs before each individual test. Useful for setting up test-specific conditions or data, ensuring each test starts from a clean slate.<\/li>\n\n\n\n<li><strong>afterEach():<\/strong> Runs after each individual test. Good for cleaning up after each test, like closing database connections or clearing temporary data or logout from the application etc.<\/li>\n\n\n\n<li><strong>after():<\/strong> Runs once after all tests in a test file. Use this for final cleanup tasks that may need to happen only once after all tests are complete.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1596\" height=\"1626\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/Cypress-hooks-before-beforeEach-after-and-afterEach.jpg\" alt=\"Cypress uses Mocha&#039;s hooks \" class=\"wp-image-11256\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>Assertions:<\/strong><\/p>\n\n\n\n<p>Cypress provides its own set of built-in assertions and extends Mocha with Chai assertions that offer a convenient way to verify test conditions. These assertions use a chainable syntax, making your tests more readable.<\/p>\n\n\n\n<p>Some of the Examples are&nbsp; <em>.should(&#8216;have.text&#8217;, &#8216;Expected Text&#8217;)<\/em>,<em>.should(&#8216;not.equal&#8217;, &#8216;Expected Text&#8217;)<\/em>,<em>.should(&#8216;exist&#8217;)<\/em>,<em>.should(&#8216;not.be.disabled&#8217;)<\/em>, <em>&nbsp;.should(&#8216;have.class&#8217;, &#8216;active&#8217;),<\/em> etc.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1678\" height=\"1376\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image8.jpg\" alt=\"cypress Assertions\" class=\"wp-image-11257\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>Read more about <a href=\"https:\/\/testgrid.io\/blog\/cypress-assertions\/\" data-type=\"URL\" data-id=\"https:\/\/testgrid.io\/blog\/cypress-assertions\/\">Cypress assertions<\/a> in this post.<\/p>\n\n\n\n<p><strong>Excluding the Tests<\/strong><\/p>\n\n\n\n<p>The<strong> .skip()<\/strong> method allows you to temporarily exclude a test or an entire test suite from being run during test execution.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1616\" height=\"804\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image18.jpg\" alt=\"cypress .skip() method to execute the test\" class=\"wp-image-11259\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>The<strong><em> describe.skip()<\/em><\/strong> function allows you to temporarily exclude an entire suite of tests defined within a describe block from execution during test runs.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1512\" height=\"1110\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image7.jpg\" alt=\"describe.skip() function syntex in cypress\" class=\"wp-image-11261\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>Including the Tests:<\/strong><\/p>\n\n\n\n<p>The <strong><em>.only() <\/em><\/strong>method allows you to instruct the test runner to execute only the specific test or test suite marked with .only(). All other tests in the suite or file will be skipped.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1666\" height=\"1096\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/describe.only-method.jpg\" alt=\"describe.only() method in cypress\" class=\"wp-image-11262\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>So far we discussed some concepts that are required for a user who is writing his script for the first time. Let&#8217;s leverage these concepts we&#8217;ve discussed to build a simple Cypress script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Automate Your First Cypress Test Case<\/strong><\/h2>\n\n\n\n<p>Let\u2019s create a new folder under the e2e folder named&nbsp; \u201ccypress_testgrid_testcases\u201d to perform Cypress end-to-end testing.<\/p>\n\n\n\n<p>Let\u2019s create a new folder under the e2e folder named&nbsp; \u201ccypress_testgrid_testcases\u201d to perform Cypress end-to-end testing.<br>Let\u2019s take Cypress testing examples to test end-to-end test scenarios from login to the site https:\/\/testgrid.io\/ with valid credentials. Verify tab functionality and log out from the application.<\/p>\n\n\n\n<p><strong>Test Scenario<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><br>1. Visit the Site https:\/\/testgrid.io\/<br>2. Log in to the site with valid credentials&nbsp;<br>3. Verify the user is logged in by varying the text&nbsp; \u201cDashboard\u201d<br>4. Click on the \u2018Codeless\u2019&nbsp; link under the Automation section.<br>5. Verify the text \u201cLet&#8217;s get you started with codeless automation.\u201d<br>6. Open The Link &#8216;Real Device Cloud&#8217; in a New Tab and then Back To the Parent Page<br>7. Verify text \u201cSelenium\u201d to make sure the user is back to the parent page<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Write a simple Cypress test in file \u2018<strong>loginAndTabFunctionality.cy.js<\/strong>\u2019 that covers below functionality<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Visiting the website <a href=\"https:\/\/testgrid.io\/\">https:\/\/testgrid.io\/<\/a><\/li>\n\n\n\n<li>Log in to the site with valid data<\/li>\n\n\n\n<li>Verify tabbing functionality.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>\/\/\/ &lt;reference types=\"cypress\" \/&gt;\ndescribe(\"Verify Login\/Logout And Tab functionality\", () =&gt; {\n beforeEach(() =&gt; {\n   cy.visit(\"https:\/\/testgrid.io\/\")\n   cy.get('&#91;title=\"Sign in\"]').click()\n   cy.get(\"#email\").clear(\"xxxxxx@gmail.com\").type(\"xxxxxx@gmail.com\");\n   cy.get(\"#password\").clear().type(\"xxxxx\");\n   cy.get(\".signin-button\").click();\n });\n afterEach(() =&gt; {\n   cy.get(\"&#91;data-toggle='dropdown']\").click();\n   cy.contains(\"Logout\").click();\n   cy.contains(\"Forgot Password?\");\n });\n it(\"Login and Click on 'Codeless' link Under Automation Section\", function () {\n   cy.contains('Dashboard')\n   cy.get(\"#tgtestcase\").click();\n   cy.contains(\"Lets get you started with codeless automation\");\n });\n it(\"Open The Link 'Real Device Cloud' in New Tab and Back To Parent Page\", function () {\n   cy.get('&#91;id=\"realdevice_li\" ] &gt; a').invoke(\"removeAttr\", \"target\").click();\n   cy.url().should(\"include\", \"\/devicecloud\");\n   cy.contains('Device Groups')\n   cy.go(\"back\");\n   cy.contains(\"Selenium\");\n });\n});<\/code><\/pre>\n\n\n\n<p><strong>Code walkthrough<\/strong><\/p>\n\n\n\n<p>To understand the Cypress testing example script in detail. Let&#8217;s undertake the above code walkthrough.<\/p>\n\n\n\n<p><strong>Before Each Test (beforeEach):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Visit the TestGrid.io homepage (cy.visit(&#8220;https:\/\/testgrid.io\/&#8221;)).<\/li>\n\n\n\n<li>Clicks on the &#8220;Sign in&#8221; button (cy.get(&#8216;[title=&#8221;Sign in&#8221;]&#8217;).click()).<\/li>\n\n\n\n<li>Enters the email address &#8220;xxxxx@gmail.com&#8221; in the email field (cy.get(&#8220;#email&#8221;).clear().type(&#8220;jxxxxx@gmail.com&#8221;)).<\/li>\n\n\n\n<li>Enters the password &#8220;xxxxx&#8221; in the password field (cy.get(&#8220;#password&#8221;).clear().type(&#8220;xxxxx&#8221;)).<\/li>\n\n\n\n<li>Clicks on the &#8220;Sign in&#8221; button (cy.get(&#8220;.signin-button&#8221;).click()).<\/li>\n<\/ul>\n\n\n\n<p><strong>After Each Test (afterEach):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clicks on the user dropdown menu (cy.get(&#8220;[data-toggle=&#8217;dropdown&#8217;]&#8221;).click()).<\/li>\n\n\n\n<li>Clicks on the &#8220;Logout&#8221; option (cy.contains(&#8220;Logout&#8221;).click()).<\/li>\n\n\n\n<li>Verifies the presence of the text &#8220;Forgot Password?&#8221; after logout&nbsp; for confirmation user is logged-out<\/li>\n<\/ul>\n\n\n\n<p><strong>Test 1:<\/strong> Log in and Click on the &#8216;Codeless&#8217; Link<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Verifies the presence of the text &#8220;Dashboard&#8221; after login (likely to confirm successful login).<\/li>\n\n\n\n<li>Clicks on the element with ID &#8220;tgtestcase&#8221;<\/li>\n\n\n\n<li>Verifies the presence of the text &#8220;Let&#8217;s get you started with codeless automation&#8221; to confirm navigating to the codeless automation section<\/li>\n<\/ul>\n\n\n\n<p><strong>Test 2:<\/strong> Open Link in New Tab and Go Back<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clicks on the link text within the element with ID &#8220;realdevice_li&#8221; and removes the &#8220;target&#8221; attribute using invoke (likely to open the link in a new tab &#8211; cy.get(&#8216;[id=&#8221;realdevice_li&#8221; ] &gt; a&#8217;).invoke(&#8220;removeAttr&#8221;, &#8220;target&#8221;).click()).<\/li>\n\n\n\n<li>Asserts that the URL includes &#8220;\/devicecloud&#8221; to confirm navigation to the &#8220;Real Device Cloud&#8221; page<\/li>\n\n\n\n<li>Verifies the presence of the text &#8220;Device Groups&#8221; on the new page.<\/li>\n\n\n\n<li>Uses cy.go(&#8220;back&#8221;) to navigate back to the previous page (TestGrid homepage).<\/li>\n\n\n\n<li>Verifies the presence of the text &#8220;Selenium&#8221; after returning to the homepage of TestGrid.<\/li>\n<\/ul>\n\n\n\n<p>We have written our FIRST script now time to execute the script locally. In the next section you will see how we can execute the test script locally.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Execute The Test Cases Locally<\/strong><\/h2>\n\n\n\n<p>To execute the test cases, you can use Cypress commands in your terminal. You can run Cypress tests in either headed or headless mode, depending on whether you need real-time visibility or faster background execution.<\/p>\n\n\n\n<p>Cypress provides options to run tests both in headless mode (where you can see the browser window) and headless mode (where tests run in the background without a visible browser window).&nbsp;<\/p>\n\n\n\n<p>Here&#8217;s how you can execute the test cases in both modes:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Run Test Cases In Headed Mode:<\/strong><\/h3>\n\n\n\n<p>In headed mode, you can see the test execution in a visible browser window.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open your terminal.<\/li>\n\n\n\n<li>Navigate to the directory where your Cypress tests are located.<\/li>\n\n\n\n<li>Run the following command to execute the tests in headed mode.<\/li>\n<\/ul>\n\n\n\n<p>Run the command yarn cypress open or npx cypress open. As we run the command, it will open a screen below<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"1254\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/cypress-dashboard.jpg\" alt=\"cypress e2e testing\" class=\"wp-image-11250\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>Click on E2E testing and select the browser, and finally execute the test case from the list. In our case, we are executing the test case \u2018<strong><em>loginAndTabFunctionality.cy.js<\/em><\/strong>\u2019<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"809\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image3.jpg\" alt=\"cypress test execution\" class=\"wp-image-11263\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>When we click on <strong><em>loginAndTabFunctionality.cy.js <\/em><\/strong>test case starts executing, and finally both test cases pass successfully.<\/p>\n\n\n\n<p>Below are the screenshots of the test case execution locally.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"977\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image19.jpg\" alt=\"cypress test case execution locally\" class=\"wp-image-11266\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"1018\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image14.jpg\" alt=\"cypress test cases\" class=\"wp-image-11265\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"1030\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image20.jpg\" alt=\"test case execution locally on cypress\" class=\"wp-image-11264\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"971\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/image5.jpg\" alt=\"cypress testing\" class=\"wp-image-11267\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Run Test case in Headless Mode<\/strong><\/h3>\n\n\n\n<p>By default, <a href=\"https:\/\/testgrid.io\/blog\/how-to-run-cypress-tests-in-headless-mode\/\" data-type=\"URL\" data-id=\"https:\/\/testgrid.io\/blog\/how-to-run-cypress-tests-in-headless-mode\/\">Cypress runs in &#8220;headless mode<\/a>,&#8221; where the browser runs in the background without opening a visible browser window.<\/p>\n\n\n\n<p>The command-line utility allows you to run Cypress tests seamlessly in CI\/CD pipelines or during automated test executions.<\/p>\n\n\n\n<p>Command to run the test case in headless mode: yarn cypress run or npx cypress run<\/p>\n\n\n\n<p><strong>Run the command:&nbsp;<\/strong><\/p>\n\n\n\n<p>\u2018yarn cypress run &#8211;spec cypress\/e2e\/cypress_testgrid_testcase\/loginAndTabFunctionality.cy.js\u2019. As you execute this command, the test case starts executing in headless mode.&nbsp;<\/p>\n\n\n\n<p>In the screenshot below, you can see that once execution is completed both test cases are passed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"1224\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/cypress-headless-execution.jpg\" alt=\"\" class=\"wp-image-11268\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Execute Cypress Test In The Cloud Using TestGrid<\/strong><\/h2>\n\n\n\n<p>Cypress, a popular JavaScript-based testing framework, has revolutionized E2E testing with its simplicity and efficiency. Combining Cypress with a powerful testing framework such as <strong>TestGrid<\/strong> brings this to a new level by providing enhanced features for executing, monitoring, and scaling tests. In the section below, you will see how to leverage Cypress for E2E testing using <strong>TestGrid<\/strong> and its benefits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Introducing TestGrid<\/strong><\/h2>\n\n\n\n<p>TestGrid is a cloud-based testing platform designed to simplify test execution and management. When integrated with Cypress, it offers powerful Cypress test management capabilities, allowing teams to organize, execute, and monitor automation runs centrally in the cloud.<\/p>\n\n\n\n<p>It offers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cross-Browser and Cross-Device Testing<\/strong>: Test your applications on multiple browsers and devices.<\/li>\n\n\n\n<li><strong>Parallel Execution<\/strong>: Run multiple tests simultaneously to save time.<\/li>\n\n\n\n<li><strong>CI\/CD Integration<\/strong>: Seamlessly integrate with tools like Jenkins, GitLab, or GitHub Actions.<\/li>\n\n\n\n<li><strong>Detailed Analytics<\/strong>: Gain insights into test performance with dashboards and reports.<\/li>\n\n\n\n<li><strong>Scalability<\/strong>: Handle large-scale test executions effortlessly.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why Combine Cypress with TestGrid?<\/strong><\/h3>\n\n\n\n<p>Combining Cypress with TestGrid enhances E2E testing by leveraging the strengths of both tools:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cloud-Based Testing<\/strong>: Execute Cypress tests on a wide range of environments without setting up local infrastructure.<\/li>\n\n\n\n<li><strong>Parallel Execution<\/strong>: Reduce testing time by running tests concurrently on TestGrid\u2019s infrastructure.<\/li>\n\n\n\n<li><strong>Device and Browser Coverage<\/strong>: Ensure your application performs well across different browsers and devices.<\/li>\n\n\n\n<li><strong>Scalability<\/strong>: Easily scale your tests to accommodate growing application needs.<\/li>\n\n\n\n<li><strong>Comprehensive Reporting<\/strong>: Access detailed logs, screenshots, and video recordings of test executions for better debugging.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Set up TestGrid for Cypress testing<\/strong><\/h3>\n\n\n\n<p>Below are the steps for setting up the Testgrid in the cloud<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> The TestGrid platform provides support for the Cypress CLI tool in your custom Cypress test automation project. Select the Cypress feature from the dashboard.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"1999\" height=\"856\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/testgrid-dashboard.png\" alt=\"cypress testing with testgrid\" class=\"wp-image-13358\" style=\"width:737px;height:auto\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 2<\/strong>: Click on the \u201cGenerate TestGrid JSON File\u201d button to create your run environment configuration.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1740\" height=\"864\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/image30.png\" alt=\"cypress testing browser dashboard testgrid\" class=\"wp-image-13359\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong> Fill in the required details:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Project Name<\/strong>, <strong>Build Name<\/strong>, and select browsers from the available options (e.g., Chromium or Firefox).<\/li>\n\n\n\n<li>Choose the desired version and click <strong>\u201cSubmit\u201d<\/strong> to generate the JSON file.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1762\" height=\"1034\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/select-browser-cypress-testgrid-dashboard.png\" alt=\"cypress testing browser choosing testgrid platform\" class=\"wp-image-13360\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 4:<\/strong> Once the <strong>testgrid.json<\/strong> file is downloaded:<\/p>\n\n\n\n<p>Update the Cypress config.file path to match your project.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"928\" height=\"460\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/cypress-config.file_.png\" alt=\"Update cypress config.file path \" class=\"wp-image-13361\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Place the <strong>testgrid.json <\/strong>file in your Cypress project directory. In the screenshot below, you can see the updated <strong>testgrid.json&nbsp;<\/strong><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"654\" height=\"726\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/Cypress-project-directory.png\" alt=\"\" class=\"wp-image-13362\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>Below testgrid.json file with detailed explanation done below of each line<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1602\" height=\"1138\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/testgrid.json_.png\" alt=\"\" class=\"wp-image-13363\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p><strong>Let&#8217;s explain the&nbsp; testgrid.json file in detail<\/strong><\/p>\n\n\n\n<p>Let&#8217;s explain the configuration file for running Cypress tests in an environment (likely provided by TestGrid, a cloud-based test automation platform).&nbsp;<\/p>\n\n\n\n<p>Below is an explanation of each key and its significance:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>domain:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Specifies the base URL of the TestGrid environment where the tests will run.<\/li>\n\n\n\n<li>Value: https:\/\/demo2.testgrid.io\/<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>auth<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Contains the credentials required for authenticating with the TestGrid platform.<\/li>\n\n\n\n<li>username: The email address for logging into TestGrid (Abc@gmail.com).<\/li>\n\n\n\n<li>access_key: A secure access key (API key) for authentication (asdi339jddndhdhd33333j).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>browsers<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Lists the browsers and their versions to be used for testing.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Run_settings<\/strong><\/p>\n\n\n\n<p>This section covers the settings that are required to run the Cypress tests in the cloud.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>cypress_config_file:\n<ul class=\"wp-block-list\">\n<li>Path to the Cypress configuration file used for test setup.<\/li>\n\n\n\n<li>Value: \/Users\/ABC\/Documents\/e2e_testgrid\/cypress.config.js<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>project_name:\n<ul class=\"wp-block-list\">\n<li>Name of the test project.<\/li>\n\n\n\n<li>Value: TestGrid_E2E.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>build_name:\n<ul class=\"wp-block-list\">\n<li>A label for the test build. Helps identify test runs.<\/li>\n\n\n\n<li>Value: TestGrid_E2E_1.0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>specs:\n<ul class=\"wp-block-list\">\n<li>An array of file paths for Cypress test specifications to be executed.<\/li>\n\n\n\n<li>Value:\n<ul class=\"wp-block-list\">\n<li>cypress\/e2e\/1-getting-started\/assertions.cy.js<\/li>\n\n\n\n<li>cypress\/e2e\/1-getting-started\/todo.cy.js<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>exclude:\n<ul class=\"wp-block-list\">\n<li>An array of test files to exclude from the run.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>parallels:\n<ul class=\"wp-block-list\">\n<li>Specifies the number of parallel test executions.<\/li>\n\n\n\n<li>Value: 2, meaning two test instances will run concurrently.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>npm_dependencies:\n<ul class=\"wp-block-list\">\n<li>Placeholder for any Node.js dependencies needed for the test run.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>package_config_options:\n<ul class=\"wp-block-list\">\n<li>Placeholder for additional package configuration options.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>headless:\n<ul class=\"wp-block-list\">\n<li>Indicates whether tests should run in headless mode (without a visible browser UI).<\/li>\n\n\n\n<li>Value: <strong>true<\/strong>, meaning the tests will run in headless mode.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Step 5:<\/strong> Install TestGrid\u2019s Cypress CLI by running one of the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-0b0b6c1d2c512f63aa0bb01a364d65db\"><code>$ npm install -g testgrid-cypress-cli\n\nOR\n\n$ sudo npm install -g testgrid-cypress-cli<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Execute the Cypress Test in the cloud<\/strong><\/h3>\n\n\n\n<p>After installing all dependencies, execute your project using the following Cypress CLI command:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>testgrid-cypress run<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Once the tests have run successfully, you can view the results, including a test summary and step execution video, on the TestGrid dashboard.<\/p>\n\n\n\n<p>In the screenshot below, you can see two test cases, <strong>assertions.cy.js<\/strong>, and <strong>todo.cy.js <\/strong>are executed in Chrome browser 120 and 121<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"1465\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/cypress-test-cases.png\" alt=\"cypress test on the TestGrid dashboard.\" class=\"wp-image-13364\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1999\" height=\"1442\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/cypress-test-automation.png\" alt=\"cypress test automation execution on the TestGrid dashboard.\" class=\"wp-image-13365\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Execute Test Cases In Multiple Browsers<\/strong><\/h3>\n\n\n\n<p>Execute the test cases in <strong>multiple browser<\/strong>s, we have to update the browsers section in the .json file . In the code\/screenshot you can see we have added both Chrome and Firefox browsers with the latest version that Testgrid supports.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-c9b2f7b027b6903c6ddac8c93af70fd8\"><code>\"browsers\": &#91;\n     {\n       \"browser\": \"chrome\",\n       \"versions\": &#91;\n         \"120\",\n         \"121\"\n       ]\n     },\n     {\n       \"browser\": \"firefox\",\n       \"versions\": &#91;\n         \"122\",\n         \"123\"\n       ]\n     }\n   ],<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1628\" height=\"1394\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2025\/01\/cypress-Execute-Test-Cases-In-Multiple-Browsers.png\" alt=\"\" class=\"wp-image-13366\" loading=\"lazy\" title=\"\"><\/figure>\n\n\n\n<p>In the screenshot below, you can see two test cases, assertions.cy.js, and todo.cy.js are executed in Chrome browser with versions 120, 121, and Firefox browser with versions 122, 123.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices for Cypress Test Automation<\/strong><\/h2>\n\n\n\n<p>Every tool has some best practices that can help maximize its effectiveness and efficiency. Below are a few best practices of Cypress.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Avoid Unnecessary Wait :<\/strong><\/h3>\n\n\n\n<p>Suppose you have a scenario where you want to test the visibility of an element after clicking on a button. Instead of using a static <strong>cy.wait()<\/strong> command, which introduces unnecessary waiting time, you can use Cypress commands like cy.get() with assertions to wait for the element to become visible.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>describe('Avoid Unnecessary Waits Example', () =&gt; {\n   it('should check visibility after button click', () =&gt; {\n     \/\/ Visit the webpage containing the button and element\n     cy.visit('https:\/\/example.com')\n      \/\/ Click on the button\n     cy.get('#myButton').click()\n      \/\/ Instead of using cy.wait(), wait for the element to become visible\n     cy.get('#myElement')\n       .should('be.visible')\n       .and('have.text', 'Expected Text')\n   })\n })\n<\/code><\/pre>\n\n\n\n<p><strong>Set baseUrl in the Cypress configuration File<\/strong><\/p>\n\n\n\n<p>Hard-coding the baseUrl using cy.visit() in the before() block of each spec file is not the best approach . With the baseUrl set in the configuration file, Cypress will automatically prepend this base URL to any relative URLs used in your tests.&nbsp;<\/p>\n\n\n\n<p>Here&#8217;s how you can use cy.visit() with a relative URL in your test files:<\/p>\n\n\n\n<p>For example, in your <strong>cypress.config.js <\/strong>file, you can set the baseUrl to the login page URL<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>const { defineConfig } = require(\"cypress\");\n\nmodule.exports = defineConfig({\n e2e: {\n   \"baseUrl\": \"https:\/\/example.com\",\n   experimentalStudio:true,\n   setupNodeEvents(on, config) {\n     \/\/ implement node event listeners here\n   },\n },\n});<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>describe('Login Page Test', () =&gt; {\n   it('should load the login page', () =&gt; {\n     \/\/ Cypress will automatically prepend the baseUrl to this relative URL\n     cy.visit('\/login')\n      \/\/ Add your test assertions here\n   })\n })<\/code><\/pre>\n\n\n\n<p><strong>Multiple assertions per test in Cypress<\/strong><\/p>\n\n\n\n<p>Writing a single assertion in a test can make it run more slowly and lead to problems with performance. The recommended practice is to add multiple assertions with a single command, which speeds up testing and improves test clarity and organization. In the example below, you can see we have multiple validations in a single command.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>cy.get('.element')\n .should('have.class', 'active')\n .and('be.visible')\n .and('have.text', 'Hello World');<\/code><\/pre>\n\n\n\n<p><strong>Isolate it() blocks<\/strong><\/p>\n\n\n\n<p>Isolating <strong>it()<\/strong> blocks is indeed a best practice in Cypress (as well as in other testing frameworks). Each <strong>it()<\/strong> block should be independent and self-contained, with its own setup and teardown steps. This ensures that tests can run independently of each other, making them more robust and reliable.<\/p>\n\n\n\n<p>Here&#8217;s how you can isolate <strong>it() <\/strong>blocks in Cypress:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>describe('Test Suite', () =&gt; {\n   beforeEach(() =&gt; {\n     \/\/ Common setup steps before each test\n     cy.visit('\/'); \/\/ Navigate to the page\n     \/\/ Other setup steps as needed\n   });\n    afterEach(() =&gt; {\n     \/\/ Common teardown steps after each test\n     \/\/ Cleanup tasks, if any\n     \/\/ We can add logout test case here \n \n   });\n    it('Test Case 1', () =&gt; {\n     \/\/ Test steps for the first scenario\n     cy.get('.element').should('have.class', 'active');\n     cy.get('.element').should('be.visible');\n   });\n    it('Test Case 2', () =&gt; {\n     \/\/ Test steps for the second scenario\n     cy.get('.other-element').should('have.text', 'Hello World');\n     cy.get('.other-element').click();\n     \/\/ Additional assertions and interactions\n   });\n });\n<\/code><\/pre>\n\n\n\n<p><strong>Keeping test data separate<\/strong><\/p>\n\n\n\n<p>Keeping test data separate is indeed a best practice in Cypress. Separate data files are easier to update and manage, especially when dealing with a lot of test data. You can reuse the same test data across multiple tests if applicable<\/p>\n\n\n\n<p>Cypress allows you to define test data in external JSON files called fixtures. This keeps your test code clean and separates concerns between test logic and test data.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>\/\/ Example fixture file: test-data.json\n{\n   \"user\": {\n     \"username\": \"testuser\",\n     \"password\": \"password123\"\n   }\n }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>\/\/ Example usage in a test\nbeforeEach(() =&gt; {\n  cy.fixture('test-data.json').then((data) =&gt; {\n    this.userData = data.user;\n  });\n});<\/code><\/pre>\n\n\n\n<p><strong>Use data- Attributes for Stable Selectors<\/strong><\/p>\n\n\n\n<p>Adding attributes to UI elements such as data-test, data-testid, or data-cy increases application testability and reduces dependency on selectors, making the test stable.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>\/\/ Avoid\nconst myBtn = cy.contains('button', 'Click Me')\nmyBtn.click()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>\/\/ Use\ncy.contains('button', 'Click Me').as('myBtn')\ncy.get('@myBtn').click()<\/code><\/pre>\n\n\n\n<p>Also, read this detailed article to <a href=\"https:\/\/testgrid.io\/blog\/cypress-best-practices\/\" data-type=\"link\" data-id=\"https:\/\/testgrid.io\/blog\/cypress-best-practices\/\">Level up your Cypress testing with these best practices<\/a><strong>!<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion&nbsp;<\/strong><\/h2>\n\n\n\n<p>This blog post provided a comprehensive introduction to Cypress, a modern and powerful end-to-end testing framework. In short, Cypress software testing delivers a complete ecosystem for building, executing, and managing automated browser tests efficiently. It covered the fundamentals of Cypress, its benefits, and best practices for writing and executing test cases efficiently. With its ease of use and robust features, Cypress is an excellent choice for automating web applications and ensuring a seamless user experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the complexity of software applications growing every day, one of the most important things for every testing team to focus on is optimizing test coverage. Testers all over the world use testing tools and frameworks to conduct various kinds of testing. However, testing software systems from end-to-end along with all of their subsystems is [&hellip;]<\/p>\n","protected":false},"author":32,"featured_media":13368,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[209,579],"tags":[],"class_list":["post-11245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-test-automation","category-guide"],"acf":[],"images":{"medium":"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/cypress-test-automation.jpg","large":"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2024\/04\/cypress-test-automation.jpg"},"_links":{"self":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/11245","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\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/comments?post=11245"}],"version-history":[{"count":13,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/11245\/revisions"}],"predecessor-version":[{"id":17193,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/11245\/revisions\/17193"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/media\/13368"}],"wp:attachment":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/media?parent=11245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/categories?post=11245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/tags?post=11245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}