views:

308

answers:

2

I'm just getting started with Cucumber and Webrat, and am adding feature specifications to an existing Rails application. One page in the app has multiple forms on it, representing different ways to build a new Character object. Here's a simplified example:

<form id="adopt_character_form">
    ....
    <input type="text" name="character[name]" id="character_name">
    ...
</form>

<form id="spawn_character_form">
    ....
    <input type="text" name="character[name]" id="character_name">
    ...
</form>

As you can see, there are (at least) two fields that have the same name and id on the page, though they are in different forms, and I can't find a way in the Webrat rdoc or source code to specify a particular one.

I know that I could change the name or id on one of them, but I would have to deviate from the Rails naming conventions to do so, which I'd really rather not. I could also put the forms on different pages, but it would take more modification to the work flow than I want to do. I could also try to merge them in to a single form that was more modular and dynamic, but having to make structural changes to the UI just to support a given testing framework seems a little questionable and might not always be feasible, plus I would have to require javascript.

Is there any way to specify one of these fields in Webrat, or should I just give up and try Cucumber on a different project?

A: 

Considering having multiple elements with the same ID is an explicit violation of HTML validity, I think you've already deviated from the path of Rails conventions. Recent versions of Rails generally make an effort to keep things standards-conformant. IIRC it's pretty straightforward to call the form helpers in such a way that the IDs are differentiated. If nothing else you can just supply an explicit ID with each call. For the purpose of Rails conventions the name field is the only one that matters, since that's the one Rails will have to map back to a field name on a model.

Avdi
+1  A: 

You can use webrats within method

For example in a cucumber steps file

#my_steps
Then /^I update character name with  "([^\"]*)"$/ do |updated_character|
  within '#adopt_character_form'
     fill_in "character[name]", :with => updated_character
  end
end

Hope that helps

Jonathan