[rspec-users] specifying which button webrat should press

aslak hellesoy aslak.hellesoy at gmail.com
Thu Nov 20 00:06:16 EST 2008


On Thu, Nov 20, 2008 at 4:32 AM, Pau Cor <lists at ruby-forum.com> wrote:
> 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*?

give buttons unique dom ids and pass the id to #clicks_button ?

> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list