将PhantomJS与Karma一起使用(Win7 x64)

时间:2013-05-01 14:13:57

标签: angularjs phantomjs karma-runner

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

使用phonecat示例,我使用Chrome运行Chrome,虽然Karma文档提到了PhantomJS(我现在安装了),但我无法弄清楚如何修改配置文件以使其运行。

我已经尝试将PhantomJS放在testacular.conf.js的browsers数组中,但我得到了;

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

我认为这意味着它正在发布,但在我看来(作为PhantomJS noob)它需要一个不同的命令行。我还下载了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。

4 个答案:

答案 0 :(得分:11)

首先,使用npm:

安装PhantomJS

npm install -g phantomjs

然后,您可能需要为业力指定PhantomJS可执行文件的位置。 npm安装将告诉你它放置可执行文件的位置。对我来说,在Git Bash中运行业力,我将以下内容添加到〜/ .profile:

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

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

答案 1 :(得分:3)

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

在您的代码中,您使用的是脚本浏览器启动程序(用于启动浏览器的自定义shell脚本)。这是可能的,但脚本必须接受一个参数,这是它应该打开的URL。 PhantomJS的表现并不像那样。

答案 2 :(得分:1)

在节点的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中调用节点脚本时没有。

答案 3 :(得分:0)

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