2017-04-21 20 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

相關問題