[rspec-users] specifying which button webrat should press

Pau Cor lists at ruby-forum.com
Wed Nov 19 22:32:27 EST 2008


I was writing a feature in which I had webrat push a button. However,
there are many buttons on this page with the same text. I wanted to
specify which one would be push in as close a way I could to the way a
person would describe which one he was pushing. Here is what I ended up
with:

#custom_webrate_steps.rb
require 'rexml/document'
include REXML

When /^I push "(.*)" near "(.*)"$/ do |button, text|
  #The dom element that contains the text must have an id

  rexml_doc = Document.new(response.body)
  regexp = /#{Regexp.escape(text)}/
  nodes = find_nodes_with_regexp(rexml_doc, regexp)
  dom_id = nodes.first.attributes['id']

  within '#' + dom_id do |scope|
    scope.clicks_button(button)
  end
end


def find_nodes_with_regexp(node, regexp)
  result = []
  result << node if node.methods.include?('text') && node.text =~ regexp
  node.each {|n| result += find_nodes_with_regexp(n, regexp)} if
node.methods.include?('each')
  result
end


Any ideas for making this *nicer*?
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list