您可以尝试以下情况吗?在 commands.js 中创建一个login() 并使用cy.request() 登录系统并添加标题。还有before 测试,我已经按照给定的cy.setCookie("cookie", "your cookie details here"); 运行设置cookie
Cypress.Commands.add("login", () => {
cy.request({
method: 'POST',
form: true,
url: 'your-url-here',
headers: {
'Content-Type': 'text/html',
'aut' : 'BeUser1',
},
body: {
"email": "your email",
//"username": "your username", depends upon your system login you could use email or username
"password": "your password",
}
}).then(response => {
const target = response.body.email;
})
})
稍后在测试中我使用cy.getCookie('cookie') 来生成cookie。
describe('Set header and cookie', function() {
before('set cookie',function(){
cy.setCookie("cookie", "add your your cookie here");
});
it.only('tests login', function() {
cy.login();
cy.getCookie('cookie')
.then((cookie) => {
console.log(cookie);
})
})
})