How use --config with CasperJS

2019-05-24 11:28发布

问题:

I've read the Casper documentation and they say that ALL PhantonJS cli options is available on CasperJS but I am trying to use --config=/path/to/config.json and is not working.

Is there a way to fix this or do something similar since I don't want to be editing the configs on terminal.

casperjs --config=config.json test.js

My config.json file:

{
    "load-images" : false
}

My test.js file:

var casper = require('casper').create();

casper.start('http://www.example.com/', function(){
    this.capture('image.png');
})

.run();

That code above is loading the images. Of course this is a simple example and what I want to is manage lots of options inside the config file.

Thanks

回答1:

This issue shows the inconsistencies:

--disk-cache => diskCacheEnabled
--load-images => autoLoadImages
--local-storage-path => offlineStoragePath
--local-storage-quota => offlineStorageDefaultQuota
--local-to-remote-url-access => localToRemoteUrlAccessEnabled
--web-security => webSecurityEnabled
--debug => printDebugMessages

But autoLoadImages is broken. I just verified that webSecurityEnabled works as expected.

PhantomJS version 1.9.7.


A workaround would be to include the following code in every test file:

if (casper.cli.has("config2")) {
    var config = require(casper.cli.get("config2"));
    casper.options.pageSettings.loadImages = config.autoLoadImages;
}

and call it with the other command line flag

casperjs --config=config.json --config2=config.json test.js