How can I click on a specific button with cucumber / webrat when the button name begins with the same word?

I have the following html with multiple inputs:

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

and I’d like to use a cucumber to check the click on the β€œSave” button. However, when I do this in a cucumber test:

 When I press "Save" 

he clicks the Save and Close button, as it appears before the Save button.

Looking at webrat source for button search:

 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 seems to accept the first match and only matches the beginning of the content.

Is there a way to make an exact match, so the cucumber finds "Save" and ignores "Save and Close"?

+4
source share
1 answer

The click_button () method, which Cucumber uses for "When I press ...", takes one of three parameters (text, name, identifier). You can simply distinguish buttons using the id or name attribute to indicate either.

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

Then say:

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

Alternatively, you can use each button in a 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> 

Then you can specify the click_button () method:

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

All Articles