Sometimes the users might need to print the website pages either in paper or to a PDF file. The site should look good and make sense. When the user clicks the "Print" option, the browser applies the screen CSS styles plus any specific media: print
styles. For example, you might remove the underlines from the links when printing the page:
1 | /* Remove Hyperlinks */ |
Sometimes you want to hide the elements, like navigation, since they make no sense in the printed view. In the app below, the "Home" link should be hidden when the user prints the results:
We can quickly check how the printed page is going to look by opening Chrome DevTools and running the "Emulate CSS print media type" command:
Tip: once you emulate the CSS print media, it stays this way until you close the DevTools or disable the emulation by running the "Do not emulate CSS media type":
In this blog post I will show how to emulate the print media in Cypress tests.
The application changes
Instead of custom CSS classes, I used Tailwind for the project. Thus hiding the footer element was simple: I applied the print:hidden built-in class.
1 | <footer class="pb-2 pt-2 print:hidden"> |
The tests
We can set emulate CSS media in Cypress tests by sending Chrome Debugger Protocol commands from the Emulation domain. I wrote the cypress-cdp plugin to make running CDP commands simpler.
1 | // https://github.com/bahmutov/cypress-cdp |
Because the CSS print media remains in force, I reset it before each test since most of the tests in the spec assume the screen media.
1 | // https://github.com/bahmutov/cypress-cdp |
Tip: if I wanted, I could use my OSS Cypress visual testing plugin bahmutov/cypress-toy-visual-testing to do even more testing of the printed styles.