skip to Main Content

I’m trying to create a folder to save the screenshots I capture from Cypress in a custom folder inside screenshots, but it’s not working. The problem is that when the code is executed, at the end it changes the route that I put and leaves the one that is by default.

The code is:

folderName = 'ROX';

var datetime = new Date();
datetime = datetime.toISOString().slice(0, 19).replace('T', '_');
datetime = datetime.replace(/:s*/g, '_');

screenshotsFolder = __dirname + "/cypress/screenshots/" + folderName + '/' + datetime + '/';
console.log("It should be: ", screenshotsFolder);

let results = await cypress.run({
    browser: 'chrome',
    configFile: __dirname + '/cypress.config.js',
    //spec: __dirname + '/cypress/e2e/investigacion/testWeb.cy.js',
    reporter: "cypress-multi-reporters",
    reporterOptions: {
        "reporterEnabled": "mochawesome",
        "mochawesomeReporterOptions": {
            "reportDir": "cypress/reports/" + folderName + '/' + datetime + "/json/",
            //"reportDir": "cypress/reports/json/",
            "overwrite": false,
            "html": false,
            "json": true
        }
    },
    videosFolder: __dirname + '/cypress/videos',
    screenshotsFolder: screenshotsFolder,
    //screenshotsFolder: __dirname + "/cypress/screenshots/",
});

console.log("But it is this", results.config.screenshotsFolder);

Output:

It should be:  C:UsersxeomDesktopAyudantiav2_scriptscript/cypress/screenshots/ROX/2023-05-08_16_13_27/

But it is this C:UsersxeomDesktopAyudantiav2_scriptscriptcypressscreenshots

How can I change it?

2

Answers


  1. screenshotsFolder should be in the config section

    let results = await cypress.run({
        browser: 'chrome',
        configFile: __dirname + '/cypress.config.js',
        //spec: __dirname + '/cypress/e2e/investigacion/testWeb.cy.js',
        reporter: "cypress-multi-reporters",
        reporterOptions: {
            "reporterEnabled": "mochawesome",
            "mochawesomeReporterOptions": {
                "reportDir": "cypress/reports/" + folderName + '/' + datetime + "/json/",
                //"reportDir": "cypress/reports/json/",
                "overwrite": false,
                "html": false,
                "json": true
            }
        },
        config:{
           videosFolder: __dirname + '/cypress/videos',
           screenshotsFolder: screenshotsFolder
        }
    });
    

    code source

    Login or Signup to reply.
  2. You need to use the start function from the Cypress API and also you can use path.join method for defining the folder path.

    I have just updated your existing code , You can try with this :-

    const path = require('path');
    
    folderName = 'ROX';
    
    var datetime = new Date();
    datetime = datetime.toISOString().slice(0, 19).replace('T', '_');
    datetime = datetime.replace(/:s*/g, '_');
    
    screenshotsFolder = path.join(__dirname, 'cypress', 'screenshots', folderName, datetime);
    
    console.log("It should be: ", screenshotsFolder);
    
    const cypress = require('cypress');
    const { start } = require('cypress/lib/tasks/plugins');
    
    start({
      onBeforeRun: async (options) => {
        options.config.screenshotsFolder = screenshotsFolder;
      },
    }).then(async () => {
      const results = await cypress.run({
    
        browser: 'chrome',
        configFile: __dirname + '/cypress.config.js',
        reporter: "cypress-multi-reporters",
        reporterOptions: {
            "reporterEnabled": "mochawesome",
            "mochawesomeReporterOptions": {
                "reportDir": "cypress/reports/" + folderName + '/' + datetime + "/json/",
                //"reportDir": "cypress/reports/json/",
                "overwrite": false,
                "html": false,
                "json": true
            }
        },
        videosFolder: __dirname + '/cypress/videos',
      });
      
      
    });
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search