Determine which capture engine is specified. Possible options:
    - casperjs
    - phantomjs
    Based on this value, locate the binary of the capture engine.

    If setting <engine>_CMD is not defined, then
    look up for ``<engine>`` in shell PATH and
    build the whole capture command.

def casperjs_command():
    """
    Determine which capture engine is specified. Possible options:
    - casperjs
    - phantomjs
    Based on this value, locate the binary of the capture engine.

    If setting <engine>_CMD is not defined, then
    look up for ``<engine>`` in shell PATH and
    build the whole capture command.
    """
    method = app_settings['CAPTURE_METHOD']
    cmd = app_settings['%s_CMD' % method.upper()]
    sys_path = os.getenv('PATH', '').split(':')
    if cmd is None:
        for binpath in sys_path:
            cmd = os.path.join(binpath, method)
            if os.path.exists(cmd):
                break
    cmd = [cmd]

    if app_settings['TEST_CAPTURE_SCRIPT']:
        try:
            proc = subprocess.Popen(cmd + ['--version'], **casperjs_command_kwargs())
            proc.communicate()
            status = proc.returncode
            assert status == 0
        except OSError:
            msg = "%s binary cannot be found in PATH (%s)" % (method, sys_path)
            raise ImproperlyConfigured(msg)
        except AssertionError:
            msg = "%s returned status code %s" % (method, status)
            raise ImproperlyConfigured(msg)

    # Add extra CLI arguments
    cmd += app_settings['CLI_ARGS']

    # Concatenate with capture script
    app_path = os.path.dirname(__file__)

    capture = app_settings['CAPTURE_SCRIPT']
    if capture.startswith('./'):
        capture = os.path.join(app_path, 'scripts', capture)

    assert os.path.exists(capture), 'Cannot find %s' % capture
    return cmd + [capture]