CasperJS和“Unsafe JavaScript attempt to access frame with URL”错误

24

我有一个简单的页面,其中包含使用JavaScript验证输入框中的电子邮件地址的代码:

email.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Email validation</title>
        <script src="email.js"></script>
    </head>
    <body>
        <span style="padding: 5px;">
            <input type="text" id="email-input" placeholder="Email..."></input>
        </span>
    </body>
</html>

email.js:

var checkEmail = function() {
    var regexp = /BIG_REGEX/;
    var email = document.getElementById('email-input').value;
    if (email === '') 
        removeFrame();
    else if (regexp.test(email))
        drawFrame('green');
    else
        drawFrame('red');
};

var removeFrame = function() {
    var input = document.getElementById('email-input');
    input.parentNode.style.backgroundColor = input.parentNode.parentNode.style.backgroundColor;
};

var drawFrame = function(color) {
    var input = document.getElementById('email-input');
    input.parentNode.style.backgroundColor = color;
};


window.onload = function() {
    document.getElementById('email-input').onkeyup = checkEmail;
};

我想使用CasperJS测试验证功能。这是我的测试案例:

test/validator.test.js:

var fillEmail = function(browser, email) {
    browser.sendKeys('#email-input', email, {reset: true});
};

var getValidation = function(browser) {
    var color = browser.evaluate(function () {
        return document.getElementById('email-input').parentNode.style.backgroundColor;
    });
    return color;
};

var validate = function(browser, email) {
    fillEmail(browser, email);
    return getValidation(browser);
};

casper.test.begin('Validation testing', function suite(test) {
    casper.start('http://localhost:8000/email.html', function() {
        test.assertEquals(validate(this, 'uskovm@gmail.com'), 'green', 'uskovm@gmail.com');
        test.assertEquals(validate(this, 'vnbgfjbndkjnv'), 'red', 'vnbgfjbndkjnv');
    }).run(function() {
        test.done();
    });

});

但是当我使用casperjs test test/validator.test.js运行测试时,总是在测试信息之后出现错误:

尝试使用不安全的 JavaScript 访问 URL 为 about:blank 的框架,来源为 URL 为 file:///C:/Users/home/AppData/Roaming/npm/node_modules/casperjs/bin/bootstrap.js 的框架。域、协议和端口必须匹配。

出了什么问题?

PhantomJS 版本:1.9.8


你确定是哪一行导致了这个问题吗?如果不确定,加一些console.log语句,尝试将其缩小到一行。 - Artjom B.
@ArtjomB。当测试通过后(在“PASS 2 tests executed in 2.693s, 2 passed...”之后),将打印此行。 - michaeluskov
这是因为PhantomJS 1.9.8中引入了一些更改,而且有一个CasperJS问题与此有关(https://github.com/n1k0/casperjs/issues/1068)。所以这个(https://dev59.com/AF8d5IYBdhLWcg3woDYZ)可能是相关的,但由于这个错误在脚本完成时出现,我不知道还能做什么。 - Artjom B.
好的,这是在脚本退出时发生的。我认为 CasperJS 无法消除这些警告。我已经在 GitHub 上创建了一个问题,针对 PhantomJS 的问题。链接 - Artjom B.
4个回答

18
近期PhantomJS (1.9.8) 引入了这个错误信息。它没有引起实际问题,除了在退出PhantomJS时混淆了日志行。 这在未发布的1.9分支中得到了修复: https://github.com/ariya/phantomjs/pull/12720

8
这个答案(https://dev59.com/AF8d5IYBdhLWcg3woDYZ#26688062)提供了一个解决该问题的方法。 - flipchart

1
这个问题已经在PhantomJS版本1.9中得到了解决。
"phantomjs": "^1.9.9" 适用于CasperJS。
casperjs --ssl-protocol=tlsv1 test  run.js

1
我遇到了同样的错误,所以尝试更新到phantomjs 1.9.9(从1.9.8)。然而,在尝试安装1.9.9时,我遇到了安装错误,因此我降级到了phantomjs 1.9.7,这为我解决了这个错误。因此,看起来这是在phantomjs 1.9.8中引入的问题。

如果有人需要以前的exe文件:https://bitbucket.org/ariya/phantomjs/downloads。谢谢! - Aliz

0

嘿,有几种解决方法可以尝试绕过这个问题,例如以不同的方式退出phantom。

   casperjs --ssl-protocol=tlsv1 test  run.js

这并没有帮助

setTimeout(function(){
    phantom.exit();
}, 0);

而不是

this.exit();

什么都没用!!!

我尝试了几个不同版本的PhantomJS。 我的输出以JSON格式呈现,经过多个小时的JSON.parse(stdout)失败尝试后,我不得不放弃。我想“去他妈的”,我只是要处理这个错误。

所以在退出之前如此简单。

this.echo(',{"error":"');

在我发送之后

this.echo('"}]');

当我收到结果后,我只需替换所有的换行符并忽略错误。
stdout = stdout.replace(/(?:\r\n|\r|\n)/g, '');

顺便提一下:这个问题曾试图通过Phantom 1.9.9来解决,但他们转而专注于构建与CasperJS不兼容的Phantom 2.0。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接