rubytestingwebrat

How do I fill in a specific field in Webrat when several share the same name and id?


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?


Solution

  • 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