javascript 超过最大调用堆栈大小- Cypress

yhuiod9q  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(118)

我尝试运行一个测试,首先,它将获得x-auth-token,我将该令牌存储在txt文件中,然后获得该令牌并存储在我的commands.js文件中,如下所示:

Cypress.Commands.add("getAllNoteIDs", () => {
  let notes_ids = [];
  cy.readFile("cypress/user_data/token.txt").then((token) => {
    cy.log("Token is "+token)
  }) // Here I am getting correct token
  cy.request({
    method: "GET",
    url: "https://practice.expandtesting.com/notes/api/notes",
    form: true,
    failOnStatusCode: false,
    timeout: 30000,
    headers: {
      accept: "application/json",
      "x-auth-token":
      cy.readFile("cypress/user_data/token.txt").then((token) => {
        cy.log(token)
      }),
    },
  }).then((response) => {
    cy.log(response)
    const responseBodyKeys = Cypress._.keys(response.body);
    cy.log(responseBodyKeys)
    cy.log(response.body['message'])
    let results = response.body.data;
    for (let i of results) {
      notes_ids.push(i.id);
    }
    cy.wrap(notes_ids);
  });
});

字符串
每当我运行这个命令,我得到以下错误:

但是,如果我直接在那里添加token,它运行得很好。我不想遵循这种做法。对此有什么解决方案?

5f0d552i

5f0d552i1#

x-auth-token类型为promise

"x-auth-token": 
  cy.readFile("cypress/user_data/token.txt").then((token) => {
    cy.log(token)
  })

字符串
改变代码

cy.readFile("cypress/user_data/token.txt").then((token) => {
  cy.request({
    method: "GET",
    url: "https://practice.expandtesting.com/notes/api/notes",
    form: true,
    failOnStatusCode: false,
    timeout: 30000,
    headers: {
      accept: "application/json",
      "x-auth-token": token // use token
    },
});

相关问题