Let's imagine a web server that also returns a plain text file. We are already testing the web pages using Cypress test runner, so how can we request and validate the plain text file? What if the returned file is in Markdown format? Let's learn.
🎁 You can find the source code for the application and the Cypress tests in the repo bahmutov/check-text-file-example.
Requesting the file
To download the file we can use the cy.request command.
1 | /// <reference types="cypress" /> |
There is no page in the browser, but we can still see the server's response by clicking on the "REQUEST" command. The response is dumped in the DevTools Console.
We can save the response as a file using the cy.writeFile command.
1 | it('receives the right text file', () => { |
Tip: store the downloaded file as a test artifact on your CI to be able to debug the testing step.
Compare the file text
We can compare the response text with the expected string. We can put the expected string inline into the spec file, or store it as a file in the repository, which we can read using the cy.readFile command.
1 | it('receives the right text file', () => { |
You can also check the file text for specific string or against a regular expression.
1 | it('receives the right text file', () => { |
Tip: Cypress includes a large number of assertions thanks to the bundled Chai library.
Show the file
One thing we are not using during this test is the application iframe itself - it is empty because we are not visiting the site. We cannot use the cy.visit command because the returned content type is NOT text/html
. Thus we need to write the contents into the document ourselves.
1 | it('visits the text file', () => { |
Tip: you can clear the application iframe before writing the new content by visiting the blank page, see the blog post Cypress Tips and Tricks.
The text does not look right - because it does not show the newlines correctly. Let's wrap the text in a <pre>
element.
1 | it('visits the text file', () => { |
Much better.
Now we can use the other Cypress commands like cy.contains to assert the text.
1 | it('visits the text file', () => { |
Markdown files
What if we receive a plain text Markdown file? We can render it using the <pre>
element.
1 | it('visits the Markdown file', () => { |
Markdown file has structure. We can convert Markdown into HTML using a 3rd party library instead of using the <pre>
element when writing the document. Then the test could use the CSS selectors to query the contents better.
1 | const mdToHtml = require('nano-markdown') |
The HTML document from the converted Markdown text certainly looks nice.
Running all tests
When running all tests together, the document.write(...)
command keeps appending to the same document.
You can clear the document before writing HTML using the document.open()
method
1 | .then((html) => { |
We can open the document before each test
1 | beforeEach(() => { |
Alternatively, we could visit the blank page to clear the document before each test. See this tip in the blog post Visit The Blank Page Between Cypress Tests.