1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00

Allow interaction with invisible elements

This commit is contained in:
Joe Ferris 2012-03-09 09:58:17 -05:00
parent 4d954b73f8
commit 02f2a8ab73
2 changed files with 1 additions and 50 deletions

View file

@ -1,9 +1,5 @@
class Capybara::Driver::Webkit class Capybara::Driver::Webkit
class Node < Capybara::Driver::Node class Node < Capybara::Driver::Node
class ElementNotDisplayedError < StandardError
end
NBSP = "\xC2\xA0" NBSP = "\xC2\xA0"
NBSP.force_encoding("UTF-8") if NBSP.respond_to?(:force_encoding) NBSP.force_encoding("UTF-8") if NBSP.respond_to?(:force_encoding)
@ -33,7 +29,6 @@ class Capybara::Driver::Webkit
end end
def select_option def select_option
check_visibility(self)
invoke "selectOption" invoke "selectOption"
end end
@ -47,13 +42,10 @@ class Capybara::Driver::Webkit
end end
def click def click
check_visibility(self)
invoke "click" invoke "click"
end end
def drag_to(element) def drag_to(element)
check_visibility(self)
check_visibility(element)
invoke 'dragTo', element.native invoke 'dragTo', element.native
end end
@ -122,9 +114,5 @@ class Capybara::Driver::Webkit
def multiple_select? def multiple_select?
self.tag_name == "select" && self["multiple"] == "multiple" self.tag_name == "select" && self["multiple"] == "multiple"
end end
def check_visibility(element)
raise(ElementNotDisplayedError, "This element is not visible so it may not be interacted with") unless element.visible?
end
end end
end end

View file

@ -713,13 +713,10 @@ describe Capybara::Driver::Webkit do
<div id="change">Change me</div> <div id="change">Change me</div>
<div id="mouseup">Push me</div> <div id="mouseup">Push me</div>
<div id="mousedown">Release me</div> <div id="mousedown">Release me</div>
<div id="invisible-mouseup" style="display:none;">You can't push me</div>
<div id="invisible-mousedown" style="display:none;">You can't release me</div>
<form action="/" method="GET"> <form action="/" method="GET">
<select id="change_select" name="change_select"> <select id="change_select" name="change_select">
<option value="1" id="option-1" selected="selected">one</option> <option value="1" id="option-1" selected="selected">one</option>
<option value="2" id="option-2">two</option> <option value="2" id="option-2">two</option>
<option value="2" id="invisible-option" style="display:none;">three</option>
</select> </select>
</form> </form>
<script type="text/javascript"> <script type="text/javascript">
@ -741,7 +738,6 @@ describe Capybara::Driver::Webkit do
}); });
</script> </script>
<a href="/next">Next</a> <a href="/next">Next</a>
<a href="/next" id="hidden" style="display:none;">Not displayed</a>
</body></html> </body></html>
HTML HTML
[200, [200,
@ -750,7 +746,7 @@ describe Capybara::Driver::Webkit do
end end
end end
it "clicks a visible element" do it "clicks an element" do
subject.find("//a").first.click subject.find("//a").first.click
subject.current_url =~ %r{/next$} subject.current_url =~ %r{/next$}
end end
@ -782,39 +778,6 @@ describe Capybara::Driver::Webkit do
subject.find("//*[@class='triggered']").size.should == 1 subject.find("//*[@class='triggered']").size.should == 1
end end
context "raises error when" do
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 "tries to drag an invisible element to a visible one" do
draggable = subject.find("//*[@id='invisible-mousedown']").first
container = subject.find("//*[@id='mouseup']").first
expect {
draggable.drag_to(container)
}.to raise_error(Capybara::Driver::Webkit::Node::ElementNotDisplayedError)
end
it "tries to drag a visible element to an invisible one" do
draggable = subject.find("//*[@id='mousedown']").first
container = subject.find("//*[@id='invisible-mouseup']").first
expect {
draggable.drag_to(container)
}.to raise_error(Capybara::Driver::Webkit::Node::ElementNotDisplayedError)
end
it "tries to select an invisible option" do
option = subject.find("//option[@id='invisible-option']").first
expect {
option.select_option
}.to raise_error(Capybara::Driver::Webkit::Node::ElementNotDisplayedError)
end
end
end end
context "nesting app" do context "nesting app" do