【问题标题】:How to override the preconfigured casper instance in a test environment如何在测试环境中覆盖预配置的 casper 实例
【发布时间】:2017-08-09 05:40:28
【问题描述】:

我已经安装了 phantomjs 1.9.7 和 casperjs 1.1.0,现在我看到我的所有脚本都需要更新并删除第一行,因为它会产生以下致命错误:

致命:您无法在测试环境中覆盖预配置的 casper 实例。 文档:http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options

阅读另一个问题,我知道我现在必须从我的脚本中删除这一行:

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

这是很多工作,因为我同时有超过 200 个脚本需要更新!!!

所以我的问题是如何在不更新所有脚本的情况下克服这个问题(在Can't run the testing framework in CasperJS 中提到)

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

if (casper.cli.has("ip") === false) {
  casper.echo("Usage: casper.js test get_info.js --ip=<x.x.x.x>").exit();
}

casper.test.begin('get info', 1, function suite(test) {
    casper.start("http://" + casper.cli.get("ip") + ":xxxx/man/", function() {
        test.assertTitle("TEST GUI", "GUI has the correct title");
        test.assertExists('form[name="loginForm"]', "login form is found");
        this.fill('form[name="loginForm"]', {
                            'userId': 'xxxxx',
                            'password': 'yyyyy'
        }, true);
    });

    casper.then(function() {
        test.assertTextExists("Welcome", "Login into TEST GUI");
        this.click('a#test_menu.menu1itemUnSel[tabindex="4"]');
    });

    casper.then(function() {
        casper.wait(5000, function() {
            this.echo('should appear after 5s');
        });
    });

    casper.then(function() {
        test.assertTextExists("TEST Data", "Login into Data");
        this.click('a#test_menu_data.menu2itemUnSel[tabindex="4"]');
    });

    casper.then(function() {
        casper.wait(5000, function() {
            this.echo('should appear after 5s');
        });
    });

    casper.then(function() {
        test.assertTextExists("Logout", "Loggin out from TEST GUI");
        this.click('a.minorLinkshighlight');
    });

    casper.run(function() {
        test.done();
        this.exit();
    });
});

在我安装 phantomjs 1.9.7 和 casperjs 1.1.0 之前,上面的脚本可以运行,我唯一不记得的是我必须重新安装服务器之前的版本!

感谢您的建议。

【问题讨论】:

  • 测试文件不应该有或从来不需要require('casper').create();,所以我不能 100% 确定你过去是如何运行测试脚本的。你能给我们看一个示例脚本吗?

标签: phantomjs casperjs


【解决方案1】:

是的,这是我避免更新旧 casperjs 脚本的方法,因为在测试环境中覆盖了预配置的 casper 实例!

转到 casperjs 路径并在路径模块中进行编辑:

/../casperjs/modules/casper.js

并注明以下几行:

exports.create = function create(options) {
    "use strict";
    // This is a bit of a hack to check if one is trying to override the preconfigured
    // casper instance from within a test environment.
//    if (phantom.casperTest && window.casper) {
//        console.error("Fatal: you can't override the preconfigured casper instance in a test environment.");
//        console.error("Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options");
//        phantom.exit(1);
//    }
    return new Casper(options);
};

这对我有用。您应该仅在您自担风险的情况下使用它!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2019-12-02
    • 2019-09-28
    • 2018-06-05
    相关资源
    最近更新 更多