【问题标题】:CasperJS include testfile dynamically in a loop with require or other functionCasperJS 使用 require 或其他函数在循环中动态包含测试文件
【发布时间】:2015-09-09 11:16:03
【问题描述】:

我收到了一个关于循环扩展测试的问题。我有一个 3 级循环结构,其中有 URLsTestfilesViewportsizes,如下所示:

var navigation = [
  "http://www.url_1.com",
  "http://www.url_2.com",
  "http://www.url_3.com",
  "http://www.url_4.com"
];

var testfiles = [
  "/componenttests/atoms/test_dropdown_buttons.js",
  "/componenttests/atoms/test_conditional_buttons.js",
  "/componenttests/atoms/test_icon_buttons.js"
];


var viewPortsizes = [
  [1440, 900],
  [320, 480],
  [320, 568],
  [600, 1024],
  [1024, 768],
  [1280, 800]
];

现在我想根据以下策略进行测试:

对所有 VIEWPORT SIZES 的 URL 运行所有测试

在以下结构中实现:

casper.start().then(function(){

  /* Loop through all URLs so that all are visited  */
  casper.eachThen(navigation, (function(response){

    var actUrl = response.data;

    /* Test different viewport resolutions for every URL */
    casper.eachThen(viewportSizes, function (responseView) {

      var actViewport = responseView.data;

      /* Set the viewport */
      casper.then(function () {            
        casper.viewport(actViewport[0], actViewport[1]);
      });

      /* Open the respective page and wait until its opened */
      casper.thenOpen(actUrl).waitForUrl(actUrl, function () {


        /* Single tests for every resolution and link */
        casper.each(testfiles, function (self, actTest, i) {

          /* AND HERE THE PROBLEM IS LOCATED, REQUIRE() ONLY WORKS ONCE */
          casper.then(function(){
            require('.' + testfiles[i]);
          });
        });
      });
    }));
})
.run(function() {
  this.test.done();
});

如代码中所述,问题是我只能通过 require 包含/加载这些测试文件一次。

那么我可以在这里做什么,我需要在最内部的循环中多次加载测试文件。

测试文件只是像 sn-ps

casper.then(function () {
  casper.waitForSelector(x("//a[normalize-space(text())='Bla']"),
    function success() {
      DO GOOD STUFF
    },
    function fail() {
      BAD THIGNS HAPPENED
    });
});

在第一次运行时包含文件,在所有其他运行中 > 1 不包含任何内容,循环运行正确但 require 不起作用。

这绝对是必需的功能,因为当我将测试代码从文件中直接复制到循环中时,它也会多次工作。

【问题讨论】:

    标签: javascript loops casperjs require


    【解决方案1】:

    我看到两个选项:

    • 将组件编写为适当的模块,并在脚本开头要求它们或
    • 读取组件测试文件并eval它。

    适当的模块

    您可以将测试组件定义为例如

    exports.test = function(){
        casper.then(function () {
            ...
        });
    };
    

    那么你可以在开头要求它们:

    testfiles = testfiles.map(function(path){
        return {
            path: path,
            test: require("." + path).test
        }
    });
    

    并直接在测试工具中使用它们:

    casper.then(function(){
        testfiles[i].test();
    });
    

    每回合评估

    或者您可以简单地在您的测试工具中使用它而不更改您的测试组件:

    var fs = require("fs");
    ...
    casper.then(function(){
        eval(fs.read("."+testfiles[i]));
    });
    

    【讨论】:

    • 感谢 Artjom 的快速响应,我现在会尽快测试。
    • 您好 Artjom,两种解决方案都运行良好,感谢您的快速响应 :)
    • 我直接选择了第二个选项并开始工作。谢谢。
    猜你喜欢
    • 2011-12-17
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2015-10-06
    • 2022-11-11
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多