【问题标题】:Reference Error :cy is not defined while running cypress testReferenceerror :cy 在运行 cypress 测试时未定义
【发布时间】:2023-03-22 05:41:01
【问题描述】:
Describe('The Login', function () {
    beforeEach(function () {
        cy.readFile(Cypress.env('test')).as("user")});

    it("Login", () => {
        cy.get("@user").then((user) =>
        cy.login(user.username, user.password))})

失败 cypress/integration/login.spec.js ● 登录页面 ›

ReferenceError: cy is not defined

  at Object.<anonymous> (cypress/integration/login.spec.js:4:5)
      at new Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

【问题讨论】:

  • 您需要输入更多细节,例如要重现的代码、来自控制台的错误。
  • 添加了详细信息

标签: cypress


【解决方案1】:

要为@sai 的答案添加一些上下文,要解决这个 eslint 错误,我所要做的就是安装 eslint-plugin-cypress from here。

npm install eslint-plugin-cypress --save-dev

然后,在我的 eslint 配置中,我在扩展数组中添加了一行。

您的配置可以是 .eslintrc.json 或 package.json 中的“eslintConfig”配置对象

{
  "extends": [
    "plugin:cypress/recommended"
  ]
}

【讨论】:

    【解决方案2】:

    在本地测试,这应该适合你。更改 json 文件的路径,并使用自定义命令 cy.login。在我的示例中,我只是将变量打印到控制台。

    describe('The Login', () => {
      beforeEach(() => {
          cy.readFile('cypress/integration/test/test.json').as("user")});
      it("Login", () => {
          cy.get("@user").then((user) =>
          cy.log(user.username, user.pass))
        })
      })
    

    我的 test.json 是:

    {
      "username":"test@test.com",
      "pass":"pass"
    }
    

    【讨论】:

      【解决方案3】:

      问题与 eslint 有关 必须安装几个软件包并更新 eslintrc 文件

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-19
      • 2021-09-21
      • 2019-09-25
      • 2018-07-20
      • 1970-01-01
      • 2020-03-09
      • 2021-04-11
      • 1970-01-01
      相关资源
      最近更新 更多