I'm currently experimenting with using Cypress to test a large Angular application that I've developed. What I want to achieve is to load an expectation file into my test and then run the test based on this expectation file.
Despite trying different combinations of cy.readFile()
, cy.fixture()
, and even using axios
to load the file via HTTP, I haven't been successful so far.
The main problem seems to be that I can't utilize these methods outside of the it()
function, which prevents me from looping through the data to create the individual tests. I am attempting something similar to the following... Is this feasible in Cypress? Am I overlooking something obvious?
Assume my expectation file looks like this:
{
"mainPage": [1, 2, 3],
"otherPage": [4, 5, 6]
}
I aim for my code to load it and navigate through the various pages:
describe(`Testing the application`, function() {
cy.readFile("path/to/expectation.json").then(function(expectation) {
Object.keys(expectation).forEach(function(pageName) {
it(`for page ${pageName}`, function() {
gotoPage(pageName);
var pageData = getDataFromPage();
expect(pageData).to.equal(expectation[pageName]);
})
})
})
})
To me, this seems like quite a common scenario, so I'm puzzled as to why it's proving to be challenging :)