Capybara Support for Multiple Submit Button Types

We had a case where we wanted to create smoke tests to ensure each customer’s subdomain was working (used pre- and post-deployment). Unfortunately, not all of the customer’s login pages were consistent in how to “submit” the login form 🙁

The Cucumber tests looked something like this:

  Scenario Outline: Check that the navigation is correctly customized per org
    Given I login as "<user>" of "<org>"
    And I am on the "<page>" page
    Then I should see only the expected "<menu_items>" in top nav bar
    | user       | org      | page | menu_items                                      |
    | acme-admin | acme     | Home | Home, Preferences, Feedback, Help, Logout       |
    | wyle-admin | | Home | Home, Preferences, Links, Help, BI Tool, Logout |

Here is how I used Capybara’s find() method to get around this issue:

# A bit of a hack, org_name is normally a subdomain, but sometimes it is the complete domain
def login(user, org_name)
  # Use the below to automatically hit each user's org's server
  if org_name.include? '.com'
    Capybara.app_host = "http://#{org_name}"
    Capybara.app_host = "http://#{org_name}"

  visit '/'
  fill_in 'username', :with => user
  fill_in 'userpwd', :with => '***'
    click_on 'submit'
  rescue Capybara::ElementNotFound
    page.find(:link_or_button, 'Log In')
    click_on 'Log In'
    rescue Capybara::ElementNotFound
      pending "Need to determine how to invoke the Login button for #{org_name} near Line ##{__LINE__} of #{__method__} in #{__FILE__} "

  # Ensure that login was successful
  page.should_not have_content 'Login failed'

Since our analysts write a bunch of these tests, I also added in a helpful error message should a new org get added that has yet another style of login.