How to import other javascript module in PhantomJS or CasperJS

11,297

Solution 1

As of 1.1, CasperJS relies on PhantomJS’ native require():
https://groups.google.com/forum/#!topic/phantomjs/0-DYnNn_6Bs

Injecting dependencies

While injecting additional modules, CasperJS looks for path relative to cur directory (the place where we run a casperjs command) We can inject dependency using clientScripts option. However the injected dependencies can't use require "globally". They are injected immediately to every page loaded.

casper.options.clientScripts = ["path/relative/to/cur/dir"]

Also we can inject modules using commandline args:

casperjs test --includes=foo.js,bar.js path/to/the/test/file

Using require

To import user modules use:

require "./user-module.coffee"

Then in user modules you can also use require. Using require paths are resolved relative to the current file (where require is called).
If in user module you want to import casper libs, then you need to patch require, check: https://casperjs.readthedocs.org/en/latest/writing_modules.html

Solution 2

After trying a number of the other suggestions (each expected to work in the context of their corresponding environments), hit on this solution:

phantom.page.injectJs( 'script.js');

Solution 3

There's a section about that in the docs

var require = patchRequire(global.require);
require('./user');

In your case you should use global.require since you're using CoffeeScript.

Share:
11,297
Robert Zaremba
Author by

Robert Zaremba

Mission statement: Knowledge is always better than ignorance. Computer Scientist (MSc), IT Solution Architect, specialized in: Go, Solidity, Rust, Python, JavaScript programming OCaml, Emacs Lisp enthusiast Blockchain engineering Cryptoeconomics Data Mining & Analytics Text processing Agile development

Updated on June 13, 2022

Comments

  • Robert Zaremba
    Robert Zaremba almost 2 years

    I'm trying to build a functional test using CasperJS. caseperjs is run by a backend test suite using the following command:

    PHANTOMJS_EXECUTABLE=../client/node_modules/phantomjs/bin/phantomjs  ../client/ext_modules/casperjs/bin/casperjs test ../client/test/functional/init.coffee
    

    In init.coffee I want to import/include other module (file) which seats just next to it. How to do it?

    The following doesn't works:

    require("user")
    

    All I want is to get a content from other file into init.coffee

  • Robert Zaremba
    Robert Zaremba almost 11 years
    Doc says that patchRequire is used in order to allow requiring casper modules using their full name, eg. require('casper') in user modules.
  • Alberto Zaccagni
    Alberto Zaccagni almost 11 years
    See the example after that. Maybe I misunderstood the docs, I didn't try it.
  • Robert Zaremba
    Robert Zaremba almost 11 years
    Yes, I saw it. thanks for this! I'm preparing an answer for this problem.
  • Anders Lindén
    Anders Lindén almost 10 years
    Should it fail silently if the file is not existing?
  • Mark Simon
    Mark Simon almost 10 years
    Soz, haven't tried that. Expecting it not to fail silently.