javascript - How to write a nightwatch custom command using jquery -
i have following custom command written in javascript nightwatch.js. how can translate using 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); }); };
there few things see causing issues.
first, have variable shadowing may cause issues. global export command has 2 variables (classid
, indexifnotzero
) , internal execute command has same parameter names.
second, custom commands, this
variable browser
. instead of doing this.browser.execute
, need call this.execute
.
as complete working code example, here go:
'use strict'; var clickelementbyindex = function(classname, index) { if (!index) { index = 0; } this.execute(function(selector, i) { var $item = $(selector + ':eq(' + + ')'); if (!!$item) { $item.click(); return true; } return false; }, [classname, index], function(result) { console.info(result); }); }; exports.command = clickelementbyindex;
note need jquery available in global scope of app work.
Comments
Post a Comment