nightwatch.js – 如何使用nightwatchjs检查元素是否可点击?

前端之家收集整理的这篇文章主要介绍了nightwatch.js – 如何使用nightwatchjs检查元素是否可点击?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用nightwatch js检查元素是否可点击?我想点击一个元素,但是当我运行nightwatch时,selenium没有点击该元素,因为它还无法点击.

解决方法

这样的事情应该有效.如果您有疑问,请告诉我
var util = require('util');
var events = require('events');

/*
 * This custom command allows us to locate an HTML element on the page and then wait until the element is both visible
 * and does not have a "disabled" state.  It rechecks the element state every 500ms until either it evaluates to true or
 * it reaches maxTimeInMilliseconds (which fails the test). Nightwatch uses the Node.js EventEmitter pattern to handle 
 * asynchronous code so this command is also an EventEmitter.
 */

function WaitUntilElementIsClickable() {
  events.EventEmitter.call(this);
  this.startTimeInMilliseconds = null;
}

util.inherits(WaitUntilElementIsClickable,events.EventEmitter);

WaitUntilElementIsClickable.prototype.command = function (element,timeoutInMilliseconds) {
  this.startTimeInMilliseconds = new Date().getTime();
  var self = this;
  var message;

  if (typeof timeoutInMilliseconds !== 'number') {
    timeoutInMilliseconds = this.api.globals.waitForConditionTimeout;
  }

  this.check(element,function (result,loadedTimeInMilliseconds) {
    if (result) {
      message = '@' + element + ' was clickable after ' + (loadedTimeInMilliseconds - self.startTimeInMilliseconds) + ' ms.';
    } else {
      message = '@' + element + ' was still not clickable after ' + timeoutInMilliseconds + ' ms.';
    }
    self.client.assertion(result,'not visible or disabled','visible and not disabled',message,true);
    self.emit('complete');
  },timeoutInMilliseconds);

  return this;
};

WaitUntilElementIsClickable.prototype.check = function (element,callback,maxTimeInMilliseconds) {
  var self = this;

  var promises =[];
  promises.push(new Promise(function(resolve) {
    self.api.isVisible(element,function(result) {
      resolve(result.status === 0 && result.value === true);
    });
  }));

  promises.push(new Promise(function(resolve) {
    self.api.getAttribute(element,'disabled',function (result) {
      resolve(result.status === 0 && result.value === null);
    });
  }));

  Promise.all(promises)
    .then(function(results) {
      var now = new Date().getTime();
      const visibleAndNotDisabled = !!results[0] && !!results[1];
      if (visibleAndNotDisabled) {
        callback(true,now);
      } else if (now - self.startTimeInMilliseconds < maxTimeInMilliseconds) {
        setTimeout(function () {
          self.check(element,maxTimeInMilliseconds);
        },500);
      } else {
        callback(false);
      }
    })
    .catch(function(error) {
      setTimeout(function () {
        self.check(element,maxTimeInMilliseconds);
      },500);
    });
};

module.exports = WaitUntilElementIsClickable;

将此代码作为文件添加到命令文件夹中.它应该被称为waitUntilElementIsClickable.js或任何你想要的命令.

用法是:

browser.waitUntilElementIsClickable('.some.css');

您还可以使用页面元素:

var page = browser.page.somePage();
page.waitUntilElementIsClickable('@someElement');
原文链接:https://www.f2er.com/js/156249.html

猜你在找的JavaScript相关文章