watir-webdriver - 单击 Javascript 按钮

发布于 2025-01-03 17:11:31 字数 630 浏览 6 评论 0原文

第一周进行 watir-webdriver 和 Web 应用程序测试,因此仍在尝试概括一些概念。

拥有这个 javascript 元素:

<input type="submit" class="button" value="Search" name="_target0">

browser.button(:value, "Search").exists?
=> "true"

browser.button(:value, "Pesquisar").present?
=> true

browser.button(:name, "_target0").value
=> "Search"

这实际上并不会驱动按钮被点击,

browser.button(:name, "_target0").click

所以我使用任一个驱动 Firefox 单击按钮,

browser.button(:name, "_target0").fire_event('on_click')
browser.button(:name, "_target0").when_present.click

但它们之间有什么区别?

First week with watir-webdriver and Web app testing in general, so still trying to wrap some concepts around.

Having this javascript element:

<input type="submit" class="button" value="Search" name="_target0">

browser.button(:value, "Search").exists?
=> "true"

browser.button(:value, "Pesquisar").present?
=> true

browser.button(:name, "_target0").value
=> "Search"

This doesn't actually drive the button to get clicked,

browser.button(:name, "_target0").click

So I got the driven Firefox clicking the button using either

browser.button(:name, "_target0").fire_event('on_click')
browser.button(:name, "_target0").when_present.click

but what are the differences between them?

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

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

发布评论

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

评论(3

海夕 2025-01-10 17:11:31

至于它们之间的差异:

  • .click = 模拟在对象上单击鼠标左键。
  • .fire_event
    = 执行 JavaScript,即使该 JavaScript 可能无法通过鼠标单击正常访问。
  • when_present.click = 等待对象
    既可用又出现在可视区域(完整浏览器
    窗口),然后再尝试单击。

when_present 在您的网站使用 AJAX 时非常有用,并且与一个对象交互会导致另一个对象最终出现。使用 .click 可能会尝试在第二个对象可用之前单击它,并且脚本将会失败。

您的页面很可能包含 AJAX 表单,并且您尝试与之交互的按钮不会立即加载,而是在短暂的延迟后加载:

  • 输入一个文本字段,
  • 单击另一个按钮
  • ,页面完成生成内容

。不查找按钮的物理表示,而是查找源中的 JS 事件,它可以在按钮存在/可见/可操作之前使用。

As far as the differences between them:

  • .click = simulates a left mouse click on the object.
  • .fire_event
    = executes a javascript even that may or may not be accessible normally via mouse click.
  • when_present.click = waits for the object
    to be both available and appear in the viewable area (full browser
    window) before it attempts to click.

when_present is useful for when your site uses AJAX, and interacting with one object causes another object to eventually appear. Using a .click may attempt to click the second object before it is available, and the script will fail.

It is likely that your page contains an AJAX form, and the button you are attempting to interact with does not load immediately, but after a short delay when:

  • a text field is entered
  • another button is clicked
  • the page finishes generating content
  • etc.

Since fire_event does not look for the physical representation of the button, but rather the JS event in the source, it can be used before the button is present/visible/actionable.

七秒鱼° 2025-01-10 17:11:31

您可以检查运行测试的浏览器的缩放级别吗?

我在使用 click 方法时遇到了同样的问题,但我可以通过将浏览器 (IE9) 的缩放级别设置回 100% 来“修复”该问题。

请注意,Internet Explorer 的 Selenium Webdriver 要求声明这是本机事件正常工作的要求。请参阅此处

Can you check the zoom level of the browser you run your test in?

I encountered the same problem with the click method, but I was able to "fix" it by setting the zoom level of my browser (IE9) back to 100%.

Note that the Selenium Webdriver requirements for Internet Explorer state this is a requirement for Native Events to work. see here

不必在意 2025-01-10 17:11:31

添加到这个故事。我遇到了同样的问题,按钮被 javascript 覆盖,将按钮文本从“保存”更改为“处理...”。使用 .click、.fire_event('onclick')、.fire_event('OnClientClick') 似乎没有任何效果。它只是在普通按钮 .click 语法前加上可以工作的 div 前缀。以下是最终的 Ruby 代码:

@browser.div(class: 'rb_DefaultLayoutDiv').button(id: OrderEntryPOM.save_id).click

Adding to this story. I encountered the same problem where the button is covered by javascript to change the button text from "Save" to "Processing...". Nothing seemed to work using .click, .fire_event('onclick'), .fire_event('OnClientClick'). It was only prefixing the normal button .click syntax with the div that would work. The following is the final Ruby code:

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