【问题标题】:Test with cypress an custom HTTP Headers使用 cypress 测试自定义 HTTP 标头
【发布时间】:2019-02-15 04:47:58
【问题描述】:

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

使用 curl,很简单,比如:

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

但是我不知道在cypress中给谁设置header和cookie:

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

【问题讨论】:

  • 你能用cy.request()代替cy.visit()吗?
  • 是的,我可以使用它。

标签: cypress


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-10-06
    • 1970-01-01
    • 2011-04-27
    • 2012-02-24
    • 2014-07-10
    • 2017-05-29
    • 1970-01-01
    • 2012-01-17
    • 2012-09-22
    相关资源
    最近更新 更多