SomberClock
SomberClock

Reputation: 151

Capybara unable to click form button in modal and different capybara drivers see different things

Ok, so my problem is Capybara cannot click the submit button of a form (generated with simple forms) that resides in a modal(Bootstrap v2.3). Please note that the following code is very messy learners code. I am attempting to get it tested up so that I can refactor the hell out of it.

Modal code:

<div class="modal hide" id="updateModal">
<button type="button" class="close" data-dismiss="modal">×</button>
    <div class="modal-header"
      <h3>Update your score</h3>
    </div>
      <div class="modal-body">
        <%= simple_form_for @update do |f| %>
          <%= render 'shared/error_messages', object: f.object %>
          <%= f.input :newread , :label => "Amount Read", :placeholder => 'pages/screens/minutes #' , :input_html => { :maxlength => 5 } %>
          <%= f.input :medium, :label=> "Medium Read", :collection => ["book", "manga", "game", "fgame", "net", "lyric", "subs", "news", "sent", "nico" ], :prompt => "Select medium read" %>
          <% lang_list = Update::user_langs(current_user,ApplicationHelper::curr_round) %>
          <%= f.input :lang, :label => "Language", :collection => lang_list, :prompt => "Select your language" %>
          <%= f.input :repeat, :label => "Repeat number", :collection =>0..50 , :priority => '0' %>
          <%= f.input :dr, :inline_label => 'Double Rowed?', :hint => 'Only to be used with Japanese books', :label => false %>
      </div>
      <div class="modal-footer">
        <%= submit_tag 'Cancel', :class => "btn btn-danger", 'data-dismiss' => "modal" %>
        <%= f.button :submit, 'Submit Update' , :class => "btn btn-primary"%>
      </div>
        <% end %>
</div>

Round Controller index function:

  def index
    @entrants = Round.includes(:user).where(:round_id => "#{ApplicationHelper::curr_round}")
    if @entrants == nil
      redirect_to root_url, :flash => { :error => "There are currently no users registered for this round." }
    end

    list = Round.where(:round_id => ApplicationHelper::curr_round).select(:tier).uniq
    lang_list = Update.where(:round_id => ApplicationHelper::curr_round).select(:lang).uniq
    @tier = list.map(&:tier)
    @tier = @tier.sort{ |a,b|  Tier::TIER_VALUES[a.to_sym] <=> Tier::TIER_VALUES[b.to_sym]}
    @lang = lang_list.map(&:lang)
    if signed_in?
      @update = current_user.updates.build
    end
  end

Update_page_spec:

describe "Update Pages" do

before do
  sign_in #omniauth fake signin
end

subject { page }

  describe "a registered user submitting an update", :js => true do
    before do
      user = User.find_by_uid(123545)
      user_round = user.rounds.create!(round_id: ApplicationHelper::curr_round, lang1: 'jp',
                                                      lang2: 'en', lang3:'zh', tier: 'Bronze', book:  10, manga: 10,
                                                      fgame: 10, game: 10, net: 10, news: 10, lyric: 10,
                                                      subs: 10, nico: 10, sent:10, pcount: 1010)
      visit round_path(ApplicationHelper.curr_round)
    end

    it "should update successfully" do
      click_link("Update")
      fill_in('update[newread]', :with => '10')
      select "book", :from => "Medium Read"
      select "Japanese", :from => "Language"
      click_button "Submit Update"
      save_and_open_page
      page.should have_selector('alert-success', :text => "Update successfully submitted")
    end
  end
end

So I do this and when I check what save_and_open_page sees and its the page with no changes at all. No evidence of the button having been pressed what so ever. So I figure that the modal being js generated might be a problem so I add , :js => true to the describe line, install the webkit driver and add Capybara.javascript_driver = :webkit to my spec_helper.rb file and run it again.

This time I am greeted to the "Signed in" flash from the top before block on the home page instead of being on the ranking page.

So I figure maybe this might turn out better with the selenium driver so I install that and try it again but this time my app complains about there being no one registered for that round. The only way for this to happen is if @entrants is nil and I have checked with pry that this is definitely not the case at least as far as the database is concerned.

Any help you guys can give will be GREATLY appreciated. I have no idea how to get this thing to press my button.

Upvotes: 4

Views: 1147

Answers (1)

brookr
brookr

Reputation: 1515

My recommendation:

  • Clean up the code as much as you can. That will help clarify what is happening, and what may be responsible for the unexpected behavior.
  • Don't rely on save_and_open_page to accurately show you what the driver is seeing exactly
  • You definitely need js for the Bootstrap modal
  • Get selenium working with Firefox so you can see on your screen if the modal is opening properly or not.
  • You may need the database_cleaner gem to properly access your db with the users you are creating.
  • you can also try the poltergeist/phantom gems for a js compatible driver. That has screenshot capabilities

Those pieces should show you what's happening with the modal.

Upvotes: 1

Related Questions