我不想清除柏树中的卡西和饼干



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

我测试使用柏树登记流。我不想在每次测试前清除缓存/cookie。有人能帮我吗?

这是我的测试文件,第一个描述块是为输入的电子邮件发送OTP。第二种方法是创建一个临时电子邮件,并将OTP保存到一个JSON文件中以供信件使用。第三个是使用API验证OTP。但是当我使用相同的URL并输入一个OTP由API验证的电子邮件时,它显示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 error得到,因为cypress在每次测试前都清除了缓存和cookie。

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

它的工作方式是您可以将其添加到beforeEach()中,以便每个测试调用它,但它只调用一次内部代码(第一次测试),然后对于后续调用,它保留并恢复在第一次测试期间设置的上述存储的值。

这里有一个例子,无法访问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
})
})

我不能从上面的示例中判断出你需要什么代码,但我相信你已经知道了。