【问题标题】:In Cypress, how to setCookie before test? [duplicate]在 Cypress 中,如何在测试前设置 Cookie? [复制]
【发布时间】:2022-01-02 06:25:34
【问题描述】:

如何解决?

赛普拉斯检测到您从命令返回了一个承诺,同时还 在该承诺中调用一个或多个 cy 命令。

返回承诺的命令是:

cy.visit()

你在 promise 中调用的 cy 命令是:

cy.setCookie()

enter image description here

/// <reference types="cypress" />

import { internet, name } from "faker";
import "cypress-localstorage-commands";

const email = internet.exampleEmail();
const password = internet.password();
const first_name = name.firstName();
const last_name = name.lastName();

let user;

before(function registerUser() {
  cy.request("POST", "https://api.domain.io/api/users/", {
    first_name: first_name,
    last_name: last_name,
    email: email,
    password: password,
  })
    .its("body")
    .then((res) => {
      user = res;
    });
});


beforeEach(function setUser() {
  cy.visit("https://app.domain.io/projects/create", {
    onBeforeLoad: (win) => {
      cy.setCookie("token", user.token);
    },
  });
});

describe("JWT", () => {
  it("create project", () => {
    cy.get('[type="text"]').type("Autotest project");
    cy.get('[type="submit"]').click();
  });
});

【问题讨论】:

标签: javascript cypress autotest


【解决方案1】:

我设法以这种方式解决它:

beforeEach(function setUser() {
  cy.setCookie("token", user.token);
  cy.visit("https://app.domain.io/projects/create");
});

【讨论】:

    猜你喜欢
    • 2016-09-23
    • 2018-11-22
    • 2022-06-29
    • 1970-01-01
    • 2019-12-13
    • 2012-12-09
    • 1970-01-01
    • 2017-02-01
    • 2014-08-23
    相关资源
    最近更新 更多