简体   繁体   中英

CasperJS via CLI: How to load external JS files?

This might be a stupid question (CasperJS noob): given this example from the CasperJS docs:

// cow-test.js
casper.test.begin('Cow can moo', 2, function suite(test) {
    var cow = new Cow();
    test.assertEquals(cow.moo(), 'moo!');
    test.assert(cow.mowed);
    test.done();
});

If Cow() is defined in a file \\path\\to\\myCowClass.js , how do I load this class when I am using CasperJS via CLI? Is this a job for the files config param or for clientScripts ?

I would really love if anybody had a concise tutorial/example.

Let's take your Cow.js file. I assume it looks like this:

function Cow() {
  this.mooed = false;
}

Cow.prototype.moo = function () {
  this.mooed = true;
  return 'moo!';
}

This file should be a dependency of your test. Here you can:

  • Inject your "class" file from the command line using the includes option
  • Inject your "class" file from your test file using phantom.injectJs

With --includes

casperjs test --includes=/path/to/Cow.js cow-test.js

With phantom.injectJs

// cow-test.js
phantom.injectJs('/path/to/Cow.js');

casper.test.begin('Cow can moo', 2, function suite(test) {
  var cow = new Cow();
  test.assertEquals(cow.moo(), 'moo!');
  test.assert(cow.mooed);
  test.done();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM