laurentj / slimerjs

A scriptable browser like PhantomJS, based on Firefox
http://slimerjs.org
Other
3k stars 259 forks source link

SlimerJs and CasperJs captureContent property issue #690

Open veterkh opened 6 years ago

veterkh commented 6 years ago

versions

Steps to reproduce the issue

I'm using casperjs with slimerjs in order to retrieve response body, cause phantomjs didn't support it. I'm bit confused where to place captureContent property, and tried all the variants, but still can't get body of response in callback.

var casper = require('casper').create({
    pageSettings: {
        captureContent: [ '/.*/' ]
    },
    captureContent: [ '/.*/' ]
});

casper.start();
casper.captureContent = [ '/.*/' ];
casper.page.captureContent = [ '/.*/' ];

casper.thenOpen('https://google.com', function() {
    casper.on('resource.received', function onKeywordResponseReceived(resource) {
        if (resource.stage === 'end') {
            console.log(resource.url);
            console.log(JSON.stringify(resource));
            console.log(JSON.stringify(resource.body));
        }
    });

    this.wait(10000, function(){});
});

casper.run(function runCasper() {
    casper.exit();
});

Run it in the following way:

casperjs --engine=slimerjs downloadTest.js

Actual results:

https://ssl.gstatic.com/gb/js/sem_7e6c732dccf2b05588c7c99408d0d47f.js 
{"id":10,"url":"https://ssl.gstatic.com/gb/js/sem_7e6c732dccf2b05588c7c99408d0d47f.js","time":"2018-03-30T18:12:51.820Z","headers":[{"name":"accept-ranges","value":"bytes"},{"name":"vary","value":"Accept-Encoding, Origin"},{"name":"content-encoding","value":"gzip"},{"name":"content-type","value":"text/javascript"},{"name":"content-length","value":"20670"},{"name":"date","value":"Wed, 14 Mar 2018 07:03:49 GMT"},{"name":"expires","value":"Thu, 14 Mar 2019 07:03:49 GMT"},{"name":"last-modified","value":"Fri, 09 Mar 2018 04:15:00 GMT"},{"name":"x-content-type-options","value":"nosniff"},{"name":"server","value":"sffe"},{"name":"x-xss-protection","value":"1; mode=block"},{"name":"cache-control","value":"public, max-age=31536000"},{"name":"age","value":"1422542"},{"name":"alt-svc","value":"hq=\":443\"; ma=2592000; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=\":443\"; ma=2592000; v=\"42,41,39,35\""},{"name":"X-Firefox-Spdy","value":"h2"}],"bodySize":56811,"contentType":"text/javascript","contentCharset":"","redirectURL":null,"stage":"end","status":200,"statusText":"OK","referrer":"https://www.google.com/","isFileDownloading":false,"body":"","httpVersion":{"major":2,"minor":0}} 
"" 

Expected results:

https://ssl.gstatic.com/gb/js/sem_7e6c732dccf2b05588c7c99408d0d47f.js 
{"id":10,"url":"https://ssl.gstatic.com/gb/js/sem_7e6c732dccf2b05588c7c99408d0d47f.js","time":"2018-03-30T18:12:51.820Z","headers":[{"name":"accept-ranges","value":"bytes"},{"name":"vary","value":"Accept-Encoding, Origin"},{"name":"content-encoding","value":"gzip"},{"name":"content-type","value":"text/javascript"},{"name":"content-length","value":"20670"},{"name":"date","value":"Wed, 14 Mar 2018 07:03:49 GMT"},{"name":"expires","value":"Thu, 14 Mar 2019 07:03:49 GMT"},{"name":"last-modified","value":"Fri, 09 Mar 2018 04:15:00 GMT"},{"name":"x-content-type-options","value":"nosniff"},{"name":"server","value":"sffe"},{"name":"x-xss-protection","value":"1; mode=block"},{"name":"cache-control","value":"public, max-age=31536000"},{"name":"age","value":"1422542"},{"name":"alt-svc","value":"hq=\":443\"; ma=2592000; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=\":443\"; ma=2592000; v=\"42,41,39,35\""},{"name":"X-Firefox-Spdy","value":"h2"}],"bodySize":56811,"contentType":"text/javascript","contentCharset":"","redirectURL":null,"stage":"end","status":200,"statusText":"OK","referrer":"https://www.google.com/","isFileDownloading":false,"body":"BODY OF RESPONSE WITH JS FILE","httpVersion":{"major":2,"minor":0}} 
"BODY OF RESPONSE WITH JS FILE" 

Js files was given for example, in test workaround.

veterkh commented 6 years ago

Parser eats the text, off course captureContent is set to [ '/.NO_SPACE*NO_SPACE/' ] just remove NO_SPACE

veterkh commented 6 years ago

Ok, the reason of empty body was too stupid. Just need to remove single quotes , cause we need to bypass clear JS regex in array, like this: [ /./ ]. And by the way, proper way to set captureContent settings is the following: casper.page.captureContent = [ /./ ] and put it AFTER casper.start(); not obvious, really

laurentj commented 6 years ago

@veterkh Does your last message mean that your problem is fixed?