watir-webdriver - 单击 Javascript 按钮 [英] watir-webdriver - clicking Javascript button
问题描述
第一周一般是 watir-webdriver 和 Web 应用程序测试,因此仍在尝试围绕一些概念.
First week with watir-webdriver and Web app testing in general, so still trying to wrap some concepts around.
有这个 javascript 元素:
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
所以我让驱动的 Firefox 使用任一按钮点击按钮
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?
推荐答案
至于它们之间的区别:
.click
= 模拟鼠标左键单击对象..fire_event
= 执行一个 javascript,即使它可能会或可能无法通过鼠标点击正常访问.when_present.click
= 等待对象既可用又出现在可视区域(完整的浏览器窗口),然后再尝试点击.
.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
在您的站点使用 AJAX 并且与一个对象交互导致另一个对象最终出现时很有用.使用 .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.
您的页面可能包含 AJAX 表单,并且您尝试与之交互的按钮不会立即加载,而是会在以下情况下短暂延迟加载:
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:
- 输入了一个文本字段
- 点击了另一个按钮
- 页面完成生成内容
- 等
由于 fire_event 不查找按钮的物理表示,而是查找源中的 JS 事件,因此它可以在按钮出现/可见/可操作之前使用.
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.
这篇关于watir-webdriver - 单击 Javascript 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!