gpt4 book ai didi

node.js - Cypress:未捕获 CypressError:无法在运行测试之外调用 "cy.get()"

转载 作者:太空宇宙 更新时间:2023-11-04 01:17:43 26 4
gpt4 key购买 nike

当运行我的 cypress 测试时,它会跳过测试并立即运行 th after hooks。然后它会记录此错误消息并完成:

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.

任何想法可能导致这种奇怪的行为。它应该首先运行测试,然后运行后 Hook 。我正在使用 cypress-cucumber-preprocessor 并运行一个功能文件。

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

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

最佳答案

我找到了这个问题的解决方案。

我正在使用页面对象模型,并在我的构造函数中执行了 cy.get 来查看我们是否可以找到 anchor 元素。

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

现在我将所有 cy.gets 放在构造函数之外。

也许有更好的方法来解决这个问题。

关于node.js - Cypress:未捕获 CypressError:无法在运行测试之外调用 "cy.get()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60349326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com