Cypress: Some Errors


Error “Blocked a frame with origin … from accessing a cross-origin frame”

Problem

We the following cypress script:

describe('example1', () => {
  beforeEach(() => {
    cy.visit('http://localhost:3000')
  })

  it('Test the button', () => {
    cy.log('Test');

    // home page
    cy.contains('Home').click()
    cy.location('pathname').should('eq', '/')
    cy.wait(3000);
    cy.go('back')

    // about page
    cy.contains('About').click()
    cy.location('pathname').should('eq', '/about')
    cy.go('back')

    // contact page
    cy.contains('Contact').click()
    cy.location('pathname').should('eq', '/contact')
    cy.go('back')
  })
})

If we execute this test we get the following error:

Blocked a frame with origin “http://localhost:3000” from accessing a cross-origin frame

Cause

The error “Blocked a frame with origin “…” from accessing a cross-origin frame” is issued when we try to leave our domain, in this case localhost, because during a cypress test we are not allowed to leave the domain which we have visited for testing.

In our case this happens because we are trying to go back from http://localhost:3000 which we just have visited and whatever this “back” domain is, its not part of localhost domain.

Resolution

Make sure that you are not leaving the domain where you are running your test.

Can’t run because no spec files were found

Problem

We use Cypress version 12.2.0, and have cypress configuration and package files in one folder (e. g. /home/userx/cypress_root) but the cypress tests are in a separate folder/path (e.g. /home/userx/myapplication). We try to run the following cypress command in WSL using the pwsh prompt or in pure Windows PowerShell:

yarn cypress run --env SomeVariable='somevalue' --spec '../myapplication/cypress/e2e/*.cy.ts' --browser chrome

And we receive the following error:

PS /home/userx/cypress_root> yarn cypress run --env SomeVariable='somevalue' --spec '../myapplication/cypress/e2e/*.cy.ts' --browser chrome
yarn run v1.22.19
$ /home/userx/cypress_root/cypress/node_modules/.bin/cypress run --env SomeVariable='somevalue'--spec '../myapplication/cypress/e2e/*.cy.ts' --browser chromeCan't run because no spec files were found.

We searched for specs matching this glob pattern:

  > /home/userx/myapplication/cypress/e2e/*.cy.ts
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Potential Resolution

Use the following command and run it directly in bash and not in WSL pwsh and also not in pure Windows PowerShell!

yarn cypress run --env SomeVariable='somevalue' --config '{"specPattern":["../myapplication/cypress/e2e/*.cy.ts"]}' --browser chrome

before:run and after:run hooks in cypress.config.js are not firing

Problem

Cypress hook ‘after:spec’ in cypress.config.js is firing but the hooks ‘before:run’ and ‘after:run’ are not firing.

The problem was observed using using Cypress ver 11.2.0 with the following configuration:

cypress.config.js:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  experimentalInteractiveRunEvents: true,
  reporter: 'cypress-multi-reporters',
  reporterOptions: {
    reporterEnabled: 'cypress-mochawesome-reporter, mocha-junit-reporter',
    cypressMochawesomeReporterReporterOptions: {
      reportDir: 'cypress/reports',
      charts: true,
      reportPageTitle: 'My Test Suite',
      embeddedScreenshots: true,
      inlineAssets: true,
    },
    mochaJunitReporterReporterOptions: {
      mochaFile: 'cypress/results/test-results.[hash].xml',
      testCaseSwitchClassnameAndName: false,
    },
    video: true,
  },
  e2e: {
    setupNodeEvents(on, config) {
		
      on('before:run', details => {
        console.log("Before run hook")
      })

      on('after:run', (results) => {
        console.log("After run hook")
      })
	  
      on('after:spec', (spec, results) => {
        if (results && results.stats.failures !== 0) {
          console.log(`${spec.name}`);
        }
      })
      return require('./cypress/plugins/index.js')(on, config)
    }
  }
})

cypress/plugins/index.js:

/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = (on, config) => {
	// `on` is used to hook into various events Cypress emits
	// `config` is the resolved Cypress config

	require('cypress-mochawesome-reporter/plugin')(on);

	on('after:run', (test, runnable) => {
		console.log("After run hook")
	});

	// IMPORTANT to return the config object
	// with the any changed environment variables
	return config
}

Potential Resolution

After the following changes to cypress.config.js the affected hooks were firing:

cypress.config.js:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  experimentalInteractiveRunEvents: true,
  reporter: 'cypress-multi-reporters',
  reporterOptions: {
    reporterEnabled: 'cypress-mochawesome-reporter, mocha-junit-reporter',
    cypressMochawesomeReporterReporterOptions: {
      reportDir: 'cypress/reports',
      charts: true,
      reportPageTitle: 'My Test Suite',
      embeddedScreenshots: true,
      inlineAssets: true,
    },
    mochaJunitReporterReporterOptions: {
      mochaFile: 'cypress/results/test-results.[hash].xml',
      testCaseSwitchClassnameAndName: false,
    },
    video: true,
  },
  e2e: {
    setupNodeEvents(on, config) {
		
      on('before:run', (test, details) => {
        console.log("Before run hook");
      });

      on('after:run', (test, runnable) => {
        console.log("After run hook")
      });
	  
      on('after:spec', (spec, results) => {
        if (results && results.stats.failures !== 0) {
          console.log(`${spec.name}`);
        }
      })
      return require('./cypress/plugins/index.js')(on, config)
    }
  }
})

The task ‘printFailedTestMessage’ was not handled in the setupNodeEvents method

Problem

After migrating from Cypress 10.1.0 to Cypress 11.2.0 and related changes to cypress.config.js we are facing the following error during a Cypress run:

...
The task 'printFailedTestMessage' was not handled in the setupNodeEvents method. The following tasks are registered: unlinkSync, ctrLogMessages, ctrLogFiles

Fix this in your setupNodeEvents method here:
/cypress-workdir/cypress.config.js

https://on.cypress.io/api/task

Because this error occurred during a `after each` hook we are skipping all of the remaining tests.
      at <unknown> (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:146162:80)
      at tryCatcher (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:11327:23)
      at Promise._settlePromiseFromHandler (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:9262:31)
      at Promise._settlePromise (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:9319:18)
      at Promise._settlePromise0 (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:9364:10)
      at Promise._settlePromises (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:9440:18)
      at _drainQueueStep (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:6034:12)
      at _drainQueue (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:6027:9)
      at Async.../../node_modules/bluebird/js/release/async.js.Async._drainQueues (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:6043:5)
      at Async.drainQueues (http://localhost:xxxxx/__cypress/runner/cypress_runner.js:5913:14)
  From Your Spec Code:
      at savePageIfTestFailed (webpack:///./node_modules/cyclope/src/index.js:257:0)
      at Context.eval (webpack:///./cypress/support/e2e.js:38:4)



[mochawesome] Report JSON saved to /cypress-workdir/cypress/reports/.jsons/mochawesome.json

setupNodeEvents: after:spec: spec.name cypress/e2e/current_tests/test1.cy.js failed.
setupNodeEvents: after:spec: spec.relative cypress/e2e/current_tests/test1.cy.js failed.
setupNodeEvents: after:spec: spec.absolute /cypress-workdir/cypress/e2e/current_tests/test1.cy.js failed.
...

Potential Resolution

Remote the Cypress package cyclope from your package.json file.