Rails测试确认框
发布日期:2021-09-29 20:08:35 浏览次数:3 分类:技术文章

本文共 4015 字,大约阅读时间需要 13 分钟。

普通的webrat和cucumber做这样的测试会比较费劲,特别是涉及ajax交互的时候,那么,看了篇关于用selenium在java测试的文章
见后,发现ruby的selenium封装并没有那么复杂,但是,确有一些技巧
实现如下:
Given /^I choose "([^"]*)" on the next confirmation$/ do |type|   selenium.send( "choose_#{type}_on_next_confirmation" ) end Given /^I choose$/ do   # selenium.send( "choose_#{type}_on_next_confirmation" )   selenium.get_confirmation() end
Scenario: Successfully remove page  if choose ok     Given I click on "Show/Hide"     And I should see "My"     And I choose "ok" on the next confirmation     And I click on "remove page "     And I choose     When I click on "Show/Hide"     Then I should see "1 pages"     When I click on "Show/Hide"     Then I should see "My"
主要用到了selenium_client的几个方法:
def get_confirmation()             return string_command("getConfirmation", [])        end
[quote]
Retrieves the message of a JavaScript confirmation dialog generated during the previous action.
By default, the confirm function will return true, having the same effect as manually clicking OK. This can be changed by prior execution of the chooseCancelOnNextConfirmation command.
If an confirmation is generated but you do not consume it with getConfirmation, the next Selenium action will fail.
NOTE: under Selenium, JavaScript confirmations will NOT pop up a visible dialog.
NOTE: Selenium does NOT support JavaScript confirmations that are generated in a page‘s onload() event handler. In this case a visible dialog WILL be generated and Selenium will hang until you manually click OK.
[/quote]
def choose_ok_on_next_confirmation()              remote_control_command("chooseOkOnNextConfirmation", [])          end
def choose_cancel_on_next_confirmation()             remote_control_command("chooseCancelOnNextConfirmation", [])          end
上面两个方法的源代码说明,是调用selenium自己的对应方法,也就是封装,这时可以参考selenium自己的API看这个两个方法怎么用。
[quote]
By default, Selenium‘s overridden window.confirm() function will return true, as if the user had manually clicked OK; after running this command, the next call to confirm() will return false, as if the user had clicked Cancel. Selenium will then resume using the default behavior for future confirmations, automatically returning true (OK) unless/until you explicitly call this command for each confirmation.
Take note - every time a confirmation comes up, you must consume it with a corresponding getConfirmation, or else the next selenium operation will fail.
[/quote]
[quote]
Undo the effect of calling chooseCancelOnNextConfirmation. Note that Selenium‘s overridden window.confirm() function will normally automatically return true, as if the user had manually clicked OK, so you shouldn‘t need to use this command unless for some reason you need to change your mind prior to the next confirmation. After any confirmation, Selenium will resume using the default behavior for future confirmations, automatically returning true (OK) unless/until you explicitly call chooseCancelOnNextConfirmation for each confirmation.
Take note - every time a confirmation comes up, you must consume it with a corresponding getConfirmation, or else the next selenium operation will fail.
[/quote]
[quote]
Selenium处理弹出窗口
分类:
对网页弹出窗口,如WIKI所述,若要保持脚本运行稳定,必须在waitForPopUp这个弹出窗口之后紧跟运行selectWindow命令选中这个弹出窗口(示例),如果仍不稳定请参考这个示例。这里介绍了chooseCancelOnNextConfirmation、chooseOkOnNextConfirmation等JavaScript脚本实现的弹出窗口处理函数,selenium会弹出网页窗口,因为它重写了window.open在文件selenium-browserbot.js函数BrowserBot.prototype.modifyWindowToRecordPopUpDialogs中的newOpen,但这必须在window.onload之后创建才有效。对于HTTPS安全性弹出窗口证书的处理,见Selenium RC。
对非网页弹出窗口,如window.alert,window.confirm,window.prompt,window. showModalDialog等,有如下方法:
1.封装Windows Api,对Java语言则有Java Native Interface (JNI)或者J/Invoke(示例)。
2.Selenium RC中开启proxy injection(PI)模式也可以识别,这种模式提供了一个HTTP代理在浏览器之前自动更改所有接收到的HTML。window.alert, window.confirm,window.prompt在文件selenium-browserbot.js函数BrowserBot.prototype.modifyWindowToRecordPopUpDialogs中被覆写。
3.这里用window.open覆写了window. showModalDialog,同样实现的还有在文件selenium-browserbot.js函数BrowserBot.prototype._modifyWindow实现开始部分添加对ModalDialog的实现。
[/quote]

转载地址:https://blog.csdn.net/horace_lee/article/details/83712699 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:TextMate伴侣 Ackmate
下一篇:脱离Rails环境用ruby对普通网页进行测试

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月27日 02时17分32秒