加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 当按钮名称以相同的单词开头时,如何使用cucumb

发布时间:2020-12-17 03:43:47 所属栏目:百科 来源:网络整理
导读:我有以下带有多个输入的html: input type="submit" value="Save and close" name="commit"/input type="submit" value="Save" name="commit"/ 并且想用黄瓜测试点击“保存”按钮.但是,当我在黄瓜测试中这样做时: When I press "Save" 它点击“保存并关闭”
我有以下带有多个输入的html:

<input type="submit" value="Save and close" name="commit"/>
<input type="submit" value="Save" name="commit"/>

并且想用黄瓜测试点击“保存”按钮.但是,当我在黄瓜测试中这样做时:

When I press "Save"

它点击“保存并关闭”按钮,因为它出现在“保存”按钮之前.

查看用于查找按钮的webrat源:

def button_element
  button_elements.detect do |element|
    @value.nil?             ||
    matches_id?(element)    ||
    matches_value?(element) ||
    matches_html?(element)  ||
    matches_alt?(element)
  end
end

...

def matches_value?(element)
  element["value"] =~ /^W*#{Regexp.escape(@value.to_s)}/i
end

...

似乎webrat接受第一场比赛,并且只从内容的开头匹配.

有没有办法完全匹配,所以黄瓜找到“保存”并忽略“保存并关闭”?

解决方法

click_utton()方法,Cucumber用于“当我按…”时,它采用三个参数之一(文本,名称,id).您可以使用id或name属性简单地区分按钮以指定其中任何一个.

<input type="submit" value="Save and close" name="commit" id="close_after_save"/>
<input type="submit" value="Save" name="commit" id="save"/>

然后说:

When I press "save"
When I press "close_after_save"

或者,您可以在div中范围内的每个按钮.

<div id="save_and_close">
  <input type="submit" value="Save and close" name="commit"/>
</div>
<div id="save">
  <input type="submit" value="Save" name="commit" id="save"/>
</div>

然后你可以使用click_button()方法的范围:

When /^I press "([^"]*)" within "([^"]*)"$/ do |button,scope_selector|
  within(scope_selector) do      
    click_button(button)
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读