Capybara::ElementNotFound: Unable to find css “#transaction_form”
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
add a comment |
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
add a comment |
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
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
ruby-on-rails rspec capybara
asked Nov 14 '18 at 11:24
MoriartyMoriarty
577
577
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
answered Nov 14 '18 at 17:24
Thomas WalpoleThomas Walpole
32.3k33153
32.3k33153
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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