click_button(value) not specific enough for my page

21 views
Skip to first unread message

lsiden

unread,
Nov 19, 2009, 5:31:51 PM11/19/09
to webrat
I am trying to test this form with cucumber: http://westside-consulting.com/tes/application/
(never mind the PHP errors at the bottom - they don't affect this
test). You will see that there are two forms, each with a button
where value="submit". Then if my script executes 'click_button
"submit" ' it submits the first form in the page which is the wrong
form. Looking in the code for Webrat I found in core/scope.rb

def click_button(value = nil)
find_button(value).click
end

then in core/locators/button_locator.rb:

def find_button(value) #:nodoc:
ButtonLocator.new(@session, dom, value).locate!
end

near the top of that:
def locate
ButtonField.load(@session, button_element)
end

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

etc.

So this is iterating over each button in the form, in the order in
which they appear, and then checking whether each of the attributes ==
@value in the above order. So if there is more than one button with
the same value, html, or alt, it just clicks the first one, which may
or may not be what I wanted!

Wouldn't it be more useful if I could say "click_button :id =>
'submit' "?

I'm new to Ruby but I would be happy to try my hand at submitting a
patch with a new method. Alas, Ruby does not seem to allow for
function overloading as does C++, so I would have to come up with a
new name in order to use this idiom. Would anyone like to suggest a
name? Would "click_button_with" work for people?

Matt Wynne

unread,
Nov 19, 2009, 5:51:42 PM11/19/09
to web...@googlegroups.com
Try using #within (which returns a scope)

As in

within('#form1') do
click_button
end
cheers,
Matt

+447974 430184
ma...@mattwynne.net
http://mattwynne.net

Larry Siden

unread,
Nov 19, 2009, 11:31:14 PM11/19/09
to web...@googlegroups.com
It worked!  Thanks!

Larry Siden
http://umich.edu/~lsiden
734-926-9614
skype: lsiden
Reply all
Reply to author
Forward
0 new messages