[rspec-users] Working with the response DOM - Step implementations using Hpricot

Andy Watts andywatts at yahoo.com
Thu Oct 25 00:29:26 EDT 2007


I'm playing with step implementations that use hpricot to manipulate
response.body.
Here's what I have so far.  
Very much work-in-progress, but may be of some use to you.

#This step implementation takes a button's label, collects the inputs and
does the POST/PUT
add.when("I click the $button_text button") do |button_text|                                                                                  
   h = Hpricot(response.body)                                                                                                                        
   form = h.search("form[button][text()=#{button_text}]").first
   action = form[:action]                                                                                                                            
   method = form[:method]
   headers = {'HTTP_REFERER' => request.env['REQUEST_URI']}                                                                                             
   params = {}
   form.search('input').each{|i| params[i[:name]] = i[:value] }                                                                                      
   send(method, action, params, headers)
end  


#This step implementation takes an input field & value, and updates the
response.body
add.when("I set the $input_field to $value") do |input_field,value|                                                                                 
  input_field.each{|s| s = s.sub!(/^'(.*?)'$/,'\1') }  # strip the quotes if
used in the input field
  input_field.gsub!(' ', '_')                            # join to get an id
to search for..not robust enough?            
  h = Hpricot(response.body)                             # get body in
Hpricot
  field_element = h.search("input##{input_field}").first # find the input
field                                                                      
  field_element[:value] = value                          # update the
field's value
  response.body = h.to_html                              # put the modified
body back in the response                                          
end

- Andy





Jonathan Linowes wrote:
> 
> I've been wondering the same for quite a while
> http://www.nabble.com/simple-story,-extract-link-t4518721.html
> but hadnt asked the question so succinctly
> 
> In that thread, Pat suggests helpers like click_link and submit_form
> If you progress along these lines, please post your solution, I'm  
> very interested.
> 
> linoj
> 
> 
> On Oct 24, 2007, at 12:43 AM, Andy Watts wrote:
> 
>>
>> I completely forgot about Hpricot
>> http://code.whytheluckystiff.net/hpricot/
>>
>> Looks perfect for this.
>>
>> - Andy
>>
>>
>>
>> Andy Watts wrote:
>>>
>>> Hi,
>>>
>>> Anyone know a way to traverse/query the DOM in a response?
>>>
>>> I'm trying to write reusable story step implementations and as  
>>> much as
>>> possible want to work with the actual response from a previous GET.
>>>
>>> As an example, I often have buttons that POST/PUT a hidden value.
>>> My goal is a reusable step implementation that takes the button  
>>> label and
>>> works the existing response DOM to make an appropriate POST/PUT.
>>>
>>> - Andy
>>>
>>>
>>
>> -- 
>> View this message in context: http://www.nabble.com/Working-with- 
>> the-response-DOM-tf4681975.html#a13378962
>> Sent from the rspec-users mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
> 
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
> 
> 

-- 
View this message in context: http://www.nabble.com/Working-with-the-response-DOM-tf4681975.html#a13400114
Sent from the rspec-users mailing list archive at Nabble.com.



More information about the rspec-users mailing list