如何使用模块将函数移动到 NightwatchJS 脚本之外?
How to move functions outside a NightwatchJS script using modules?
如果我在 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
}
当我 运行 这样的脚本时,我得到这个错误:“ReferenceError: client is not defined
在 Object.exports.getTableColumns (C:\automation\lib\utils\getTableColumns.js:4:9)
在 Object.exports.getColumnValues (C:\automation\lib\utils\getColumnValues.js:5:15)"
如果我使用 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
是:
- 使用您的函数创建一个文件,并将它们公开为一个模块
- 通过要求您之前创建的文件在两个脚本中使用您的函数
就这么简单:
// 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
}
}
如果我在 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
}
当我 运行 这样的脚本时,我得到这个错误:“ReferenceError: client is not defined 在 Object.exports.getTableColumns (C:\automation\lib\utils\getTableColumns.js:4:9) 在 Object.exports.getColumnValues (C:\automation\lib\utils\getColumnValues.js:5:15)"
如果我使用 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
是:
- 使用您的函数创建一个文件,并将它们公开为一个模块
- 通过要求您之前创建的文件在两个脚本中使用您的函数
就这么简单:
// 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
}
}