微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/js/156249.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐