Node.js 柏树:未捕获的柏树错误:无法调用;cy.get();在运行测试之外

Node.js 柏树:未捕获的柏树错误:无法调用;cy.get();在运行测试之外,node.js,cypress,Node.js,Cypress,当运行我的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 insi

当运行我的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.
你知道是什么导致了这种奇怪的行为吗。它应该先运行测试,然后运行after钩子。我正在使用cypress Cumber预处理器并运行一个功能文件

下面是支持文件夹中的我的index.js(用于挂钩):

import'@applitools/eyes-cypress/commands';
导入“./命令”;
常量xhrData=[];
//钩子
前(函数(){
cy.fixture('./TestData')。as('TestData');
//在块中的所有测试之前运行一次
cy.server({
//这里我们处理通过Cypress服务器的所有请求
onResponse:(response)=>{
if(Cypress.env('record')){
const url=response.url;
const method=response.method;
常量数据=response.response.body;
//我们将一个新条目推送到xhrData数组中
push({url,method,data});
}
}
});
//这告诉Cypress钩住任何GET请求
if(Cypress.env('record')){
赛道({
方法:“GET”,
url:“*”,
});
赛道({
方法:“POST”,
url:“*”,
});
}
//从本地JSON文件加载存根数据
if(!Cypress.env('record')){
cy.fixture(“fixture”)
。然后((数据)=>{
for(设i=0,length=data.length;i
我找到了这个问题的解决方案

我使用的是页面对象模型,在我的构造函数中,我做了一个cy.get,以查看是否可以定位锚元素

我在步骤defs中实例化了几个页面,这些页面似乎是在测试运行之前调用的,因此出现了错误

现在我把所有的cy.get放在构造函数之外


也许有一种更好的方法可以解决这个问题。

您的测试文件怎么样?测试文件是基于小黄瓜的特性文件。步骤定义单独编码请参阅公共代码存储库下载并调试问题:
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();            
    });