【问题标题】:I don't want to clear cashe and cookies in cypress我不想清除 cypress 中的缓存和 cookie
【发布时间】:2021-11-28 22:55:07
【问题描述】:

我尝试了很多但都没有成功,请帮我举个例子。我把所有代码都放在这里了,你也可以在你的系统上试试。

我正在使用 cypress 测试注册流程。而且我不想在每次测试之前清除缓存/cookie。谁能帮帮我?

这是我的测试文件,第一个描述块是为输入的电子邮件发送 OTP。第二个是创建一个临时电子邮件并将 OTP 保存到 JSON 文件中以供信件使用。第三个是使用 API 验证 OTP。但是当我使用相同的 URL 并输入一封由 API 验证 OTP 的电子邮件时,它显示 500 Internal server error

const faker = require("faker");
const firstName = faker.Name.firstName();
const lastName = faker.Name.lastName();
const email = firstName + "@mailinator.com";

describe('My Test Suite', function () {
  it('Otp Test', function () {
    cy.visit('https://outsized.site/')
    cy.get('.css-jqdzg6-commonButtonStyle > canvas', { timeout: 30000 }).click()
    cy.get('#email').type(email.toLocaleLowerCase())
    cy.get('.ant-btn').click()
    cy.fixture('data1').then((profile) => {
      profile.FreelancerName = firstName.toLocaleLowerCase()
      profile.FreelancerEmail = email.toLocaleLowerCase()
      cy.writeFile("cypress/fixtures/data1.json", profile)
      cy.wait(2000)
    })
  })
})

context('My Test Suite', function () {
  it('Otp Test', function () {
    cy.visit('https://www.mailinator.com/')
    cy.fixture("data1.json").then(profile => {
      cy.get("#addOverlay").type(profile.FreelancerName)
    })
    cy.get("#go-to-public").click()
    cy.wait(2000)
    cy.contains('table tbody tr', 'OTP').click()  // find the right email

    cy.get('#html_msg_body')  // iframe
      .its('0.contentDocument.body').should('not.be.empty')  // wait for loading
      .then(console.log)  // works with this but errors without - totally weird
      .wait(0)
      .find("table > tbody > tr:nth-child(3) > td > h2")
      .then($h2 => {
        const OTP = $h2.text()
        cy.fixture("data1.json").then(profile => {
          profile.OTP = OTP
          cy.writeFile("cypress/fixtures/data1.json", profile);
        })
      })
  })
})

context('My Test Suite', function () {
  it('Otp Test', function () {
    cy.fixture('data1').then((profile) => {
      cy.request({
        method: 'POST',
        url: 'https://api.outsized.site/graphql',
        headers: {
          'Content-Type': 'text/plain'
        },
        body:
          'mutation { verifyEmailOtp(email: "' + profile.FreelancerName + '@mailinator.com", otp: ' + profile.OTP + '){ message } }'
      })
    })
    cy.wait(5000)
    cy.fixture("data1.json").then(profile => {
      cy.visit("https://outsized.site")
      cy.wait(5000)
      //cy.visit(profile.url+profile.FreelancerName+"%40"+"mailinator.com")
      cy.get('.css-jqdzg6-commonButtonStyle > canvas', { timeout: 30000 }).click()
      cy.get('#email').type(profile.FreelancerEmail)
      cy.get('.ant-btn').click()
      cy.request({
        method: 'POST',
        url: 'https://api.outsized.site/graphql',
        headers: {
          'Content-Type': 'text/plain'
        },
        body:
          'mutation { addNewEmail(email: "' + profile.FreelancerName + '@mailinator.com"){ message } }'
      })
      cy.get('.ant-btn').click()
    })
  })
})

500 Internal server errorget 因为 cypress 在每次测试前都有清除缓存和 cookie。

【问题讨论】:

  • 你需要简化一点,因为它的代码不容易遵循。错误发生在哪里?为什么你认为它与 cookie cleardown 有关?错误状态码500与此不一致。
  • 正如我所说,第一个描述块是为输入的电子邮件发送 OTP。第二个是创建一个临时电子邮件并将 OTP 保存到 JSON 文件中以供信件使用。第三个是使用 API 验证 OTP。在我使用相同的 URL 并输入一封由 API 验证 OTP 的电子邮件后,它显示 500 Internal server error

标签: api local-storage cypress session-cookies session-storage


【解决方案1】:

有一个相对较新的命令 cy.session() (docs) 可以保留 cookie、localStorage 和 sessionStorage。不确定这是否包括“缓存”,实际上我不知道你指的是什么。

它的工作方式是您可以将它添加到 beforeEach() 中,这样它就会被每个测试调用,但它只调用一次内部的代码(第一次测试),然后对于后续调用,它会保留并从以上是在第一次测试时设置的商店。

这里有一个例子Unable to access modal-dialogue in cypress,比官方文档中的例子简单。

基本模式值得重复

Cypress.config('experimentalSessionSupport', true)  // set this flag

beforeEach(() => {
  cy.session('mySession', () => {

    // code that sets cookies, only called once
    // thereafter same cookies, localstorage, sessionStorage
    // are preserved for future test

  })
})

我无法从上面的示例中确定您需要什么代码,但我相信您已经知道了。

【讨论】:

  • 我有多个域,所以我需要使用不同的 describe 块。那我该怎么办?
  • 也许这就是你得到500 Internal server error的原因,如果你从cookie中丢失凭据,这不是你得到的状态码。那会给你401 (Unauthorized) status code
  • 有什么办法吗?
  • 多个域与describe 块无关,所以是的,只需添加它,看看是否修复它。
  • 我听不懂。你能描述一下吗?
猜你喜欢
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 2021-11-20
  • 2017-06-25
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多