How to write a nightwatch custom command using jquery

后端 未结 1 911
渐次进展
渐次进展 2021-01-15 17:08

I have the following custom command written in javascript for nightwatch.js. How can I translate this to using jquery?

exports.command = function (classId,          


        
相关标签:
1条回答
  • 2021-01-15 17:43

    There are a few things that I see that are causing your issues.

    First, you have variable shadowing that may cause issues. Your global export command has 2 variables (classId and indexIfNotZero) and your internal execute command has the same parameter names.

    Second, for custom commands, the this variable is actually the browser. So instead of doing this.browser.execute, you need to just call this.execute.

    As for a complete working code example, here you go:

    '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;
    

    Note that you do need jQuery available in the global scope of your app for this to work.

    0 讨论(0)
提交回复
热议问题