Selenium waitFor机制的内部工作原理是什么? [英] What are the inner workings of the Selenium waitFor mechanism?

查看:113
本文介绍了Selenium waitFor机制的内部工作原理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过拦截对doClick(定位器)的调用来自定义Selenium的click命令的行为(通过user-extentions.js)。基本上我需要在显示应用程序的忙碌指示符时延迟点击操作。

I am trying to customize the behavior of Selenium's click command, (via user-extentions.js), by intercepting calls to doClick(locator). Basically I need to delay click actions whenever our application's "busy indicator" is being displayed.

(现在这种事情的标准答案是将waitFor插入到这些情况的脚本。事实上,我们目前在整个脚本中都有数以万计。我试图消除它们。)

(Now the standard answer for this kind of thing is to insert a waitFor into the script for those situations. Indeed, we currently have zillions of them throughout our scripts. I'm trying to eliminate those.)

检测页面元素是微不足道的部分。棘手的部分是让脚本实际等待。我看起来很有希望,但失败的尝试看起来像这样:

Detecting the page element is the trivial part. The tricky part is getting the script to actually wait. My promising looking, but failed attempt looks like this:

var nativeClick = Selenium.prototype.doClick;
Selenium.prototype.doClick = function(locator) {
  this.doWaitForCondition("!selenium.browserbot.findElementOrNull('busy-indicator')", 5000);
  return nativeClick.call(this, locator);
}

每次点击前都会调用doWaitForCondition,但在条件评估为false时等待。 nativeClick总是立即被调用,因此不会引入延迟。我怀疑doWaitForCondition函数实际上并没有执行任何等待,而是在命令执行循环中为它建立条件。在这种情况下,click命令已经在运行,我正在尝试在命令中运行命令。

The doWaitForCondition gets called before every click, but it does not wait when the condition evaluates to false. nativeClick always gets called immediately, and so no delay is introduced. I suspect that the doWaitForCondition function doesn't actually do any waiting per se, but rather establishes the conditions for it within the command execution loop. And in this case the click command is already in play, and I'm trying to run a command within a command.

有人可以了解Selenium命令如何执行和waitFor工作,或提供有关如何做到这一点的建议?

Can somebody shed some light on how Selenium command execution and waitFor works, or offer suggestions on how this might be done?

推荐答案

我终于解决了这个问题。并且使用比尝试以各种形式拦截点击处理更好的方法。我的目标是:当我们的应用程序忙时,延迟执行脚本命令。

I have finally solved this. And with an approach that is much better than trying to intercept click processing in its various forms. My refined goal is: to delay execution of script command completion when our application is "busy".

完成后,每个selenium命令返回一个 ActionResult 对象(参见 ActionHandler.prototype.execute 。此对象上的 terminationCondition 属性是一个函数,用于确定selenium何时可以继续执行下一个命令 TestLoop.prototype .continueTestWhenConditionIsTrue 。基本上,selenium重复执行条件函数,直到它产生true。结果对象非常简单:

Upon completion, each selenium command returns an ActionResult object, (see ActionHandler.prototype.execute). The terminationCondition attribute on this object is a function that determines when it is okay for selenium to proceed to the next command, (TestLoop.prototype.continueTestWhenConditionIsTrue). Basically, selenium repeatedly executes the condition function until it yields true. The result object it quite trivial:

function ActionResult(terminationCondition) {
  this.terminationCondition = terminationCondition;
}



自定义:



我想在任何时候延迟执行 myAppIsBusy()返回true。当然,所有标准延迟也需要保持不变,例如等待页面加载,以及脚本化的显式waitFor条件。解决方案是在我的user-extensions.js中重新定义selenium结果对象,如下所示:

Customizing it:

I want to delay execution any time myAppIsBusy() returns true. Of course all of the standard delays need to remain in place as well, like waiting for page loads, and explicit waitFor conditions as scripted. The solution is to redefine the selenium result object in my user-extensions.js, as follows:

function ActionResult(terminationCondition) {
  this.terminationCondition = function() {
    // a null terminationCondition means okay to continue
    return (!terminationCondition || terminationCondition()) && !myAppIsBusy();
  }
}

最重要的是,它足够低它适用于IDE以及RC的级别。

The great thing is that this is at a low enough level that it works for the IDE, as well as for RC.

请注意,这不会影响Accessor或Assert命令类型,它们返回不同的结果对象。但这应该没问题,因为这些命令不会影响应用程序的状态。

这篇关于Selenium waitFor机制的内部工作原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆