I am trying to run a test, from first, it will get x-auth-token
and I am storing that token within txt file, and then getting that token and storing in my commands.js
file, like this:
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);
});
});
Whenever I ran this command, I am getting following error:
But, if I directly add token there, it runs fine. I don't want to follow that practice. What's the solution for this.
x-auth-token type is promise
"x-auth-token":
cy.readFile("cypress/user_data/token.txt").then((token) => {
cy.log(token)
})
change code
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
},
});