如何使用模块在NightwatchJS脚本之外移动函数?

时间:2017-04-21 12:35:25

标签: nightwatch.js

如果我在Nightwatch脚本中编写了多个函数 - 如下所示 - 我怎样才能将它们移到脚本之外以便其他脚本使用?

function firstFunction (aaa, callback) {
    // do async stuff
    callback(result)
}

function secondFunction (bbb, callback) {
    firstFunction (aaa, function (result) {
    // do async stuff
    callback(result)
    }
}

secondFunction (ccc, function (result) {
    // script actions/verifications
}

我想在脚本中只保留最后一部分(脚本操作)并将函数移到外面以供其他脚本使用。

LE:我创建了一个名为Utils的独立文件夹,我在其中创建了2个文件 - getColumnValues.js和getTableColumns.js(我在脚本中运行的函数)。

我的文件内容:

// getTableColumns.js(第一个函数)

exports.getTableColumns = function (tableSelector, callback) {
    var columnNames = [];
    var tableHeaderSelector = tableSelector + ' > tbody > tr:nth-of-type(1) > th';
    this.api.elements('css selector', tableHeaderSelector, function (objectResults) {
        for (var i in objectResults.value) {
            this.elementIdAttribute(objectResults.value[i].ELEMENT, 'innerText', function(result) {
                columnNames.push(result.value);
                if (columnNames.length == objectResults.value.length) {
                    callback(columnNames);
                }
            });
        }
    });
}

// getColumnValues.js(第二个函数)

var path = require(' path'); var utils = require(path.resolve(__dirname," ./ getTableColumns"));

exports.getColumnValues = function (columnName, tableSelector, callback) {
    utils.getTableColumns(tableSelector, function (columnList) {
        var columnIndex = columnList.indexOf(columnName) + 1;
        var columnValues = [];
        cellSelector = tableSelector + ' > tbody > tr:nth-of-type(3) > td:nth-of-type(' + columnIndex + ')';
        this.api.element('css selector', cellSelector, function (objectResult) {
            this.elementIdAttribute(objectResult.value.ELEMENT, 'childElementCount', function(result1) {
                for (var j = 2; j < 22; j++) {
                    cellSelector = tableSelector + ' > tbody > tr:nth-of-type(' + j + ') > td:nth-of-type(' + columnIndex + ')';
                    this.api.element('css selector', cellSelector, function (objectResult) {
                        this.elementIdAttribute(objectResult.value.ELEMENT, 'innerText', function(result) {
                            columnValues.push(result.value);
                            if (columnValues.length == 20) {
                                callback(columnValues);
                            }
                        });
                    });             
                }
            });
        }); 
    });
}

// myScript.js

var utils = require('../../lib/utils/getColumnValues.js');
utils.getColumnValues('Route', 'table.table-striped', function (result) {
//do something
}

当我像这样运行脚本时,我收到此错误:&#34; ReferenceError:未定义客户端        at Object.exports.getTableColumns(C:\ automation \ lib \ utils \ getTableColumns.js:4:9)        at Object.exports.getColumnValues(C:\ automation \ lib \ utils \ getColumnValues.js:5:15)&#34;

如果我用this.api更改客户端,我会收到此错误:

Running:  Demo test
 ✖ TypeError: Cannot read property 'elements' of undefined
    at Object.exports.getTableColumns (C:\automation\lib\utils\getTableColumns.js:4:17)
    at Object.exports.getColumnValues (C:\automation\lib\utils\getColumnValues.js:5:15)
    at Object.Demo test (C:\automation\tests\Kite\demotest1.js:55:16)
    at Module.call (C:\automation\lib\nightwatch\lib\runner\module.js:62:34)
    at C:\automation\lib\nightwatch\lib\runner\testcase.js:70:29

FAILED:  1 errors (15ms)

 _________________________________________________

 TEST FAILURE: 1 error during execution,  0 assertions failed, 0 passed. (93ms)

 × Kite\demotest1

   - Demo test (15ms)

     Error while running [Kite / Demotest1 / Demo test]:

     TypeError: Cannot read property 'elements' of undefined
       at Object.exports.getTableColumns (C:\automation\lib\utils\getTableColumns.js:4:17)
       at Object.exports.getColumnValues (C:\automation\lib\utils\getColumnValues.js:5:15)
       at Object.Demo test (C:\automation\tests\Kite\demotest1.js:55:16)
       at Module.call (C:\automation\lib\nightwatch\lib\runner\module.js:62:34)
       at C:\automation\lib\nightwatch\lib\runner\testcase.js:70:29

1 个答案:

答案 0 :(得分:0)

是:

  1. 使用您的功能创建一个文件,并将其作为模块公开
  2. 通过要求您先前创建的文件
  3. 在两个脚本中使用您的函数

    就这么简单:

    // utils/getTableColumns
    // Note the first argument: client
    exports.getTableColumns = (client, tableSelector, callback) => {
      // use the client here
      client.api.element('css selector', tableSelector, () => {
        callback();
      });
    }
    

    在你的第二个档案中:

    var utils = require('../../lib/utils/getColumnValues.js');
    
    
    describe("suite" , () => {
      it("should test", test);
    });
    
    function test(client){
      // Pass the client as an argument
      utils.getColumnValues(client, 'Route', 'table.table-striped', function (result) {
        //do something
      }
    }