4
votes

I am using RSpec and Capybara for Ruby on Rails testing.

My Rails app is localized for a number of different languages (English, German, etc.).

For example, I would like Capybara to submit a form but obviously can't use its value to select it because the value changes depending on the language that has been chosen.

This won't work in my case:

click_button("Create my account")

Is there any way to simply select the first input[type="submit"] element on the page with Capybara?

Thanks for any help.

4
BTW: Using an ID is the way to go... But, normally testing functionality of clicking a button should not be affected by different localization. I always create tests against the default locale label... 'en' in my case.Jon Kern

4 Answers

10
votes
8
votes

I'd say:

find('input[type="submit"]').first.click

but give it a class or an id, it's much safer.

1
votes

Simple way:

first('input[type="submit"]').click
1
votes

You can also narrow down @apneadiving's answer using css selectors:

find("#my_form input[type='submit']").click

That way you don't have to modify the HTML to match your tests.