无法在CasperJS中运行测试框架 [英] Can't run the testing framework in CasperJS

查看:73
本文介绍了无法在CasperJS中运行测试框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习CasperJS以及如何编写单元测试,但我的测试失败了。我尝试了网站上的示例代码,但也失败了!我对如何在CasperJS中使用测试框架感到困惑。

I am learning CasperJS and how to write unit tests but my test fails. I tried the example code from the website and it fails as well! I am confused about how to use the testing framework in CasperJS.

这是我的代码:

var casper = require('casper').create();

casper.test.begin('The title exists', 1, function suite(test) {
    casper.start('http://stackoverflow.com', function() {
        test.assertExists('title');
    }).run(function() {
        test.done();
    });
});

并执行它:

casperjs test script.js

输出:

Test file: test.js                                                              
Fatal: you can't override the preconfigured casper instance in a test environment.
Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/local/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.


推荐答案

我也遇到过这个问题。不幸的是,文档并没有很好地说明将会失败的不同组合。

I came across this issue, too. Unfortunately, the docs don't spell out very well the different combinations that will both work and that will fail.

如果你注释掉/删除第一行
var casper = require('casper')。create(); )并运行命令 casperjs test test.js ,然后你的脚本就可以了。

If you comment out/remove the first line (var casper = require('casper').create();) and run your command casperjs test test.js, then your script will work.

如果你离开第一行,但不包括test作为您的命令行: casperjs test.js 然后您的脚本将起作用。

If you leave in the first line, but don't include "test" as part of your command line: casperjs test.js then your script will work.

值得注意的是,第二个选项在控制台中没有输出。

Notably, the second option produces no output in the console.

如果您有两个选项 - 通过命令行告诉casper您正在使用 test 函数,尝试在脚本中创建一个新实例(当包含第一行时) - 这就是它给出错误的原因。基本上,一个或另一个将创建一个新的casper实例。您不需要创建它两次。

If you have both options - telling casper via the command line that you are using the test function, and trying to create a new instance in the script (when that first line is included) - that is why it gives the error it does. Basically, one or the other will create a new casper instance. You don't need to create it twice.

这篇关于无法在CasperJS中运行测试框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆