【问题标题】:Running cy.request on beforeEach在 beforeEach 上运行 cy.request
【发布时间】:2023-03-07 18:28:01
【问题描述】:

我正在尝试在我的 cypress 框架上设置一种方法,以便在每个测试场景中执行一些操作,其中包括 cy.request。

我的beforeEach.js 文件位于以下位置:

import { Actor, Action } from "cypress-screenplay";
import * as auth from "../../../../../support/ui/auth";

new Actor().perform(auth.uiLogin);

(Actor 对象最终执行 cy.request)通过 API 执行登录)。

然后通过最简单的测试,我收到以下消息:

The following error originated from your test code, not from Cypress.

  > Cannot call cy.request() 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.

我还能如何解决这个问题?

我不想在我的黄瓜测试的另一个步骤中包含 beforeEach 的内容,因为它会增加很多噪音(每次测试一行,100 次测试...)

【问题讨论】:

    标签: cypress cypress-cucumber-preprocessor


    【解决方案1】:

    beforeEach() 是一个钩子——一个函数调用——而不是一个文件。在/cypress/support/index.js添加

    import { Actor, Action } from "cypress-screenplay";
    import * as auth from "./ui/auth";  
    
    beforeEach(() => {
      new Actor().perform(auth.uiLogin)
    })
    

    这将在每次测试之前运行您的登录。

    一个问题,剧本的用法

    const actor = new Actor();
    

    在测试的顶部,登录的演员应该是被测试的同一演员。

    您或许可以改用自定义命令,

    /cypress/support/index.js

    import { Actor, Action } from "cypress-screenplay";
    import * as auth from "./ui/auth";  
    
    Cypress.Commands.add('loginNewActor', () => {
      const actor = new Actor()
      actor.perform(auth.uiLogin)
      return actor
    })
    

    测试

    let actor
    
    beforeEach(() => {
      actor = cy.loginNewActor()
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-04
      • 2018-12-25
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      相关资源
      最近更新 更多