Home  Cypress   To install ...

To install Cypress and set up your first test

To install Cypress and set up your first test, follow these steps:

Installation

  1. Node.js Setup: Ensure you have Node.js installed on your machine. You can download it from nodejs.org and follow the installation instructions.

  2. Cypress Installation: Open your terminal and navigate to your project directory.

    npm init -y  # Initialize npm (if you haven't already)
    npm install cypress --save-dev  # Install Cypress as a dev dependency
    

    This command installs Cypress and adds it to your package.json file.

  3. Opening Cypress: Once installed, you can open Cypress by running:

    npx cypress open
    

    This command launches the Cypress Test Runner. Alternatively, you can also use yarn if you prefer:

    yarn add cypress --dev
    yarn cypress open
    

Setting Up Your First Test

  1. Create a Test File: By default, Cypress creates a cypress/integration directory where you can place your test files. Create your first test file here, for example:

    cypress/integration/my_first_test.spec.js
    
  2. Write Your Test: Open my_first_test.spec.js in your code editor and write your first test using Cypress’s API. Here’s a simple example:

    describe('My First Test', () => {
      it('Visits the Cypress example website', () => {
        cy.visit('https://example.cypress.io');
    
        // Assert the page title
        cy.title().should('include', 'Kitchen Sink');
      });
    });
    

    This test visits the Cypress example website and asserts that the page title includes the text "Kitchen Sink".

  3. Run Your Test: Back in your terminal, with Cypress Test Runner open (npx cypress open or yarn cypress open), you will see your test file listed. Click on the test file (my_first_test.spec.js) to run it.

  4. View Test Results: Cypress will open a browser window and execute your test. You can see the test execution in real-time in the Cypress Test Runner interface. If successful, you’ll see green checkmarks indicating passing tests.

Additional Configuration (Optional)

Published on: Jun 28, 2024, 01:19 AM  
 

Comments

Add your comment