为什么每次用户点击时,Selenium IDE 记录器都会多次调用我的 LocatorBuilder 函数?

发布于 2024-09-26 15:48:53 字数 498 浏览 0 评论 0原文

我定义了一个 LocatorBuilder 函数并将其插入为 LocatorBuilders 数组的第一个元素。它运行良好。但是,当我在函数顶部添加alert() 时,我发现对于记录的每个用户点击,我的函数都会被调用两次。具体来说,当我打开记录并单击页面上的按钮时,事件顺序是:1)我的函数被调用,2)利用我生成的定位器表达式记录单击,3)浏览器处理单击, 4) 我的函数再次被调用。

注意 - 我在任何地方都找不到此文档,但我推测从根本上讲,记录器调用 LocatorBuilders 列表中的每个函数,每个函数返回一个候选定位器表达式,直到它获得与页面上的一个元素完全匹配的非空表达式。

所以我的函数按预期工作,但额外的函数调用似乎是多余的。我的表达式生成器函数需要多次调用是否有正当理由?例如,主题 HTML 元素是否可以在调用之间更改?在连续调用我的函数之间记录器正在做什么?

编辑:哦!我发现我将函数添加到 LocatorBuilders.order 列表中两次。所以现在我的函数只被调用两次。尽管如此,为什么要多次调用呢?

I have defined a LocatorBuilder function and inserted it as the first element of LocatorBuilders array. It is working nicely. However, when I add an alert() at the top of my function, I observe that my function is being called twice for each user click that is recorded. Specifically, when I turn on recording and click a button on the page, the sequence of events is: 1) my function gets called, 2) the click is recorded utilizing the locator expression that I have produced, 3) the browser processes the click, 4) my function is called again.

Note - I can't find this documented anywhere, but I surmise that fundamentally, the recorder calls each function in the LocatorBuilders list, each returning a candidate locator expression, until it gets a non-null expression that matches exactly one element on the page.

So my function works as desired, but the extra function call seem redundant. Is there some valid reason that my expression builder function needs to be called more than once? For example, is it possible for the subject HTML element to change between calls? What is the recorder doing between successive calls to my function?

EDIT: DOH! I discovered that I was adding my function into the LocatorBuilders.order list twice. So now my function only gets called twice. Still though, why multiple calls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2024-10-03 15:48:53

当您通过 LocatorBuilders.add(name, func) 定义定位器构建器时,Selenium 会将名称添加到全局数组:LocatorBuilders.order。记录器在每个用户事件上迭代这些名称,依次调用每个构建器函数。

通过将脚本配置为“核心扩展”或“IDE 扩展”,可以将自定义定义加载到 IDE 中。当 Selenium IDE 窗口打开时,两组扩展都会加载(IDE 然后是 Core),因此您指定哪一组似乎并不重要。但请注意,第一次在 IDE 窗口中播放命令时,核心扩展脚本会重新加载。因此,配置为核心扩展的脚本需要幂等

因此,由于定位器构建器是记录器功能,因此将其配置为 IDE 扩展,并且只会加载一次。如果出于某种原因它需要与运行时代码共存(可能是共享逻辑),您可以将其作为核心扩展加载,但请确保它是幂等的。 (它也可能只在 IDE 中加载。)例如:(

if ("SeleniumIDE" in window) { // we're running in the IDE window
  var i = LocatorBuilders.order.indexOf(locatorName);
  if (i != -1)
    LocatorBuilders.order.splice(i, 1); // remove a previous entry
  LocatorBuilders.add(locatorName, function(elem) { ...
}

请注意,并非所有浏览器都支持 indexOf() 和 splice() 数组方法,但 Firefox 支持,这是 IDE 逻辑。)

When you define a locator-builder via LocatorBuilders.add(name, func), Selenium adds the name to the global array: LocatorBuilders.order. The recorder iterates over these names on each user event, calling each builder function in turn.

Custom definition(s) are loaded into the IDE by configuring your script as either a "Core extension" or an "IDE extension". Both sets of extensions are loaded when the Selenium IDE window opens, (IDE then Core), so it might not seem to matter which one you specify. But beware that the Core extension scripts get reloaded the first time a command is played back in the IDE window. Therefore scripts that are configured as Core extensions need to be idempotent.

So since a locator-builder is Recorder functionality, configure it as an IDE extension and it will get loaded only once. If there is some reason it needs to coexist with runtime code, (shared logic is likely), you can load it as a Core extension, but make sure it is idempotent. (And it may as well be loaded only in the IDE.) For example:

if ("SeleniumIDE" in window) { // we're running in the IDE window
  var i = LocatorBuilders.order.indexOf(locatorName);
  if (i != -1)
    LocatorBuilders.order.splice(i, 1); // remove a previous entry
  LocatorBuilders.add(locatorName, function(elem) { ...
}

(Note that the indexOf() and splice() array methods are not supported by all browsers, but Firefox does, and this is IDE logic.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文