繁体   English   中英

赛普拉斯抛出未定义的授权承载并且未传递令牌

[英]Cypress throws Authorisation Bearer undefined and not passing the token

在运行Cypress测试时,收到The response we received from your web server was: > 401: Unauthorized收到The response we received from your web server was: > 401: Unauthorized并且cypress在错误详细信息中显示Authorization": "Bearer undefined" 。在调试过程中,beforeEach> loginToPortal()方法返回有效令牌不知道为什么令牌没有传递给deleteTestCompanies方法,如果有人可以对这个问题有所了解,那将有很大的帮助?

以下是收到的错误

我们发送的请求是:

Method: GET
URL: https://somesite.com/api/Companies
Headers: {
  "Connection": "keep-alive",
  "Authorization": "Bearer undefined",
  "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
  "accept": "*/*",
  "accept-encoding": "gzip, deflate"
}


beforeEach(
        "Login to web Portal and Navigate to some Page",
        function() {
          cy.server();
          cy.loginToPortal().then((response) => {        
            cy.deleteTestCompanies(this.bearerToken);        
            cy.setTokens({ token: this.bearerToken });
          });
          cy.visit("/stateprofile");
        }
      );



  support/index.js
   import "./commands";
   import "./apiCommands";

//以下函数位于“ apiCommands.js”文件中;

Cypress.Commands.add("getCompanies", token => {
  cy.request({
    url: "/api/Companies",
    headers: {
      Authorization: `Bearer ${token}`
    }
  }).then(response => {
    var matchedCompany = [];
    const companies = response.body;
    companies.forEach(function(element, index) {
      if (companies[index].name.includes("test-")) {
        matchedCompany.push(companies[index].id);
      }
    });
    cy.wrap(matchedCompany).as("testCompanies");
  });
});



  Cypress.Commands.add("deleteTestCompanies", token => {
  cy.getCompanies().then(companies => {
    companies.forEach(function(element, index) {
      cy.request({
        method: "DELETE",
        url: `/api/companies/${companies[index]}`,
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-type": "application/json"
        }
      });
    });
  });
});

我已经在cy.getCompanies(token)方法中传递了token ,并解决了问题。 另外console.log("InsideCompanies:" +token)循环内的令牌。

Cypress.Commands.add("deleteTestCompanies", token => {
  cy.getCompanies(token).then(companies => {
    companies.forEach(function(element, index) {
    console.log("InsideCompanies:" +token);
      cy.request({
        method: "DELETE",
        url: `/api/companies/${companies[index]}`,
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-type": "application/json"
        }
      });
    });
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM