Cypress - Build First Test


Once Cypress has been configured, a framework gets created within the project which is automatically visible in the Explorer. The new test file (say FirstTest.spec.js) should be created within the integration folder, as mentioned below.

Integration Folder

Cypress Folder Structure

Let us understand the folder structure in Cypress. The factors that are included in a Cypress folder are explained below −

  • fixtures − Test data in form of key-value pairs for the tests are maintained here.

  • integration − Test cases for the framework are maintained here.

  • plugins − Cypress events (prior and post events to be executed for a test) are maintained here.

  • support − Reusable methods or customized commands, which can be utilised by test cases directly, without object creation are created here.

  • videos − Executed test steps are recorded in the form of videos and maintained here.

  • node_modules − Project dependencies from the npm are maintained in this folder.It is the heart of the Cypress project execution.

  • cypress.json − Default configurations are set in this folder. The values of the current configurations can be modified here, which overrules the default configurations.

  • package.json − Dependencies and scripts for the projects are maintained in this folder.

Structure of a Basic Test

Cypress follows the JavaScript test frameworks (Mocha, Jasmine, and so on). To create a test in Cypress, we have to adhere to the below mentioned framework guidelines −

  • Test suite name has to be provided within the describe function.

  • Test case names within a test suite have to be provided within the same or you have to specify the function.

  • Test steps within a test case have to be implemented inside the it/specify block.

Basic Test Implementation

The basic test implementation can be done by using the following command −

// test suite name
describe('Tutorialspoint Test', function () {
// Test case
   it('Scenario 1', function (){
      // test step for URL launching
      cy.visit("https://www.google.com/");
   });
});

The cy command used above does not require an object invocation. It becomes available by default on installing the node modules.

Test Execution

For execution from the command line, run the command given below −

./node_modules/.bin/cypress run

Here, all the files within the integration folder get triggered.

For execution from the Test Runner, run the command stated below −

./node_modules/.bin/cypress open

Then, click on the spec file that we want to trigger for execution.

To trigger execution for a specific file from command line, run the command mentioned below −

cypress run --spec "<spec file path>"

The following screen will appear on your computer −

Specific File from Command line
Advertisements