2017-04-21 41 views
-1

如果我在Nightwatch脚本中写入了多个函数 - 如下所示 - 我如何将它们移出脚本以便其他脚本也可以使用它们?如何使用模块将功能移到NightwatchJS脚本之外?

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 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 
} 

当我跑出这样的剧本,我得到这个错误:“的ReferenceError:客户端未在Object.exports.getTableColumns(C定义 :\自动化\ lib中\ utils的\ getTableColumns.js:4:9) 在Object.exports.getColumnValues(C:\自动化\ lib中\ utils的\ getColumnValues.js:5:15)”

如果更改客户端与此.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 
+0

我修改了你的客户端访问的 – ghusse

回答

0

是:

  1. 创建你的函数的文件,揭露他们作为一个模块
  2. ,要求你以前创建

这么简单的文件使用你的函数在这两个脚本如下:

// 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 
    } 
} 
+0

问题的反应我约我如何试图解决这一问题用你的建议最初的评论添加更多细节。我尝试运行脚本时仍然收到一些错误。欢迎任何建议。谢谢。 – dorin123

+0

你能更新你使用的代码并发布错误吗? – ghusse

+0

我更新了函数和错误消息。如果你需要其他东西,请告诉我。 – dorin123

相关问题