Testing alternate css for printing

I am using an alternate style sheet for printing, loading it via
<%= stylesheet_link_tag ‘print’, :media => :print %>
which is working as expected.

The problem is with testing, I would like to test that the layout is working correctly, in my controller functional tests, but I cannot see how to do this as, if I understand correctly, the style sheet selection change is made by the browser. I can check, for example, that items to be hidden are enclosed in divs of the appropriate class so that they should be hidden, but I cannot check that they actually will be hidden when printing. Is there any way to do this?

Any suggestions will be appreciated.


Colin Law wrote:


I cannot check that they actually will be hidden when printing. Is


any way to do this?

Several ideas come to mind.

  • Use JavaScript to check that display:hidden is properly set.

I cannot see how to integrate this into an automated test.

  • Generate a PDF file with the browser’s Print command, then parse it


  • The same, but start with a reference rendering, get a checksum from

the reference rendering, and raise a flag if the checksum changes.

The same question here, I would like this to be integrated into an automated test, would it be possible to invoke a browser from the rails test environment to do the print to PDF? Would I want to?

Maybe I am asking the impossible, or at least the ‘not worth the effort’.

Many thanks for the suggestions, I think maybe I will just stick with checking the class for the moment. I suppose the only thing that should make it fail would be an accidental edit of the stylesheet or somehow overriding the display property with another style.

Thinking about it there are lots of style related things that are not tested. For example I could intentionally change the colour of some text by changing the colour in the class, with the accidental side effect of hiding some text on another page due to the new colour being the same as (or similar to) the background colour on that page. There is no way that I can see that the fact that the text was now invisible or difficult to read would be picked up by an automated test.