【问题标题】:Using PhantomJS with Karma (Win7 x64)将 PhantomJS 与 Karma 一起使用 (Win7 x64)
【发布时间】:2014-03-08 04:24:37
【问题描述】:

有没有人有关于如何配置 Karma 以使用 PhantomJS 的简单入门指南?

使用 phonecat 示例,我可以在 Chrome 上正常运行 Karma,尽管 Karma 文档提到了 PhantomJS(我现在已经安装),但我不知道如何修改配置文件以使其运行。

我尝试将 PhantomJS 放在 testacular.conf.js 的 browsers 数组中,但我明白了;

 { [Error: spawn OK] code: 'OK', errno: 'OK', syscall: 'spawn' }

我认为这意味着它可以正常启动,但在我看来(作为 PhantomJS 菜鸟)它需要不同的命令行。我也下载了phantomjs-launcher,但是不知道怎么用。

(我正在运行 Windows 7 64 位,如果这有影响的话。)

test.bat

@echo off

REM Windows script for running unit tests
REM You have to run server and capture some browser first
REM
REM Requirements:
REM -NodeJS (http://nodejs.org/)
REM -Testacular (npm install -g karma)

set BASE_DIR= % ~dp0
karma start "%BASE_DIR%\..\config\testacular.conf.js" %*

testacular.conf.js

basePath = '../';

files =[
  JASMINE,
  JASMINE_ADAPTER,
  'app/lib/angular/angular.js',
  'app/lib/angular/angular-*.js',
  'test/lib/angular/angular-mocks.js',
  'app/js/**/*.js',
  'test/unit/**/*.js'
];

autoWatch = true;

browsers =['Chrome', 'phantomjs'];

junitReporter = {
    outputFile: 'test_out/unit.xml',
    suite: 'unit'
};

根据 procmon.exe PhantomJS 根本没有启动,所以为了规避环境问题,我已经修改了我的配置;

browsers = ['Chrome','%USERPROFILE%\\AppData\\Roaming\\npm\\phantomjs.cmd'];

%userprofile% 扩展的地方,似乎启动它,但现在我明白了;

INFO [launcher]: Starting browser %USERPROFILE%\AppData\Roaming\npm\phantomjs.cmd
ERROR [launcher]: Cannot start %USERPROFILE%\AppData\Roaming\npm\phantomjs.cmd
        Can't open 'http://localhost:9876/?id=16572367'

events.js:72
        throw er; // Unhandled 'error' event
          ^
Error: spawn OK
    at errnoException (child_process.js:975:11)
    at Process.ChildProcess._handle.onexit (child_process.js:766:34)

该错误现在似乎来自 PhantomJS.exe。

【问题讨论】:

    标签: angularjs phantomjs karma-runner


    【解决方案1】:

    首先,使用 npm 安装 PhantomJS:

    npm install -g phantomjs

    然后您可能需要为 karma 指定 PhantomJS 可执行文件的位置。 npm install 会告诉你它把可执行文件放在哪里。对我来说,在 Git Bash 中运行 karma,我将以下内容添加到 ~/.profile:

    export PHANTOMJS_BIN ='C:/Users/JohnSmith/AppData/Roaming/npm/node_modules/phantomjs/lib/phantom/phantomjs.exe'`
    

    这两个步骤,加上将 PhantomJS 添加到 browsers 条目,足以让 karma 成功调用 Phantom 并运行我的所有测试。

    【讨论】:

    • 不走运。我之前错过了 npm install 步骤,而是下载了它,所以我想可能就是这样。第一次运行时它抱怨在 9100 发生了端口冲突,但之后每次都遇到与以前相同的错误。
    • 向我们展示你的业力配置文件,我猜?
    • 已添加。我觉得没什么特别的?
    【解决方案2】:

    使用 PhantomJS 启动器并将 env PHANTOMJS_BIN 设置为 phantomjs 二进制文件的正确位置。 在 windows 上是 phantomjs.exe,而不是 .cmd 文件(cmd 文件只是 windows 上的 npm 包装器)。

    在您的代码中,您使用的是脚本浏览器启动器(用于启动浏览器的自定义 shell 脚本)。这是可能的,但脚本必须接受一个参数,即它应该打开的 url。 PhantomJS 的行为并非如此。

    【讨论】:

    • 另外请注意,在即将发布的 Karma (0.10) 版本中,启动器是单独的插件,并且 karma-phantomjs-launcher 使用从 NPM 安装的 phantomjs,因此您实际上不需要做任何事情,它应该即使在名为 Windows 的伟大操作系统上也能正常工作;-)
    【解决方案3】:

    在节点的 child_process.spawn() 函数中使用相对路径时,我看到了这个错误 --

    var spawn = require('child_process').spawn;
    var child = spawn('phantomjs', ['./suspendmonitors.js']);
    

    我的解决方案是使用绝对路径:

    var spawn = require('child_process').spawn;
    var child = spawn('phantomjs', ['C:/Users/kkhalsa/workspace/misc_scripts/phantomjs/suspendmonitors.js']);
    

    由于某种原因,从 Windows 命令提示符调用节点脚本时,相对路径有效,但在 Windows Powershell 中调用节点脚本时无效。

    【讨论】:

      【解决方案4】:

      您使用的是 cmd.exe 还是 Powershell? 尝试手动添加 PHANTOMJS_BIN 并将其指向 phantomjs.exe,而不是 .cmd。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 2013-07-10
        • 2013-07-19
        • 2011-09-24
        相关资源
        最近更新 更多