如何使用jquery编写nightwatch自定义命令
我有用javascript为nightwatch.js编写的以下自定义命令。 如何将其转换为使用jquery?
exports.command = function (classId, indexIfNotZero) { this.browser.execute(function (classId, indexIfNotZero) { if (classId.charAt(0) == '.') { classId = classId.substring(1); } var items = document.getElementsByClassName(classId); if (items.length) { var item = indexIfNotZero ? items[indexIfNotZero] : items[0]; if (item) { item.click(); return true; } } return false; //alert(rxp); }, [classId, indexIfNotZero], function (result) { console.info(result); }); };
我看到有一些事情会导致你的问题。
首先,您有可变阴影 ,可能会导致问题。 您的全局导出命令有2个变量( classId
和indexIfNotZero
),您的内部执行命令具有相同的参数名称。
其次,对于自定义命令, this
变量实际上是browser
。 因此,不需要执行this.browser.execute
,只需要调用this.execute
。
至于完整的工作代码示例,请转到:
'use strict'; var ClickElementByIndex = function(className, index) { if (!index) { index = 0; } this.execute(function(selector, i) { var $item = $(selector + ':eq(' + i + ')'); if (!!$item) { $item.click(); return true; } return false; }, [className, index], function(result) { console.info(result); }); }; exports.command = ClickElementByIndex;
请注意,您确实需要在应用程序的全局范围内使用jQuery才能实现此目的。