CasperJS 和“不安全的 JavaScript 尝试使用 URL 访问框架”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26670696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:17:49  来源:igfitidea点击:

CasperJS and 'Unsafe JavaScript attempt to access frame with URL' error

javascriptphantomjsintegration-testingcasperjs

提问by michaeluskov

I have simple page with javascript which validates email written in input:

我有一个带有 javascript 的简单页面,用于验证输入中编写的电子邮件:

email.html:

电子邮件.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:

电子邮件.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;
};

I want to test validation functionality using CasperJS. Here is my test case:

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

test/validator.test.js:

测试/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, '[email protected]'), 'green', '[email protected]');
        test.assertEquals(validate(this, 'vnbgfjbndkjnv'), 'red', 'vnbgfjbndkjnv');
    }).run(function() {
        test.done();
    });

});

But when I run tests using casperjs test test/validator.test.js, there is always error after information about tests:

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

Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///C:/Users/home/AppData/Roaming/npm/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.

Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///C:/Users/home/AppData/Roaming/npm/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.

What's wrong?

怎么了?

PhantomJS version: 1.9.8

PhantomJS 版本:1.9.8

采纳答案by Jakozaur

Recent PhantomJS (1.9.8) introduced this error message. It doesn't cause any real issue, other than confusing log lines when quiting PhantomJS.

最近的 PhantomJS (1.9.8) 引入了这个错误信息。除了退出 PhantomJS 时混淆日志行之外,它不会导致任何实际问题。

It is fixed in unreleased 1.9 branch: https://github.com/ariya/phantomjs/pull/12720

它已在未发布的 1.9 分支中修复:https: //github.com/ariya/phantomjs/pull/12720

回答by PPB

This issue is fixed in phantomjs version 1.9

此问题已在 phantomjs 1.9 版中修复

"phantomjs": "^1.9.9"

phantomjs": "^1.9.9"

for casperJs

对于 casperJs

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

回答by noobie

I was getting this same error, so I tried updating to phantomjs 1.9.9(from 1.9.8). However, I was getting an install error when trying to install 1.9.9, so I down rev-ed to phantomjs 1.9.7, and that fixed this error for me. So, it seems like this is an issue introduced in phantomjs 1.9.8.

我遇到了同样的错误,所以我尝试更新到phantomjs 1.9.9(从1.9.8)。但是,我在尝试安装时遇到了安装错误1.9.9,所以我降级为phantomjs 1.9.7,并为我修复了这个错误。因此,这似乎是phantomjs 1.9.8.

回答by Peter the Russian

Hey there are several workarounds to try and get around this by exiting phantom in a different way such as

嘿,有几种解决方法可以尝试通过以不同的方式退出幻像来解决这个问题,例如

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

WHICH DOSE NOT HELP and

这没有帮助和

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

instead of

代替

this.exit();

Nothing has worked!!!

什么都没用!!!

I tried several different versions of PhantomJS. My output comes in as JSON and after many hours of failed attempts of JSON.parse(stdout)I had to give up. I figured 'F' it I'm just going to handle the 'F'ing error.

我尝试了几个不同版本的 PhantomJS。我的输出是 JSON,经过数小时的 JSON.parse(stdout) 失败尝试后,我不得不放弃。我想 'F' 它我只是要处理 'F'ing 错误。

so simple before i exit i

在我退出之前如此简单

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

and After I send

在我发送之后

this.echo('"}]');

When i get my results back i simply replace all the line breaks and ignore the error

当我得到我的结果时,我只需替换所有换行符并忽略错误

stdout = stdout.replace(/(?:\r\n|\r|\n)/g, '');

Side note: this problem was tried to be fixed with phantom 1.9.9 but instead the focussed on building phantom 2.0 which is not compatible with CasperJS

旁注:这个问题试图用 phantom 1.9.9 修复,但重点是构建与 CasperJS 不兼容的 phantom 2.0