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

Colin L. wrote:
[…]

I cannot check that they actually will be hidden when printing. Is
there
any way to do this?

Several ideas come to mind.

  • Use JavaScript to check that display:hidden is properly set.
  • Generate a PDF file with the browser’s Print command, then parse it
    somehow.
  • The same, but start with a reference rendering, get a checksum from
    the reference rendering, and raise a flag if the checksum changes.

I hope these are a shove in the right direction…

Any suggestions will be appreciated.

Colin

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

2009/5/8 Marnen Laibow-Koser [email protected]

Colin L. wrote:
[…]

I cannot check that they actually will be hidden when printing. Is
there
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
    somehow.
  • 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.

Colin

2009/5/9 Marnen Laibow-Koser [email protected]

Colin L. wrote:
[…]

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

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

Hmm, gotta think about that some more. But there should be a way.

  • Generate a PDF file with the browser’s Print command, then parse it
    somehow.
  • 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?

Sure. Look at Selenium, and at its integration with Webrat and Rails.

Would I want to?

That’s another question. It might make your tests too slow, but it’s
worth a shot!

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

I don’t think this is impossible, but I do think that you may be better
off just checking for appropriate CSS class names in the DOM. I’m not
sure.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]