代码之家  ›  专栏  ›  技术社区  ›  qatesterbecomingprogrammer

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

  •  0
  • qatesterbecomingprogrammer  · 技术社区  · 1 年前

    我正在尝试运行一个测试,从一开始,它就会 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);
      });
    });
    

    每当我运行此命令时,我都会收到以下错误: enter image description here enter image description here

    但是,如果我直接在那里添加令牌,它运行良好。我不想遵循那种做法。 解决方案是什么。

    1 回复  |  直到 1 年前
        1
  •  1
  •   Odri    1 年前

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