Capybara::ElementNotFound: Unable to find css “#transaction_form”










0















I am learning to write feature specs using rspec and capybara. I am trying to write spec for an application, which handles transactions. My transaction controller is as follows:



 def new
@transaction = Transaction.new
end

def create
transaction = Transaction.new(transaction_params)
transaction.account = current_account
if transaction.save && transaction.account.save
flash[:success] = 'Transaction successfull'
else
flash[:danger] = 'Insufficient balance'
end
redirect_to root_path
end


It's view is as follows transactions/new:



<div class = 'row'>
<div class = 'col-xs-12'>
<%= form_for(@transaction, id: 'transaction_form', :html => class: 'form-horizontal', role: 'form') do |t| %>
<div class = 'form-group'>
<div class = 'control-label col-sm-2'>
<%= t.label :amount %>
</div>

<div class = 'col-sm-8'>
<%= t.text_field :amount, class: 'form-control', placeholder: 'Enter amount', autofocus: true %>
</div>
</div>

<div class = 'form-group'>
<div class = 'control-label col-sm-2'>
<%= t.label :transaction_type %>
</div>

<div class = 'col-sm-8'>
<%= t.select :transaction_type, Transaction.transaction_types.keys %>
</div>
</div>

<div class = 'form-group'>
<div class = 'col-sm-offset-2 col-sm-10'>
<%= t.submit 'Submit', class: 'btn btn-primary btn' %>
</div>
</div>
<% end %>




I added id: transaction_form to form to avoid ambiguous error.
The spec code is as follows:



RSpec.feature 'Transactions', type: :feature do
context 'create new transaction' do
scenario 'should be successfull' do
visit new_transaction_path
within('#transaction_form') do
fill_in 'Amount', with: '60'
end
click_button 'Submit'
expect(page).to have_content('Transaction successfull')
end
end
end


On running this spec, however,I get error as:



 1) Transactions create new transaction should be successfull
Failure/Error:
within('#transaction_form') do
fill_in 'Amount', with: '60'
end

Capybara::ElementNotFound:
Unable to find css "#transaction_form"


What is it I am missing? If I use form directly, it is throwing the ambiguous error as it is getting the same element from different file. What is wrong with this code?



Also, the /transactions/new page will be displayed only if the user is logged in. So will this also affect the transaction spec? If yes, then what should be done?



Please help. Thanks in advance.










share|improve this question


























    0















    I am learning to write feature specs using rspec and capybara. I am trying to write spec for an application, which handles transactions. My transaction controller is as follows:



     def new
    @transaction = Transaction.new
    end

    def create
    transaction = Transaction.new(transaction_params)
    transaction.account = current_account
    if transaction.save && transaction.account.save
    flash[:success] = 'Transaction successfull'
    else
    flash[:danger] = 'Insufficient balance'
    end
    redirect_to root_path
    end


    It's view is as follows transactions/new:



    <div class = 'row'>
    <div class = 'col-xs-12'>
    <%= form_for(@transaction, id: 'transaction_form', :html => class: 'form-horizontal', role: 'form') do |t| %>
    <div class = 'form-group'>
    <div class = 'control-label col-sm-2'>
    <%= t.label :amount %>
    </div>

    <div class = 'col-sm-8'>
    <%= t.text_field :amount, class: 'form-control', placeholder: 'Enter amount', autofocus: true %>
    </div>
    </div>

    <div class = 'form-group'>
    <div class = 'control-label col-sm-2'>
    <%= t.label :transaction_type %>
    </div>

    <div class = 'col-sm-8'>
    <%= t.select :transaction_type, Transaction.transaction_types.keys %>
    </div>
    </div>

    <div class = 'form-group'>
    <div class = 'col-sm-offset-2 col-sm-10'>
    <%= t.submit 'Submit', class: 'btn btn-primary btn' %>
    </div>
    </div>
    <% end %>




    I added id: transaction_form to form to avoid ambiguous error.
    The spec code is as follows:



    RSpec.feature 'Transactions', type: :feature do
    context 'create new transaction' do
    scenario 'should be successfull' do
    visit new_transaction_path
    within('#transaction_form') do
    fill_in 'Amount', with: '60'
    end
    click_button 'Submit'
    expect(page).to have_content('Transaction successfull')
    end
    end
    end


    On running this spec, however,I get error as:



     1) Transactions create new transaction should be successfull
    Failure/Error:
    within('#transaction_form') do
    fill_in 'Amount', with: '60'
    end

    Capybara::ElementNotFound:
    Unable to find css "#transaction_form"


    What is it I am missing? If I use form directly, it is throwing the ambiguous error as it is getting the same element from different file. What is wrong with this code?



    Also, the /transactions/new page will be displayed only if the user is logged in. So will this also affect the transaction spec? If yes, then what should be done?



    Please help. Thanks in advance.










    share|improve this question
























      0












      0








      0








      I am learning to write feature specs using rspec and capybara. I am trying to write spec for an application, which handles transactions. My transaction controller is as follows:



       def new
      @transaction = Transaction.new
      end

      def create
      transaction = Transaction.new(transaction_params)
      transaction.account = current_account
      if transaction.save && transaction.account.save
      flash[:success] = 'Transaction successfull'
      else
      flash[:danger] = 'Insufficient balance'
      end
      redirect_to root_path
      end


      It's view is as follows transactions/new:



      <div class = 'row'>
      <div class = 'col-xs-12'>
      <%= form_for(@transaction, id: 'transaction_form', :html => class: 'form-horizontal', role: 'form') do |t| %>
      <div class = 'form-group'>
      <div class = 'control-label col-sm-2'>
      <%= t.label :amount %>
      </div>

      <div class = 'col-sm-8'>
      <%= t.text_field :amount, class: 'form-control', placeholder: 'Enter amount', autofocus: true %>
      </div>
      </div>

      <div class = 'form-group'>
      <div class = 'control-label col-sm-2'>
      <%= t.label :transaction_type %>
      </div>

      <div class = 'col-sm-8'>
      <%= t.select :transaction_type, Transaction.transaction_types.keys %>
      </div>
      </div>

      <div class = 'form-group'>
      <div class = 'col-sm-offset-2 col-sm-10'>
      <%= t.submit 'Submit', class: 'btn btn-primary btn' %>
      </div>
      </div>
      <% end %>




      I added id: transaction_form to form to avoid ambiguous error.
      The spec code is as follows:



      RSpec.feature 'Transactions', type: :feature do
      context 'create new transaction' do
      scenario 'should be successfull' do
      visit new_transaction_path
      within('#transaction_form') do
      fill_in 'Amount', with: '60'
      end
      click_button 'Submit'
      expect(page).to have_content('Transaction successfull')
      end
      end
      end


      On running this spec, however,I get error as:



       1) Transactions create new transaction should be successfull
      Failure/Error:
      within('#transaction_form') do
      fill_in 'Amount', with: '60'
      end

      Capybara::ElementNotFound:
      Unable to find css "#transaction_form"


      What is it I am missing? If I use form directly, it is throwing the ambiguous error as it is getting the same element from different file. What is wrong with this code?



      Also, the /transactions/new page will be displayed only if the user is logged in. So will this also affect the transaction spec? If yes, then what should be done?



      Please help. Thanks in advance.










      share|improve this question














      I am learning to write feature specs using rspec and capybara. I am trying to write spec for an application, which handles transactions. My transaction controller is as follows:



       def new
      @transaction = Transaction.new
      end

      def create
      transaction = Transaction.new(transaction_params)
      transaction.account = current_account
      if transaction.save && transaction.account.save
      flash[:success] = 'Transaction successfull'
      else
      flash[:danger] = 'Insufficient balance'
      end
      redirect_to root_path
      end


      It's view is as follows transactions/new:



      <div class = 'row'>
      <div class = 'col-xs-12'>
      <%= form_for(@transaction, id: 'transaction_form', :html => class: 'form-horizontal', role: 'form') do |t| %>
      <div class = 'form-group'>
      <div class = 'control-label col-sm-2'>
      <%= t.label :amount %>
      </div>

      <div class = 'col-sm-8'>
      <%= t.text_field :amount, class: 'form-control', placeholder: 'Enter amount', autofocus: true %>
      </div>
      </div>

      <div class = 'form-group'>
      <div class = 'control-label col-sm-2'>
      <%= t.label :transaction_type %>
      </div>

      <div class = 'col-sm-8'>
      <%= t.select :transaction_type, Transaction.transaction_types.keys %>
      </div>
      </div>

      <div class = 'form-group'>
      <div class = 'col-sm-offset-2 col-sm-10'>
      <%= t.submit 'Submit', class: 'btn btn-primary btn' %>
      </div>
      </div>
      <% end %>




      I added id: transaction_form to form to avoid ambiguous error.
      The spec code is as follows:



      RSpec.feature 'Transactions', type: :feature do
      context 'create new transaction' do
      scenario 'should be successfull' do
      visit new_transaction_path
      within('#transaction_form') do
      fill_in 'Amount', with: '60'
      end
      click_button 'Submit'
      expect(page).to have_content('Transaction successfull')
      end
      end
      end


      On running this spec, however,I get error as:



       1) Transactions create new transaction should be successfull
      Failure/Error:
      within('#transaction_form') do
      fill_in 'Amount', with: '60'
      end

      Capybara::ElementNotFound:
      Unable to find css "#transaction_form"


      What is it I am missing? If I use form directly, it is throwing the ambiguous error as it is getting the same element from different file. What is wrong with this code?



      Also, the /transactions/new page will be displayed only if the user is logged in. So will this also affect the transaction spec? If yes, then what should be done?



      Please help. Thanks in advance.







      ruby-on-rails rspec capybara






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 11:24









      MoriartyMoriarty

      527




      527






















          1 Answer
          1






          active

          oldest

          votes


















          0














          If the page you want to interact with is only visible when the user is logged in, then you need to log the user in. That also means you will need to create the user you're going to log in before the test starts. Usually that would be done using either Rails fixtures, or a factory (like the factory_bot gem). Once you have create the user then you'll need to log them in, which can be as simple as visiting the login page and entering the users username and password. If you're using a gem for authentication it may provide a test mode which allows for bypassing actually visiting the login page in order to speed up tests (ie. devise provides this - https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara)






          share|improve this answer























          • Ok i'll try this.

            – Moriarty
            Nov 15 '18 at 4:57











          • I used warden to create user to perform the transactions. The user is getting logged in properly. But still I am getting the same error of 'Unable to find css'. Is there anything wrong in giving id to the form of new_transaction?

            – Moriarty
            Nov 15 '18 at 6:59











          • The problem is solved. Thankyou.

            – Moriarty
            Nov 15 '18 at 7:24










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299090%2fcapybaraelementnotfound-unable-to-find-css-transaction-form%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          If the page you want to interact with is only visible when the user is logged in, then you need to log the user in. That also means you will need to create the user you're going to log in before the test starts. Usually that would be done using either Rails fixtures, or a factory (like the factory_bot gem). Once you have create the user then you'll need to log them in, which can be as simple as visiting the login page and entering the users username and password. If you're using a gem for authentication it may provide a test mode which allows for bypassing actually visiting the login page in order to speed up tests (ie. devise provides this - https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara)






          share|improve this answer























          • Ok i'll try this.

            – Moriarty
            Nov 15 '18 at 4:57











          • I used warden to create user to perform the transactions. The user is getting logged in properly. But still I am getting the same error of 'Unable to find css'. Is there anything wrong in giving id to the form of new_transaction?

            – Moriarty
            Nov 15 '18 at 6:59











          • The problem is solved. Thankyou.

            – Moriarty
            Nov 15 '18 at 7:24















          0














          If the page you want to interact with is only visible when the user is logged in, then you need to log the user in. That also means you will need to create the user you're going to log in before the test starts. Usually that would be done using either Rails fixtures, or a factory (like the factory_bot gem). Once you have create the user then you'll need to log them in, which can be as simple as visiting the login page and entering the users username and password. If you're using a gem for authentication it may provide a test mode which allows for bypassing actually visiting the login page in order to speed up tests (ie. devise provides this - https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara)






          share|improve this answer























          • Ok i'll try this.

            – Moriarty
            Nov 15 '18 at 4:57











          • I used warden to create user to perform the transactions. The user is getting logged in properly. But still I am getting the same error of 'Unable to find css'. Is there anything wrong in giving id to the form of new_transaction?

            – Moriarty
            Nov 15 '18 at 6:59











          • The problem is solved. Thankyou.

            – Moriarty
            Nov 15 '18 at 7:24













          0












          0








          0







          If the page you want to interact with is only visible when the user is logged in, then you need to log the user in. That also means you will need to create the user you're going to log in before the test starts. Usually that would be done using either Rails fixtures, or a factory (like the factory_bot gem). Once you have create the user then you'll need to log them in, which can be as simple as visiting the login page and entering the users username and password. If you're using a gem for authentication it may provide a test mode which allows for bypassing actually visiting the login page in order to speed up tests (ie. devise provides this - https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara)






          share|improve this answer













          If the page you want to interact with is only visible when the user is logged in, then you need to log the user in. That also means you will need to create the user you're going to log in before the test starts. Usually that would be done using either Rails fixtures, or a factory (like the factory_bot gem). Once you have create the user then you'll need to log them in, which can be as simple as visiting the login page and entering the users username and password. If you're using a gem for authentication it may provide a test mode which allows for bypassing actually visiting the login page in order to speed up tests (ie. devise provides this - https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 17:24









          Thomas WalpoleThomas Walpole

          31.4k33052




          31.4k33052












          • Ok i'll try this.

            – Moriarty
            Nov 15 '18 at 4:57











          • I used warden to create user to perform the transactions. The user is getting logged in properly. But still I am getting the same error of 'Unable to find css'. Is there anything wrong in giving id to the form of new_transaction?

            – Moriarty
            Nov 15 '18 at 6:59











          • The problem is solved. Thankyou.

            – Moriarty
            Nov 15 '18 at 7:24

















          • Ok i'll try this.

            – Moriarty
            Nov 15 '18 at 4:57











          • I used warden to create user to perform the transactions. The user is getting logged in properly. But still I am getting the same error of 'Unable to find css'. Is there anything wrong in giving id to the form of new_transaction?

            – Moriarty
            Nov 15 '18 at 6:59











          • The problem is solved. Thankyou.

            – Moriarty
            Nov 15 '18 at 7:24
















          Ok i'll try this.

          – Moriarty
          Nov 15 '18 at 4:57





          Ok i'll try this.

          – Moriarty
          Nov 15 '18 at 4:57













          I used warden to create user to perform the transactions. The user is getting logged in properly. But still I am getting the same error of 'Unable to find css'. Is there anything wrong in giving id to the form of new_transaction?

          – Moriarty
          Nov 15 '18 at 6:59





          I used warden to create user to perform the transactions. The user is getting logged in properly. But still I am getting the same error of 'Unable to find css'. Is there anything wrong in giving id to the form of new_transaction?

          – Moriarty
          Nov 15 '18 at 6:59













          The problem is solved. Thankyou.

          – Moriarty
          Nov 15 '18 at 7:24





          The problem is solved. Thankyou.

          – Moriarty
          Nov 15 '18 at 7:24



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299090%2fcapybaraelementnotfound-unable-to-find-css-transaction-form%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo