Rails selenium测试 定位和一些事件解释
发布日期:2021-09-29 20:08:32 浏览次数:2 分类:技术文章

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

最近在写cucumber测试的过程中,遇到一些问题,本来打算系统的整理一下,看样子没有这个时间,就能记多少记多少,杂乱无章的收吧。
在Rails项目中,越来越多的使用AJAX的时候,验收测试页面用cucumber就不行了,因为,Cucumber本身是使用webrat的模拟浏览器行为。
那么,就需要用selenium来测试,这时候,一些事件的封装就不太一样了。
比如:
<%= image_submit_tag "image_file_name", :id => "submit" %>
你可以写
And I press "submit"
实际上,我们查看step定义我们知道press是调用的click_button
如果,我们页面ajax调用又多个submit按钮,或者多个input框,而且用同样的name,(也有时候就是循环产生的)
那么在cucumber下我们可能要用scope
When /^I (press|follow|check|uncheck|choose) "([^\"]*)" for (.*) whose (.*) is "([^\"]*)"$/ do |action, whatyouclick, class_name, var_name, value|   unless var_name == "id" then     id = eval("\"#{class_name}\".classify.constantize.find_by_#{var_name}(\"#{value}\").id.to_s")   else     id = value   end   within("tr[id=row_#{class_name}_#{id}]") do     case action       when "press"         click_button(whatyouclick)       when "follow"         click_link(whatyouclick)       when "check"         check(whatyouclick)       when "uncheck"         uncheck(whatyouclick)       when "choose"         uncheck(whatyouclick)     end   end end
然而,我们尝试就会发现selenium并不支持within
那么就需要使用封装了的selenium语法进行测试
When /(?:|I )press "([^"]*)" via ajax$/ do |locator|   selenium.click "css=#{locator}" end When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)" via ajax$/ do |locator, value|   selenium.type( "css=#{locator}",value ) end Given /^I check "([^"]*)" via ajax$/ do |locator|   selenium.check "css=#{locator}" end
其中,locator可以支持的语法包括
[quote]
id=id
name=name
dom=javascriptExpression
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
[/quote]
更详细的信息可以参考
[url]http://seleniumhq.org/docs/04_selenese_commands.html[/url]
[url]http://release.seleniumhq.org/selenium-core/0.8.0/reference.html[/url]
关于用selenium 定位一个属性是否在一个html范围内
[url]见下篇[/url]

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

上一篇:the rspec book 下载
下一篇:在单双行显示不同的颜色Rails的一些view helper

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年03月28日 00时41分15秒