Frameworks 使用CasperJS和配置文件进行前端无头浏览器测试?

Frameworks 使用CasperJS和配置文件进行前端无头浏览器测试?,frameworks,phantomjs,casperjs,configuration-files,headless-browser,Frameworks,Phantomjs,Casperjs,Configuration Files,Headless Browser,我尝试使用CasperJS进行无头浏览器测试,使用PhantomJS,并希望有一个配置文件或其他东西来更改网站URL、用户名密码等,所以最终我找到了。你们知道还有什么更好的方法吗?如果我想从头开始写一个,我也想知道。我得到了一个解决方案(不完美;),它使用多个配置文件(用于选择器、执行、桌面、移动等) 我在casperjs测试的执行中包含了一个文件,该文件提供了我需要的所有配置(我还包括全局函数)。 让我们猜测测试执行情况如下所示: casperjs test --includes=lo

我尝试使用CasperJS进行无头浏览器测试,使用PhantomJS,并希望有一个配置文件或其他东西来更改网站URL、用户名密码等,所以最终我找到了。你们知道还有什么更好的方法吗?如果我想从头开始写一个,我也想知道。

我得到了一个解决方案(不完美;),它使用多个配置文件(用于选择器、执行、桌面、移动等)
我在casperjs测试的执行中包含了一个文件,该文件提供了我需要的所有配置(我还包括全局函数)。 让我们猜测测试执行情况如下所示:

    casperjs test --includes=loadGlobals.js test_1.js
    var fs = require('fs');
    var config = {},
      configFile = fs.read('config.json');
    config = JSON.parse(configFile);
    {
        "url": "http://www.yourTestUrl.com",
        "variable_1": "bla",
        "variable_2": "blub",
        "nextTier": {
            "variable_1": "blablub"
        }
    }
在示例中,loadGlobals.js包含如下函数:

    casperjs test --includes=loadGlobals.js test_1.js
    var fs = require('fs');
    var config = {},
      configFile = fs.read('config.json');
    config = JSON.parse(configFile);
    {
        "url": "http://www.yourTestUrl.com",
        "variable_1": "bla",
        "variable_2": "blub",
        "nextTier": {
            "variable_1": "blablub"
        }
    }
很可能config.json是这样的:

    casperjs test --includes=loadGlobals.js test_1.js
    var fs = require('fs');
    var config = {},
      configFile = fs.read('config.json');
    config = JSON.parse(configFile);
    {
        "url": "http://www.yourTestUrl.com",
        "variable_1": "bla",
        "variable_2": "blub",
        "nextTier": {
            "variable_1": "blablub"
        }
    }
现在,您可以在test_1.js中调用config.json的变量:

casper.start(config.url, function() {
  casper.then(function() {
    casper.echo(config.variable_1);
    casper.echo(config.variable_2);
    casper.echo(config.nextTier.variable_1);
  });
})
casper.run();
您可以像那样使用不同的配置文件,甚至在测试期间覆盖它(如果需要)
测试应以页面对象模式样式编写,因此它们具有高度可维护性,尤其是外包配置。



NuclearJ我不知道,但我也会研究一下。

它说找不到变量config。你是否在测试执行中包括loadGlobals.js,比如--includes=loadGlobals.js?是的,正如你提到的,我包括了它。我只是在一个新的环境中重建它,它可以工作。:Files(都在同一个目录中):“config.json”,“globalConfig.js”、“test_1.js”/使用“casperjs test--includes=globalConfig.js test_1.js”执行测试。还有人遇到同样的问题吗?