# Testing alternate css for printing

**URL:** <https://www.ruby-forum.com/t/testing-alternate-css-for-printing/167509>\
**Category:** Rails\
**Created:** [May 8, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/testing-alternate-css-for-printing/167509 "2009-05-08T18:51:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 8, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/testing-alternate-css-for-printing/167509/1 "2009-05-08T18:51:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 8, 2009, 7:01pm UTC](https://www.ruby-forum.com/t/testing-alternate-css-for-printing/167509/2 "2009-05-08T19:01:43Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 8, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/testing-alternate-css-for-printing/167509/3 "2009-05-08T21:24:39Z")

</div>

2009/5/8 Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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’.

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 9, 2009, 12:50pm UTC](https://www.ruby-forum.com/t/testing-alternate-css-for-printing/167509/4 "2009-05-09T12:50:35Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 9, 2009, 11:20am UTC](https://www.ruby-forum.com/t/testing-alternate-css-for-printing/167509/5 "2009-05-09T11:20:57Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
