【问题标题】:How to include a file content in a casper test file?如何在 casper 测试文件中包含文件内容?
【发布时间】:2015-02-20 22:29:24
【问题描述】:

我有一个典型的 casper 测试文件和一个包含我想在 casper 测试文件中使用的常量的文件,如何在 casper 测试文件中包含常量文件?

例如,常量文件包含:

var ERR_XXX=1000;
var ERR_YYY=1001;

casper 测试文件包含:

var x = require('casper').selectXPath;

casper.test.begin("Open Unexisting Page", function(test) {

    casper.start("https://localhost/xxx.html",
        function() {
            this.test.assertHttpStatus(404, 'HTTP status is 404 for ' + this.getCurrentUrl());
        });

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

});

我希望能够在 casper 文件中调用 console.log("Some error:" + ERR_XXX)。我该如何继续?

【问题讨论】:

    标签: javascript node.js casperjs


    【解决方案1】:

    您可以使用 PhantomJS 的 file system moduleeval 立即 read 常量文件,以使以下代码知道变量。

    但我建议您将文件编写为 JSON

    {
        "ERR_XXX": 1000,
        "ERR_YYY": 1001
    }
    

    只需 require 那个 JSON 文件:

    var c = require("const.json");
    console.log(c.ERR_XXX);
    

    更多信息请查看我的回答here

    【讨论】:

      猜你喜欢
      • 2016-09-21
      • 2022-11-11
      • 1970-01-01
      • 2016-10-20
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      相关资源
      最近更新 更多