Reputation: 83768
Is there a way to download a file in PhantomJS (or CasperJS) without going through the QTWebKit client?
I would like to download, from a URL unrelated to the page being tested, data encoded in JSON.
It is not apparent from the PhantomJS API or CasperJS API how one would do this, as their functionality seems geared towards opening the URL in the spooky client.
I would expect that one could simply run something like:
fetch(URL, callback);
The ordinary course of downloading to the PhantomJs page then to the client is problematic for two reasons. First, it's slow and cumbersome. Second, it violates the same-origin policy (so the CasperJS base64encode function will not work).
At its most basic, I expect that the javascript runner for the PhantomJS client would implement XMLHttpRequest (or something like it), but I wonder if something a little more canonical would be available.
Edit
Perhaps one could just include jQuery and use $.ajax
, but unfortunately that throws an same-origin exception as well:
XMLHttpRequest cannot load http://example.com:5000/test. Origin file:// is not allowed by Access-Control-Allow-Origin.
The only solution I can come up with is to download the file I want to access in JSON in a separate process (e.g. with wget), but since PhantomJS does not permit execution of processes (as of this writing), it would seem that it would have to be initiated separately from PhantomJS.
Upvotes: 1
Views: 1367
Reputation: 3811
You could try looking at
casper.download(String url, String target[, String method, Object data])
from http://docs.casperjs.org/en/latest/modules/casper.html#download
I'm unsure from the question what you would want to do with the downloaded data, however you could then use PhantomJS' injectJS()
or includeJS()
and pass in the local file in order to use that data in the page being tested.
Upvotes: 2