【发布时间】: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