0

我正在尝试在我的 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 次测试中......)

4

1 回答 1

0

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()
})
于 2021-08-19T20:33:02.910 回答