Mojo::Phantom - Interact with your client side code via PhantomJS


      use Mojolicious::Lite;
      use Test::More;
      use Test::Mojo::WithRoles qw/Phantom/;
      any '/' => 'index';
      my $t = Test::Mojo::WithRoles->new;
      $t->phantom_ok('/' => <<'JS');
        var text = page.evaluate(function(){
          return document.getElementById('name').innerHTML;
        });, 'Bender', 'name changed after loading');
      @@ index.html.ep
      <!DOCTYPE html>
          <p id="name">Leela</p>
            (function(){ document.getElementById('name').innerHTML = 'Bender' })();


    Mojo::Phantom is the transport backbone for Test::Mojo::Role::Phantom.
    Currently it is used to evaluate javascript tests using PhantomJS,
    though more is possible. Please note that this class is not yet as
    stable as the public api for the test role.


    The upstream phantom.js has been retired in favor of headless chrome. A
    Mojo::Chrome (and related Test::Mojo::Role::Chrome) was planned but has
    been stalled for lack of certain necessary usage documentation that has
    not been forthcoming. While this module will continue to function, just
    know that it depends on a project that is defunct.


    Mojo::Phantom inherits the attributes from Mojo::Base and implements
    the following new ones.


    An array reference containing command-line arguments to be passed
    directly to the PhantomJS process.


    An instance of Mojo::URL used to make relative urls absolute. This is
    used, for example, in setting cookies


    A hash reference used to bind JS methods and Perl functions. Keys are
    methods to be created in the perl object in javascript. Values are
    functions for those methods to invoke when the message is received by
    the Perl process. The functions may be relative to the package or are
    absolute if they contain ::. If the function is false, then the key is
    used as the function name.


    An array reference containing Mojo::Cookie::Response objects.


    The package for binding relative function names. Defaults to main


    An additional string of javascript which is executed after the page
    object is created but before the url is opened.


    A string used to separate messages from the JS side. Defaults to


    A string which is used to build a Mojo::Template object. It takes as
    its arguments the instance, a target url, and a string of javascript to
    be evaluated.

    The default handles much of what this module does, you should be very
    sure of why you need to change this before doing so.


    Do not automatically call phantom.exit() after the provided JavaScript
    code. This is useful when testing asynchronous events.


    Redirect console.log output to TAP as note events. This is usually
    helpful when writing tests. The default is off for Mojo::Phantom and on
    for Test::Mojo::Role::Phantom.


    The executable name or path to call PhantomJS. You may substitute a
    compatible platform, for example using casperjs to use CasperJS.

    Note that while you can use this to specify the full path of an
    alternate version of PhantomJS, during the install of Mojo::Phantom you
    must have phantomjs in your PATH for configuration and testing.


    Mojo::Phantom inherits all methods from Mojo::Base and implements the
    following new ones.


    A lower level function which handles the message passing etc. You
    probably want execute_url. Takes a file path to start phantomjs with
    and a callback.

    Returns a pre-initialized instance of Mojo::Phantom::Process. The end
    user likely does not need to worry about this object, though it might
    be useful if the process needs to be killed or the stream timeout needs
    to be lengthened.


    Builds the template for PhantomJS to execute and starts it. Takes a
    target url, a string of javascript to be executed in the context that
    the template provides and a callback. By default this is the page
    context. The return value is the same as "execute_file".

    The executable name or path to call PhantomJS. You may substitute a
    compatible platform, for example using casperjs to use CasperJS.


    NOTE that if your Perl version does not provide CORE::die and
    CORE::warn, they will be monkey-patched into the CORE namespace before
    executing the javascript.



    Joel Berger, <>


    Graham Ollis (plicease)

    Sebastian Paaske Tørholm (Eckankar)


    Copyright (C) 2015 by "AUTHOR" and "CONTRIBUTORS".

    This library is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.