[Webtest] pdf download - can I examine this using webtest?
Marc Guillemot
Marc Guillemot <mguillemot@yahoo.fr>
Mon, 02 Jul 2007 14:03:23 +0200
Hi John,
"application/x-download" is not really a correct mime type for a pdf
file and your developers would probably be better inspired to use the
Content-Disposition header if they want to propose the file for download
(rather than direct opening in the browser).
Due to this incorrect headers you have to customize WebTest to allow it
to "see" these files as PDF. For instance with a small Groovy script
(take care to use the latest build, this doesn't work with 2.5):
<groovy>
import com.gargoylesoftware.htmlunit.*;
import com.canoo.webtest.plugins.pdftest.htmlunit.PDFPage;
import com.canoo.webtest.plugins.pdftest.htmlunit.pdfbox.PdfBoxPDFPage;
class MyPageCreator extends DefaultPageCreator
{
public Page createPage(
final WebResponse webResponse,
final WebWindow webWindow )
throws IOException
{
String contentType = webResponse.getContentType().toLowerCase();
if ("application/pdf".equals(contentType) ||
"application/x-download".equals(contentType))
{
PDFPage newPage = new PdfBoxPDFPage(webResponse, webWindow);
webWindow.setEnclosedPage(newPage);
return newPage;
}
else
return super.createPage(webResponse, webWindow);
}
}
step.context.webClient.pageCreator = new MyPageCreator()
</groovy>
(as always, my mailer doesn't compile any groovy code and doesn't warn
about type errors, missing imports, ... ;-))
Marc.
John and Pip wrote:
> Hi Michael and all,
>
> If I click it by hand in a browser it gives me the browsers download
> (open/save) file dialog.
>
> In the webtest results file it gives:
> <step>
> <parameter name="description" value="step: check can click option to
> print boarding pass"/>
> <parameter name="label" value="Print Boarding Pass. "/>
> <parameter name="resultFilename"
> value="response_1182372842528_clickLink.unknown"/>
> <parameter name="taskName" value="clickLink"/>
> <result>
> <completed duration="2703"/>
> </result>
> </step>
>
> And the funny thing is that if I open up
> response_1182372842528_clickLink.unknown it actually displays the pdf
> file in my browser, so I guess I'm closer than I thought to getting it
> to do what I want - but how to get it to treat this as a pdf so I can
> examine it in webtest validate steps?
>
> I've just checked and the mime type and content headers etc are
> deliberately setup so the browser thinks that it is a generic file
> rather than a pdf as they want the user to be offered the choice to
> download or open the file rather than it being by default opened in the
> browser.
>
> So I guess the question is whether there is some way I can tell WebTest
> to treat it as a pdf?
>
> I inserted your code to write the log and the output was only:
>
> Test Start
> com.gargoylesoftware.htmlunit.WebClient@1383942
> Test End
>
> regards,
> John
>
> On 6/20/07, *Michael Habbert* <Michael.Habbert@netpioneer.de
> <mailto:Michael.Habbert@netpioneer.de>> wrote:
>
> Hi John and Pip,
>
>
> [...]
> > > The site I am testing has a number of pdf documents
> (tickets etc)
> > > generated on the fly. However, they are generated as
> downloads rather
> > > than as pdf links.
> > >
> > > When I use the normal webtest pdf syntax (admittedly new
> to me)
> > like so:
> > > <clickLink
> label="${Txt_AltText_PrintBoardingPass}" />
> > > <pdfVerifyText text="Traveller Info"/>
> > > I get the error
> > > filename="C:\working\cwt\doc\isis\testTest.xml" line="133"
> > > message="Current response is not a PDF page but has following
> > mime type
> > > application/x-download (
> > > com.gargoylesoftware.htmlunit.UnexpectedPage@4b0cbc)">
> > >
> > > which makes sense when I think about it.
> > [...]
> [...]
>
> May be you can identify the UnexpectedPage by using something like:
>
> > <groovy>
> > def logFile = new File('./webtest.log')
> > logFile.append("\nTest Start\n")
> > logFile.append(step.context.webClient.toString())
> > logFile.append("\nTest End\n")
> > </groovy>
>
> at the right place. So we are able to see what unexpected page shows
> up. Btw. what happens if you
> click it by hand?
>
> --
> Mit freundlichen Grüßen
>
> Michael Habbert
>
>