Permalink
Browse files

Raise error when an invisible element receives #click so it resembles…

… a browser more closely.
  • Loading branch information...
1 parent 59ba861 commit 04a1baaf2ac3e95b368508f74d00ee645d67cc63 @marcofognog marcofognog committed with jferris Jan 18, 2012
Showing with 17 additions and 2 deletions.
  1. +9 −1 lib/capybara/driver/webkit/node.rb
  2. +8 −1 spec/driver_spec.rb
@@ -1,5 +1,9 @@
class Capybara::Driver::Webkit
class Node < Capybara::Driver::Node
+
+ class ElementNotDisplayedError < StandardError
+ end
+
NBSP = "\xC2\xA0"
NBSP.force_encoding("UTF-8") if NBSP.respond_to?(:force_encoding)
@@ -46,7 +50,11 @@ def unselect_option
end
def click
- invoke "click"
+ if visible?
+ invoke "click"
+ else
+ raise ElementNotDisplayedError, "This element is not visible so it may not be interacted with"
+ end
end
def drag_to(element)
View
@@ -639,6 +639,7 @@
});
</script>
<a href="/next">Next</a>
+ <a href="/next" id="hidden" style="display:none;">Not displayed</a>
</body></html>
HTML
[200,
@@ -647,7 +648,7 @@
end
end
- it "clicks an element" do
+ it "clicks a visible element" do
subject.find("//a").first.click
subject.current_url =~ %r{/next$}
end
@@ -657,6 +658,12 @@
subject.find("//*[@class='triggered']").should_not be_empty
end
+ it "raises error when it tries to click an invisible element" do
+ expect {
+ subject.find("//*[@id='hidden']").first.click
+ }.to raise_error(Capybara::Driver::Webkit::Node::ElementNotDisplayedError)
+ end
+
it "fires a non-mouse event" do
subject.find("//*[@id='change']").first.trigger("change")
subject.find("//*[@class='triggered']").should_not be_empty

0 comments on commit 04a1baa

Please sign in to comment.