Horje
Configuration of cypress.config.js File in Cypress

Cypress, a powerful end-to-end testing framework, has evolved its configuration approach to enhance flexibility and control. The cypress.config.js file is now the cornerstone of Cypress configuration, replacing the older JSON-based setup. This article explores effectively setting up and utilizing this JavaScript configuration file to optimize your Cypress testing environment.

By mastering the cypress.config.js configuration, QA engineers and developers can create more robust and efficient test suites. Whether you’re new to Cypress or migrating from the previous JSON configuration, understanding these configuration techniques is crucial for leveraging the full power of Cypress in your CI/CD pipeline.

Let’s dive into the essentials of configuring Cypress using the cypress.config.js file, empowering you to enhance your automated testing workflow.

What is the cypress.config.js File?

The cypress.config.js file is the chief configuration file of Cypress. It is difficult to the older cypress.json file and enables the usage of better options by which you can manage your test settings (configuration options). For the methodology of this guide, we assume Cypress 10+ and the new JavaScript configuration file format.

Default Configuration

When you initialize Cypress, a basic cypress.config.js file is created by default. Next, I want you to see the code.

JavaScript
const { defineConfig } = require('cypress');

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
  },
});

Override default values

Cypress presents a set of default configurations, and you can replace them in the cypress.config.js.

Below are some of the options you might want to override:

Configuration Option

Default Value

Use

baseUrl

” “

Set the base URL for your application.

viewportWidth

1000

Set the default width of the browser window.

viewportHeight

660

Set the default height of the browser window.

specPattern

“cypress/e2e/**/*.cy.{js,jsx,ts,tsx}”

Define the pattern for locating test files.

supportFile

“cypress/support/e2e.js”

Specify the support file that loads before each test.

retries.runMode

0

Number of times to retry failed tests in run mode.

retries.openMode

0

Number of times to retry failed tests in open mode.

video

true

Enable or disable video recording of test runs.

screenshotOnRunFailure

true

Enable or disable screenshots on test failure.

defaultCommandTimeout

4000

Time, in ms, to wait for assertions.

execTimeout

60000

Time, in ms, to wait for a system command to finish.

pageLoadTimeout

5000

Time, in ms, to wait for a page to load.

setupNodeEvents

function (on, config) {}

Define Node event listeners.

It looks like after override for custom use :

JavaScript
const { defineConfig } = require('cypress');

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:3000',
    viewportWidth: 1280,
    viewportHeight: 720,
    retries: {
      runMode: 2,
      openMode: 1,
    },
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
  },
});

Actual Test in cypress.config.js File in Cypress

Involving test performance in Cypress digital vendor front-end testing, by its nature, consists of creating real test scripts in JavaScript. The files that are stored under the scripts and the cypress.config.js file must have the specPattern field properly set and connected to a specific folder. The text provided a general idea of the scripts which I know you can modify because a spec is actually the name of a file again either in the same or not the same folder as the spec. This folder is the target for the test where the scripts are identified.

Example Test Script

Here is a very simple test script that illustrates a script:

JavaScript
describe("GeeksforGeeks Tests", () => {
  it("Visits the GeeksforGeeks homepage", () => {
    cy.visit("/tag");
    cy.contains("GeeksforGeeks");
  });
});


Output:

Configuration-of-cypressconfigjs-File-in-Cypress

Configuration of cypress.config.js File in Cypress Output

Disable Overriding Default Configurations

If you want to use the default configurations and not override them, simply set the properties you don’t want to override to their default values or omit them entirely from the config file.

Note: Make certain that the crucial settings are elaborated and approved by your team, so no unintentional configurations are ever made. You cannot really physically lock the settings or configurations, but you may opt to use the team’s consistent methods of working and code reviews, for this.

Conclusion

To customize your testing environment in Cypress, the cypress.config.js file must be configured. This environment provides the capacity to introduce various options like base URLs, viewport sizes, and retry strategies, thereby improving the efficiency and accuracy of your tests. The settings are important and should be understood and used properly for the testing framework to be tailored to the needs of your projects.

You can optimize the framework in such a way that it would be easily understood and executed if you learn the configuration settings of a particular tool. It is thus ensured that a project is not hindered by the testing configuration. People should also guarantee that their settings are all well-documented and that these are also reviewed by their fellow programmers so that the chances of the code for the tests that are not elicited.

Frequently Asked Questions on Configuration of cypress.config.js File in Cypress

How do I change the base URL for my tests?

You can need to alter the baseUrl property in the cypress.config.js file to change the base URL: baseUrl: ‘http://localhost:3000’

How can I set different viewports for different tests?

The direct way to make viewport settings different for individual test scripts is to override the settings through the use of Cypress commands: cy.viewport(1024, 768);

Is it possible to disable video recording?

Yes, videotaping is not the only option available. If you do not want to, you can just simply put the video property to false in the cypress.config.js file: video: false

What happened to cypress.json?

cypress.json has been replaced by cypress.config.js for greater flexibility and control.

Can I have multiple configuration files?

There is no feature of having multiple configuration files in Cypress as a typical way of doing things.




Reffered: https://www.geeksforgeeks.org


Software Testing

Related
Architecture and Environment Setup in Cypress Architecture and Environment Setup in Cypress
Assertions in Cypress Assertions in Cypress
Top Bug-Tracking Tools For Developers in 2024 Top Bug-Tracking Tools For Developers in 2024
Cookies in Cypress Cookies in Cypress
How to think like a Tester? How to think like a Tester?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
20