In the previous post here and here, we saw how to write BDD test cases using cucumber along with Cypress and to use datatables. As we saw in those examples, we are hard coding few data in feature files. This is not a best practise since we need to modify the feature file for every new set of data. Assuming we need to have different data in different environments, this will make it hard to run the tests across various test environment. Let us look at how we can make read these data from a file outside of feature file.
Cypress provides two options to read external files. They are readFile and Fixtures. In this blog post, let us look into readFile method and how to use it.
According to documentation, the command syntax is as below
1 2 3 4
Let us write a new scenario to read both text file and json file. We then assert the content of the files.
Corresponding Step definition will look like below. Here we get information from datatable and assert the text file content as is. For JSON files, cypress yields a JSON object . Hence we convert the expected text to json object and assert on its properties.
Now it is time to run above scenario. Open Cypress by running command npx cypress open
Run the scenario on cypress UI and result will look like below. We can clearly see that assertions are passed.