Suppose there is a JSON file named data.json on Github. The raw view of the file can be accessed through a URL like this: https://raw.githubusercontent.com/data.json (Please note that this URL is fictional and not real).
Assume that the URL contains JSON data structured as follows:
"users_1": [
{
"id": 1234,
"name": "Bob"
},
{
"id": 5678,
"name": "Alice"
}
]
How can I extract the entire JSON data from this URL and store it in a variable within a Cypress test? Cypress does not typically use Promises, which makes implementation challenging. In Typescript, my attempts so far look like this:
let users; // I want to store JSON data from the URL in this variable
const dataUrl = "https://raw.githubusercontent.com/data.json";
cy.request(dataUrl).then((response) => {
users = JSON.parse(response); // This results in an error due to the type of response being Cypress.Response<any>
})
For my upcoming project where I transition from Protractor to Cypress, I intend to perform similar tasks. In a Protractor test, I extract JSON data from a Github file and store it in a variable using a Promise. I aim to achieve the same functionality with Cypress.