0
0
Cypresstesting~15 mins

Cypress folder structure - Build an Automation Script

Choose your learning style9 modes available
Verify Cypress default folder structure is present after installation
Preconditions (2)
Step 1: Open the project root directory
Step 2: Locate the 'cypress' folder
Step 3: Inside the 'cypress' folder, verify the presence of 'fixtures', 'integration', 'plugins', and 'support' folders
Step 4: Open the 'cypress.json' configuration file in the project root
✅ Expected Result: The 'cypress' folder contains 'fixtures', 'integration', 'plugins', and 'support' folders. The 'cypress.json' file exists in the project root.
Automation Requirements - Cypress
Assertions Needed:
Verify 'cypress' folder exists
Verify 'fixtures', 'integration', 'plugins', and 'support' folders exist inside 'cypress'
Verify 'cypress.json' file exists in project root
Best Practices:
Use cy.task to access Node.js file system for folder and file existence checks
Use explicit assertions with cy.wrap and chai assertions
Keep test code clean and readable
Automated Solution
Cypress
/// <reference types="cypress" />

// In cypress/plugins/index.js add this task to check folder and file existence
module.exports = (on, config) => {
  const fs = require('fs');
  const path = require('path');

  on('task', {
    checkExists({ pathToCheck }) {
      return fs.existsSync(pathToCheck);
    }
  });
};

// Test file: cypress/integration/folder_structure_spec.js

describe('Cypress folder structure verification', () => {
  const projectRoot = Cypress.config('projectRoot');
  const cypressFolder = `${projectRoot}/cypress`;

  it('should have cypress folder', () => {
    cy.task('checkExists', { pathToCheck: cypressFolder }).then(exists => {
      expect(exists).to.be.true;
    });
  });

  ['fixtures', 'integration', 'plugins', 'support'].forEach(subfolder => {
    it(`should have ${subfolder} folder inside cypress`, () => {
      cy.task('checkExists', { pathToCheck: `${cypressFolder}/${subfolder}` }).then(exists => {
        expect(exists).to.be.true;
      });
    });
  });

  it('should have cypress.json file in project root', () => {
    cy.task('checkExists', { pathToCheck: `${projectRoot}/cypress.json` }).then(exists => {
      expect(exists).to.be.true;
    });
  });
});

This test uses Cypress tasks to access the Node.js file system because Cypress commands run in the browser context and cannot directly check files or folders on disk.

In cypress/plugins/index.js, we define a task checkExists that returns true if a given path exists.

The test file folder_structure_spec.js uses this task to verify the presence of the cypress folder, its subfolders fixtures, integration, plugins, and support, and the cypress.json configuration file in the project root.

Each check uses cy.task and asserts the result is true, ensuring the folder structure is as expected.

Common Mistakes - 3 Pitfalls
Trying to check folder existence using only Cypress browser commands
{'mistake': "Hardcoding absolute paths instead of using Cypress.config('projectRoot')", 'why_bad': 'Hardcoded paths may not work on different machines or environments, causing tests to fail.', 'correct_approach': "Use Cypress.config('projectRoot') to get the project root dynamically."}
Not asserting the result of the file existence check
Bonus Challenge

Now add data-driven testing to verify additional custom folders inside the 'cypress' folder, such as 'e2e' and 'support_old', using an array of folder names.

Show Hint