NodeJS Cypress:未捕获Cypress错误:无法在运行测试之外调用“cy.get()”

kknvjkwl  于 2023-02-08  发布在  Node.js
关注(0)|答案(1)|浏览(230)

当运行我的cypress测试时,它跳过测试,直接在钩子后运行。然后它记录这个错误消息并完成:

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.

知道是什么导致了这个奇怪的行为吗?应该先运行测试,然后再运行后钩子。我正在使用cypress-cubber-preprocessor和运行一个特性文件。
下面是我的index.js从支持文件夹(挂钩):

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();            
    });
myzjeezk

myzjeezk1#

我找到了解决这个问题的办法。
我使用页面对象模型,在我的构造函数中,我做了一个cy.get来查看我们是否可以定位一个锚元素。
我在步骤defs中示例化了几个页面,这些页面似乎是在测试运行之前调用的,因此出现了错误。
现在我把所有的cy.gets放在构造函数之外。
也许有一个更好的方法来解决这个问题。

相关问题