简体   繁体   中英

Cypress cannot find the browser when ran from module api

I'm trying to run a pretty simple test through cypress, with chrome , through module-api . I don't want to run the tests through their launcher but from node as I'll start the tests through mocha framework. The tests at least can start with electron, but can't find the elements I'm trying to click. It works with chrome through the launcher - with npx cypress run --browser chrome . Though when I use the module-api I get this error:

Can't run because you've entered an invalid browser name.

Browser: electron was not found on your system or is not supported by Cypress.

Cypress supports the following browsers:

Here's my config:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  fixturesFolder: false,
  e2e: {
      experimentalSessionAndOrigin: true, // multi-domain tests as there could be an Auth0 authentication
      setupNodeEvents(on, config) {
      return {
        browsers: config.browsers.filter(
          (b) => {
              if (b.name == 'chrome') {
                  console.log(b); // print info about the browser
                  return b;
              }
            }
        ),
      }
    }
  },
})

The file that starts the test (eg example-test.js ):

const cypress = require('cypress')

return cypress.run({
  spec: './cypress/e2e/simple-login.cy.js'
}).then((result) => {
    console.log(result);
    process.exit(0);
})

I'm starting it with:

node example-test.js

(the simple-login just enters a password, email and clicks a button and it works when I use cypress run.. ).

The console log prints correctly the information about the browser,though still there's an error. Any idea how this can be fixed?

Your return value in setupNodeEvents() is only returning chrome type browsers, so the error

Browser: electron was not found on your system or is not supported by Cypress

is caused by that.

The other side-effect is that other config like experimentalSessionAndOrigin are being removed.

This is how you can log chrome browsers without changing the config

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  fixturesFolder: false,
  e2e: {
    experimentalSessionAndOrigin: true, 
    setupNodeEvents(on, config) {
      config.browsers.forEach((b) => {
        if (b.name == 'chrome') {
          console.log(b); // print info about the browser
        }
      })
      return config    // NOTE unchanged config
    }
  },
})

Note npx cypress run --browser chrome will work because your filter is allowing chrome browsers to pass, but npx cypress run --browser electron will fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM