Why can't I catch Capybara::ElementNotFound in the to_raise matcher?












0














pry#<> expect(find("#recipients")).to raise_error(Capybara::ElementNotFound)
Capybara::ElementNotFound: Unable to find visible css "#recipients"


I know it's hard and potentially error prone to test that certain elements are "not" found, but I feel confident my use case is a good one.



I'm trying to make a #dont_find matcher...










share|improve this question






















  • The answer by Alex D is correct but why not just do expect(page).not_to have_css('#recipients') or expect(page).to have_no_css('#receipients') which are actual expectations rather than a '#dont_find` method, or potentially a custom selector and/or helper methods have_recipients, etc - if you're looking to get more complicated.
    – Thomas Walpole
    Nov 20 at 19:18












  • This is a reasonable question, but there is a very subtle, yet extremely important difference between find and have_css. Find actually makes sure that the selector is "visible", whereas have_css doesn't. In my case the selector is actually on the page, but is hidden. I tried an implementation like this initially but it didn't work. Regrettably this isn't obvious from the documentation for #find. The exception shows it's looking for 'visible' elements: Capybara::ElementNotFound: Unable to find visible css "abc"
    – pixelearth
    Nov 20 at 21:32










  • You're wrong about that -- both find and have_css check visibility by default. If you have a case where it's not that would be a serious bug and you should file an issue on the Capybara project.
    – Thomas Walpole
    Nov 20 at 22:38












  • Just to clarify why I know I'm right on that - I am the current author of Capybara. So if you do have a case where have_css isn't verifying visibility please do file an issue on Capybara so I can confirm and fix.
    – Thomas Walpole
    Nov 20 at 23:00










  • Perhaps it wasn't has_css (has_css ?) that I used, but the first implementation I used returned true when the selector was on the page, visible or not.
    – pixelearth
    Nov 21 at 3:05
















0














pry#<> expect(find("#recipients")).to raise_error(Capybara::ElementNotFound)
Capybara::ElementNotFound: Unable to find visible css "#recipients"


I know it's hard and potentially error prone to test that certain elements are "not" found, but I feel confident my use case is a good one.



I'm trying to make a #dont_find matcher...










share|improve this question






















  • The answer by Alex D is correct but why not just do expect(page).not_to have_css('#recipients') or expect(page).to have_no_css('#receipients') which are actual expectations rather than a '#dont_find` method, or potentially a custom selector and/or helper methods have_recipients, etc - if you're looking to get more complicated.
    – Thomas Walpole
    Nov 20 at 19:18












  • This is a reasonable question, but there is a very subtle, yet extremely important difference between find and have_css. Find actually makes sure that the selector is "visible", whereas have_css doesn't. In my case the selector is actually on the page, but is hidden. I tried an implementation like this initially but it didn't work. Regrettably this isn't obvious from the documentation for #find. The exception shows it's looking for 'visible' elements: Capybara::ElementNotFound: Unable to find visible css "abc"
    – pixelearth
    Nov 20 at 21:32










  • You're wrong about that -- both find and have_css check visibility by default. If you have a case where it's not that would be a serious bug and you should file an issue on the Capybara project.
    – Thomas Walpole
    Nov 20 at 22:38












  • Just to clarify why I know I'm right on that - I am the current author of Capybara. So if you do have a case where have_css isn't verifying visibility please do file an issue on Capybara so I can confirm and fix.
    – Thomas Walpole
    Nov 20 at 23:00










  • Perhaps it wasn't has_css (has_css ?) that I used, but the first implementation I used returned true when the selector was on the page, visible or not.
    – pixelearth
    Nov 21 at 3:05














0












0








0







pry#<> expect(find("#recipients")).to raise_error(Capybara::ElementNotFound)
Capybara::ElementNotFound: Unable to find visible css "#recipients"


I know it's hard and potentially error prone to test that certain elements are "not" found, but I feel confident my use case is a good one.



I'm trying to make a #dont_find matcher...










share|improve this question













pry#<> expect(find("#recipients")).to raise_error(Capybara::ElementNotFound)
Capybara::ElementNotFound: Unable to find visible css "#recipients"


I know it's hard and potentially error prone to test that certain elements are "not" found, but I feel confident my use case is a good one.



I'm trying to make a #dont_find matcher...







rspec capybara






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 18:35









pixelearth

6,03943769




6,03943769












  • The answer by Alex D is correct but why not just do expect(page).not_to have_css('#recipients') or expect(page).to have_no_css('#receipients') which are actual expectations rather than a '#dont_find` method, or potentially a custom selector and/or helper methods have_recipients, etc - if you're looking to get more complicated.
    – Thomas Walpole
    Nov 20 at 19:18












  • This is a reasonable question, but there is a very subtle, yet extremely important difference between find and have_css. Find actually makes sure that the selector is "visible", whereas have_css doesn't. In my case the selector is actually on the page, but is hidden. I tried an implementation like this initially but it didn't work. Regrettably this isn't obvious from the documentation for #find. The exception shows it's looking for 'visible' elements: Capybara::ElementNotFound: Unable to find visible css "abc"
    – pixelearth
    Nov 20 at 21:32










  • You're wrong about that -- both find and have_css check visibility by default. If you have a case where it's not that would be a serious bug and you should file an issue on the Capybara project.
    – Thomas Walpole
    Nov 20 at 22:38












  • Just to clarify why I know I'm right on that - I am the current author of Capybara. So if you do have a case where have_css isn't verifying visibility please do file an issue on Capybara so I can confirm and fix.
    – Thomas Walpole
    Nov 20 at 23:00










  • Perhaps it wasn't has_css (has_css ?) that I used, but the first implementation I used returned true when the selector was on the page, visible or not.
    – pixelearth
    Nov 21 at 3:05


















  • The answer by Alex D is correct but why not just do expect(page).not_to have_css('#recipients') or expect(page).to have_no_css('#receipients') which are actual expectations rather than a '#dont_find` method, or potentially a custom selector and/or helper methods have_recipients, etc - if you're looking to get more complicated.
    – Thomas Walpole
    Nov 20 at 19:18












  • This is a reasonable question, but there is a very subtle, yet extremely important difference between find and have_css. Find actually makes sure that the selector is "visible", whereas have_css doesn't. In my case the selector is actually on the page, but is hidden. I tried an implementation like this initially but it didn't work. Regrettably this isn't obvious from the documentation for #find. The exception shows it's looking for 'visible' elements: Capybara::ElementNotFound: Unable to find visible css "abc"
    – pixelearth
    Nov 20 at 21:32










  • You're wrong about that -- both find and have_css check visibility by default. If you have a case where it's not that would be a serious bug and you should file an issue on the Capybara project.
    – Thomas Walpole
    Nov 20 at 22:38












  • Just to clarify why I know I'm right on that - I am the current author of Capybara. So if you do have a case where have_css isn't verifying visibility please do file an issue on Capybara so I can confirm and fix.
    – Thomas Walpole
    Nov 20 at 23:00










  • Perhaps it wasn't has_css (has_css ?) that I used, but the first implementation I used returned true when the selector was on the page, visible or not.
    – pixelearth
    Nov 21 at 3:05
















The answer by Alex D is correct but why not just do expect(page).not_to have_css('#recipients') or expect(page).to have_no_css('#receipients') which are actual expectations rather than a '#dont_find` method, or potentially a custom selector and/or helper methods have_recipients, etc - if you're looking to get more complicated.
– Thomas Walpole
Nov 20 at 19:18






The answer by Alex D is correct but why not just do expect(page).not_to have_css('#recipients') or expect(page).to have_no_css('#receipients') which are actual expectations rather than a '#dont_find` method, or potentially a custom selector and/or helper methods have_recipients, etc - if you're looking to get more complicated.
– Thomas Walpole
Nov 20 at 19:18














This is a reasonable question, but there is a very subtle, yet extremely important difference between find and have_css. Find actually makes sure that the selector is "visible", whereas have_css doesn't. In my case the selector is actually on the page, but is hidden. I tried an implementation like this initially but it didn't work. Regrettably this isn't obvious from the documentation for #find. The exception shows it's looking for 'visible' elements: Capybara::ElementNotFound: Unable to find visible css "abc"
– pixelearth
Nov 20 at 21:32




This is a reasonable question, but there is a very subtle, yet extremely important difference between find and have_css. Find actually makes sure that the selector is "visible", whereas have_css doesn't. In my case the selector is actually on the page, but is hidden. I tried an implementation like this initially but it didn't work. Regrettably this isn't obvious from the documentation for #find. The exception shows it's looking for 'visible' elements: Capybara::ElementNotFound: Unable to find visible css "abc"
– pixelearth
Nov 20 at 21:32












You're wrong about that -- both find and have_css check visibility by default. If you have a case where it's not that would be a serious bug and you should file an issue on the Capybara project.
– Thomas Walpole
Nov 20 at 22:38






You're wrong about that -- both find and have_css check visibility by default. If you have a case where it's not that would be a serious bug and you should file an issue on the Capybara project.
– Thomas Walpole
Nov 20 at 22:38














Just to clarify why I know I'm right on that - I am the current author of Capybara. So if you do have a case where have_css isn't verifying visibility please do file an issue on Capybara so I can confirm and fix.
– Thomas Walpole
Nov 20 at 23:00




Just to clarify why I know I'm right on that - I am the current author of Capybara. So if you do have a case where have_css isn't verifying visibility please do file an issue on Capybara so I can confirm and fix.
– Thomas Walpole
Nov 20 at 23:00












Perhaps it wasn't has_css (has_css ?) that I used, but the first implementation I used returned true when the selector was on the page, visible or not.
– pixelearth
Nov 21 at 3:05




Perhaps it wasn't has_css (has_css ?) that I used, but the first implementation I used returned true when the selector was on the page, visible or not.
– pixelearth
Nov 21 at 3:05












1 Answer
1






active

oldest

votes


















1














Your problem is that you are trying to pass find('#recipients') directly to expect, but this doesn't work, because find raises an exception and thus expect is never even called. You should be passing a block instead, like so:



expect { find('#recipients') }.to raise_error(Capybara::ElementNotFound)





share|improve this answer





















  • If you want to assert if an element exist it's a better solution use a capybara matcher like has_css? with an rspec expectation i think rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/… relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
    – bott
    Dec 12 at 12:34










  • @bott That is a good point. I just copied the OP's code and fixed his bug, but didn't think about writing the assertion in a totally different way. You could post another answer if you like.
    – Alex D
    Dec 14 at 5:38











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%2f53399407%2fwhy-cant-i-catch-capybaraelementnotfound-in-the-to-raise-matcher%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









1














Your problem is that you are trying to pass find('#recipients') directly to expect, but this doesn't work, because find raises an exception and thus expect is never even called. You should be passing a block instead, like so:



expect { find('#recipients') }.to raise_error(Capybara::ElementNotFound)





share|improve this answer





















  • If you want to assert if an element exist it's a better solution use a capybara matcher like has_css? with an rspec expectation i think rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/… relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
    – bott
    Dec 12 at 12:34










  • @bott That is a good point. I just copied the OP's code and fixed his bug, but didn't think about writing the assertion in a totally different way. You could post another answer if you like.
    – Alex D
    Dec 14 at 5:38
















1














Your problem is that you are trying to pass find('#recipients') directly to expect, but this doesn't work, because find raises an exception and thus expect is never even called. You should be passing a block instead, like so:



expect { find('#recipients') }.to raise_error(Capybara::ElementNotFound)





share|improve this answer





















  • If you want to assert if an element exist it's a better solution use a capybara matcher like has_css? with an rspec expectation i think rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/… relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
    – bott
    Dec 12 at 12:34










  • @bott That is a good point. I just copied the OP's code and fixed his bug, but didn't think about writing the assertion in a totally different way. You could post another answer if you like.
    – Alex D
    Dec 14 at 5:38














1












1








1






Your problem is that you are trying to pass find('#recipients') directly to expect, but this doesn't work, because find raises an exception and thus expect is never even called. You should be passing a block instead, like so:



expect { find('#recipients') }.to raise_error(Capybara::ElementNotFound)





share|improve this answer












Your problem is that you are trying to pass find('#recipients') directly to expect, but this doesn't work, because find raises an exception and thus expect is never even called. You should be passing a block instead, like so:



expect { find('#recipients') }.to raise_error(Capybara::ElementNotFound)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 18:39









Alex D

23.6k360104




23.6k360104












  • If you want to assert if an element exist it's a better solution use a capybara matcher like has_css? with an rspec expectation i think rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/… relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
    – bott
    Dec 12 at 12:34










  • @bott That is a good point. I just copied the OP's code and fixed his bug, but didn't think about writing the assertion in a totally different way. You could post another answer if you like.
    – Alex D
    Dec 14 at 5:38


















  • If you want to assert if an element exist it's a better solution use a capybara matcher like has_css? with an rspec expectation i think rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/… relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
    – bott
    Dec 12 at 12:34










  • @bott That is a good point. I just copied the OP's code and fixed his bug, but didn't think about writing the assertion in a totally different way. You could post another answer if you like.
    – Alex D
    Dec 14 at 5:38
















If you want to assert if an element exist it's a better solution use a capybara matcher like has_css? with an rspec expectation i think rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/… relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
– bott
Dec 12 at 12:34




If you want to assert if an element exist it's a better solution use a capybara matcher like has_css? with an rspec expectation i think rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/… relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
– bott
Dec 12 at 12:34












@bott That is a good point. I just copied the OP's code and fixed his bug, but didn't think about writing the assertion in a totally different way. You could post another answer if you like.
– Alex D
Dec 14 at 5:38




@bott That is a good point. I just copied the OP's code and fixed his bug, but didn't think about writing the assertion in a totally different way. You could post another answer if you like.
– Alex D
Dec 14 at 5:38


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53399407%2fwhy-cant-i-catch-capybaraelementnotfound-in-the-to-raise-matcher%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

Costa Masnaga

Fotorealismo

Sidney Franklin