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.

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 the command line, run the command mentioned below βˆ’

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

The following screen will appear on your computer βˆ’

Leave a Reply