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

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...