Search code examples
rubygoogle-chromeprintingwatirprint-preview

Ruby Watir reference Chrome Print Preview


I'm writing a script to cycle through some links and print to PDF. When you click on one of the links a new window pops up of the document to be printed and on top of that new window Chrome's print preview box pops up.

I'm trying to change the printer and then click the print button.

What's happening with my code however is that watir is only looking at the window html and not the print preview html so the elements are unable to be located.

My code looks like this:

        begin
            browser.window(title: 'Window Title Name').use do
                browser.div(:id => "navbar-scroll-container").button(:class => "destination-settings-change-button").click
                sleep(1)
                browser.element(:title => "CutePDF Writer").click
                browser.button(:visible_text => 'Print').click
            end
        rescue Watir::Exception::NoMatchingWindowFoundException
            retry
        end 

I have tried using just "browser.windows.last.use do" instead of the title name but that also doesn't work.

I know it can only see the underlying window's html because when I puts browser.html the output is that underlying windows html.

If you want to look at the html situation just Ctrl+P this page (in chrome) to see what I mean.


Solution

  • Of course, after posting a question I soon found a sort of answer; basically chrome opens 2 browser windows and the print preview one couldn't be accessed by title (because it had the same title as the underlying one) but could be accessed by browser.window(:index => 1).use do. not sure why browser.windows.last.use do didn't work. Also following that I would have thought browser.window(:index => 2).use do would have been the correct one as index 0 would be the originally opened one, index 1 the popup and index 2 the print preview.