gpt4 book ai didi

cypress - 使用 cypress 测试自定义 HTTP header

转载 作者:行者123 更新时间:2023-12-03 23:53:04 24 4
gpt4 key购买 nike

我尝试用 cypress 构建一个测试。我需要为测试设置一个 cookie 和一个自定义标题字段。

使用 curl,这很容易,例如:

curl -H "aut: BeUser1"  --cookie "aut=BeUser1" ....

但我不知道在 cypress 中设置 header 和 cookie 的是谁:
cy.setCookie("aut", "BeUser1")
cy.visit("/some/foo");

最佳答案

你可以试试你的情况如下吗?创建一个 login()在 commands.js 中并使用 cy.request()登录系统并添加标题。还有 before测试,我已经按照给定的方式运行了设置的 cookie cy.setCookie("cookie", "your cookie details here");

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);
})
})
})

关于cypress - 使用 cypress 测试自定义 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54672695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com