user1709356
user1709356

Reputation: 335

Cypress: Uncaught CypressError: Cannot call "cy.get()" outside a running test

When running my cypress test it skips the test and rund th after hooks straight away. It then logs this error msg and finishes:

Uncaught CypressError: Cannot call "cy.get()" outside a running test.

This usually happens when you accidentally write commands outside an it(...) test.

If that is the case, just move these commands inside an it(...) test.

Check your test file for errors.

https://on.cypress.io/cannot-execute-commands-outside-test

This error originated from your test code, not from Cypress.

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

Cypress could not associate this error to any specific test.

We dynamically generated a new test to display this failure.

Any ideas what could be causing this strange behaviour. It should run the test first then run the after hooks. I am using cypress-cucumber-preprocessor and running a feature file.

Below is my index.js from support folder (for hooks):

import '@applitools/eyes-cypress/commands';
import './commands';

const xhrData = [];

    //Hooks
    before(function () {
        cy.fixture('./TestData').as('TestData');                       

        // runs once before all tests in the block
        cy.server({
            // Here we hanDle all requests passing through Cypress' server
            onResponse: (response) => {
            if (Cypress.env('record')) {
                const url = response.url;
                const method = response.method;
                const data = response.response.body;
                // We push a new entry into the xhrData array
                xhrData.push({ url, method, data });
            }
            }
        });

        // This tells Cypress to hook into any GET request
        if (Cypress.env('record')) {
            cy.route({
            method: 'GET',
            url: '*',
            });
            cy.route({
                method: 'POST',
                url: '*',
                });
        }

        // Load stubbed data from local JSON file
        if (!Cypress.env('record')) {
            cy.fixture('fixture')
            .then((data) => {
                for (let i = 0, length = data.length; i < length; i++) {
                    cy.route(data[i].method, data[i].url, data[i].data);
                }
            });
        }
    });

    beforeEach(function () {        

        cy.visit(Cypress.config().baseUrl);
        // runs before each test in the block
        cy.eyesOpen({
            appName: 'CafeTownsend',
            testName: 'Complete Happy Path',
            browser: {
                "viewportWidth": 1000,
                "viewportHeight": 660
              },
          });                       
    });

    after(function () {
        // runs once after all tests in the block        
        if (Cypress.env('record')) {
            const path = './cypress/fixtures/fixture.json';
            cy.writeFile(path, xhrData);
            cy.log("Wrote "+ xhrData.length +" XHR responses to local file "+path);
        } 
    });

    afterEach(function () {
        // runs after each test in the block
        cy.eyesClose();
        cy.reload();            
    });

Upvotes: 3

Views: 12371

Answers (1)

user1709356
user1709356

Reputation: 335

I found the solution to this issue.

I was using page object model and in my constructors I did a cy.get to see that we could locate an anchor element.

I instantiate several pages in the step defs, and these seemed to be called before the test is run, hence the error.

For now I am putting all cy.gets outside of the constructor.

perhaps there is a bettwe way to solve this issue.

Upvotes: 2

Related Questions