使用赛普拉斯测试自定义HTTP标头

时间:2019-02-13 14:35:16

标签: cypress

我尝试用cypress建立一个测试。我需要为测试设置Cookie和自定义标头字段。

卷曲,很容易,就像:

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

但是我不知道谁在赛普拉斯中设置标题和cookie:

cy.setCookie("aut", "BeUser1")
cy.visit("/some/foo");

1 个答案:

答案 0 :(得分:0)

您可以尝试以下案件吗?在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);
       })
     })
  })