使用Cypress-Cucumber-Preproperpropersor重复使用之前的步骤。

发布于 2025-01-28 02:20:50 字数 2235 浏览 4 评论 0 原文

我有一个柏树项目,在该项目中,我使用柏树会话API来维护整个功能的会话。

现在,我尝试从不推荐的 klaveens cypress cucumbers cucumber cucumber provocersor a href =“ https://github.com/badeball/cypress-cucumber-preprecessor” rel =“ nofollow noreferrer”> badeball的Cypress Cucumber cucumber预处理。但是我遇到了一个问题。在测试开始之前,我的身份验证发生的 步骤在测试开始之前重复几次。最终,赛普拉斯“从中抢走”并开始运行实际的测试 - 但显然这是非常大量的资源和时间密集的,出了问题。

我的设置:

依赖项:

    "cypress": "^9.6.1",
    "@badeball/cypress-cucumber-preprocessor": "^9.1.3",

index.ts:

beforeEach(() => {
  let isAuthInitialized = false;
  function spyOnAuthInitialized(window: Window) {
    window.addEventListener('react:authIsInitialized', () => {
      isAuthInitialized = true;
    });
  }

  login();
  cy.visit('/', { onBeforeLoad: spyOnAuthInitialized });
  cy.waitUntil(() => isAuthInitialized, { timeout: 30000 });
});

login()函数:

export function login() {
  cy.session('auth', () => {
    cy.authenticate();
  });
}

据我所知,我遵循 cy.session 几乎从字面上看。

我的身份验证命令仅具有特定的应用程序,它确实包括 cy.visit('/') - 之后,将我的应用程序重定向到登录服务(不同的域),然后继续进行。

问题

cy.sessession可以正常工作,它会在第一次尝试中创建一个会话 - 然后每个后续时间都会记录一个有效的会话的成功还原。但这发生了多次,似乎会陷入循环。

截屏:

在我看来,就像 cy.visit()再次触发了 tofe the each()。也许清除一些会话数据(LocalStorage?),这会导致我的身份验证重定向再次发生 - 或者以某种方式使柏树认为测试开始新鲜。但是,当然 theeach()应每次功能一次一次。

我正在查看我的代码更改的差异,除了预处理器更改之外,唯一的区别是:

  • 我的.cypress-cucumber-preprecessorrc.json(我根据 docs
  • 严格
  • 打字更改,此预处理器对打字插件/index.ts文件也更加 根据文档,

我在预处理器中查看一个错误吗?

I have a Cypress project where I use the Cypress session API to maintain a session throughout features.

Now I try switching from the deprecated Klaveness Cypress Cucumber Preprocessor to the replacement, Badeball's Cypress Cucumber Preprocessor. But I am running into an issue; the beforeEach() step where my authentication takes place gets repeated several times before the tests start. Eventually, Cypress "snaps out of it" and starts running the actual tests - but obviously this is very resource and time intensive, something is going wrong.

My setup:

Dependencies:

    "cypress": "^9.6.1",
    "@badeball/cypress-cucumber-preprocessor": "^9.1.3",

index.ts:

beforeEach(() => {
  let isAuthInitialized = false;
  function spyOnAuthInitialized(window: Window) {
    window.addEventListener('react:authIsInitialized', () => {
      isAuthInitialized = true;
    });
  }

  login();
  cy.visit('/', { onBeforeLoad: spyOnAuthInitialized });
  cy.waitUntil(() => isAuthInitialized, { timeout: 30000 });
});

login() function:

export function login() {
  cy.session('auth', () => {
    cy.authenticate();
  });
}

As far as I can see, I follow the docs for cy.session almost literally.

My authenticate command has only application specific steps, it does include a cy.visit('/') - after which my application is redirected to a login service (different domain) and then continues.

The problem

cy.session works OK, it creates a session on the first try - then each subsequent time it logs a succesful restore of a valid session. But this happens a number of times, it seems to get stuck in a loop.

Screenshot:
Repeated before logs

It looks to me like cy.visit() is somehow triggering the beforeEach() again. Perhaps clearing some session data (localstorage?) that causes my authentication redirect to happen again - or somehow makes Cypress think the test starts fresh. But of course beforeEach() should only happen once per feature.

I am looking at a diff of my code changes, and the only difference except the preprocessor change is:

  • my .cypress-cucumber-preprocessorrc.json (which I set up according to the docs
  • typing changes, this preprocessor is stricter about typings
  • plugins/index.ts file, also set up according to the docs

Am I looking at a bug in the preprocessor? Did I make a mistake? Or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2025-02-04 02:20:50

柏树 +黄瓜与预处理器有两个方面,使这种可能使

  1. 柏树的混淆> 10“运行所有规格”行为

,如 gleb bahmutov博士的很棒的博客文章,如果您不配置柏树以其他方式进行,则运行所有Specs 在每个测试之前运行每个钩子。他提出的解决方案是不使用“所有规格”按钮,我发现它过多 - 因为有解决方案;请参阅下面的使用黄瓜预处理器的工作解决方案。

注意:从赛普拉斯10开始,“运行所有规格”不再支持(由于与此不持久性有关的原因)。

  1. Cucumber预处理配置

赛赛赛赛赛赛赛赛赛偶联 nonglobalstepdefinitions ,而是配置特定路径,例如() /blob/master/docs/step-definitions.md“ rel =” nofollow noreferrer“> source ):

  "stepDefinitions": [
    "cypress/integration/[filepath]/**/*.{js,ts}",
    "cypress/integration/[filepath].{js,ts}",
    "cypress/support/step_definitions/**/*.{js,ts}",
  ]
}

它并未明确说明,是其中包括您的挂钩的文件(在我的情况下,我的情况。如果您不希望它们进行每个测试,则应将TS)排除在这些路径之外!我可以看到一个人可能会认为这很明显,但是很容易在此FilePath配置中偶然地包含挂钩的文件。

tldr:如果我排除了我的index.ts文件,其中包括我的stepdefinitions配置中的挂钩,我可以按预期使用“运行所有规格” - 使用 tofereach()在每次测试之前仅运行一次。

There are two aspects of Cypress + Cucumber with preprocessor that make this potentially confusing

  1. Cypress >10 "Run all specs" behaviour

As demonstrated in Gleb Bahmutov PhD's great blog post, if you don't configure Cypress to do otherwise, running all specs runs each hook before each test. His proposed solution is to not use the "run all specs" button, which I find excessive - because there are ways around this; see below for a working solution with the Cucumber preprocessor.

Note: as of Cypress 10, "run all specs" is no longer supported (for reasons related to this unclarity).

  1. Cucumber preprocessor config

The Cypress Cucumber preprocessor recommends to not use the config option nonGlobalStepDefinitions, but instead configure specific paths like (source):

  "stepDefinitions": [
    "cypress/integration/[filepath]/**/*.{js,ts}",
    "cypress/integration/[filepath].{js,ts}",
    "cypress/support/step_definitions/**/*.{js,ts}",
  ]
}

What it doesn't explicitly state though, is that the file which includes your hooks (in my case index.ts) should be excluded from these paths if you don't want them to run for each test! I could see how one might think this is obvious, but it's easy to accidentally include your hooks' file in this filepath config.

TLDR: If I exclude my index.ts file which includes my hooks from my stepDefinitions config, I can use "run all specs" as intended - with beforeEach() running only once before each test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文