Writing Your First Test Script in Cypress

 Cypress is a modern, fast, and reliable end-to-end testing framework designed for web applications. Unlike traditional Selenium-based tools, Cypress runs directly in the browser, giving developers and testers an easier way to write and debug tests. Ready to get started? Here’s how to write your first Cypress test script step by step.

📌 What is Cypress?

Cypress is an open-source testing tool built for the modern web. It supports writing tests in JavaScript or TypeScript, with powerful features like automatic waiting, time-travel debugging, and a rich interactive test runner.

📌 Installing Cypress

First, install Cypress in your project with npm:

npm install cypress --save-dev

To open the Cypress Test Runner for the first time, run:

npx cypress open

Cypress will create a default folder structure, including /cypress/e2e for your test files.

📌 Writing Your First Test

Create a file named first_test.cy.js inside cypress/e2e/. Add the following script:

describe('My First Test', () => {

  it('should visit the Cypress documentation and check the page title', () => {

    cy.visit('https://docs.cypress.io'); // Navigate to the URL

    cy.title().should('include', 'Cypress'); // Assert that the page title contains "Cypress"

    cy.get('header').should('be.visible'); // Check that the header element is visible

  });

});

📌 Understanding the Script

✅ describe() groups related test cases.

✅ it() defines a single test case with a description.

✅ cy.visit() navigates to a URL.

✅ cy.title() gets the page title, and .should() is used for assertions.

✅ cy.get() selects DOM elements; here, we check if the header is visible.

📌 Running the Test

After saving the script, launch the Cypress Test Runner:

npx cypress open

You’ll see your first_test.cy.js file listed. Click it to run the test in an interactive browser window. Cypress will execute each step, highlight elements, and display the test results in real-time.

📌 Conclusion

You’ve just written and run your first Cypress test! Cypress’s intuitive syntax and powerful interactive runner make writing and debugging tests faster and easier. As you advance, you can add more commands, create custom commands, and integrate Cypress with your CI/CD pipeline for continuous testing.

Learn Testing Tools Training Course

Read More:

Creating Test Scripts in Selenium

How to Use JIRA for Bug Tracking

Test Case Management Tools You Should Learn

What is QTP/UFT and How Does It Work?

Visit Quality Thought Training Institute

Get Direction

 

Comments

Popular posts from this blog

DevOps vs Agile: Key Differences Explained

Regression Analysis in Python

Tosca Installation Guide for Beginners