【问题标题】:CasperJS and 'Unsafe JavaScript attempt to access frame with URL' errorCasperJS 和“不安全的 JavaScript 尝试使用 URL 访问框架”错误
【发布时间】:2014-12-27 12:55:57
【问题描述】:

我有一个带有 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 运行测试时,在测试信息之后总是出现错误:

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.

怎么了?

PhantomJS 版本:1.9.8

【问题讨论】:

  • 您是否已将范围缩小到导致此问题的哪一行?如果没有,请添加一些 console.log 并尝试将其缩小到一行。
  • @ArtjomB。测试通过后(PASS 2 tests executed in 2.693s, 2 passed... 之后)会打印此行
  • 这是因为 PhantomJS 1.9.8 中引入了一些更改,并且有一个 CasperJS issue 关于它。所以this 可能是相关的,但是由于脚本完成时出现此错误,我不知道可以做什么。
  • 好的,这发生在脚本退出时。我没有看到 CasperJS 可以做任何事情来删除这些警告。我为 PhantomJS 问题创建了一个GitHub issue

标签: javascript phantomjs integration-testing casperjs


【解决方案1】:

我遇到了同样的错误,所以我尝试更新到phantomjs 1.9.9(来自1.9.8)。但是,我在尝试安装 1.9.9 时遇到了安装错误,因此我将其修改为 phantomjs 1.9.7,这为我解决了这个错误。所以,这似乎是phantomjs 1.9.8 中引入的一个问题。

【讨论】:

【解决方案2】:

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

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

没有帮助和

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

而不是

this.exit();

没有任何效果!!!

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

在我退出之前很简单

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

在我发送之后

this.echo('"}]');

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

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

旁注:此问题已尝试使用 phantom 1.9.9 修复,但重点是构建与 CasperJS 不兼容的 phantom 2.0

【讨论】:

    【解决方案3】:

    此问题已在 phantomjs 1.9 版中修复

    "phantomjs": "^1.9.9"

    对于 casperJs

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-18
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多