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

Trigger mousedown and mouseup events

This commit is contained in:
Matthew Mongeau 2012-01-27 16:46:14 -05:00
parent df39268395
commit 16c16372c8
2 changed files with 60 additions and 2 deletions

View file

@ -529,6 +529,44 @@ describe Capybara::Driver::Webkit do
end end
end end
context "dom events" do
before(:all) do
@app = lambda do |env|
body = <<-HTML
<html><body>
<a href='#' class='watch'>Link</a>
<ul id="events"></ul>
<script type="text/javascript">
var events = document.getElementById("events");
var recordEvent = function (event) {
var element = document.createElement("li");
element.innerHTML = event.type;
events.appendChild(element);
};
var elements = document.getElementsByClassName("watch");
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
element.addEventListener("mousedown", recordEvent);
element.addEventListener("mouseup", recordEvent);
element.addEventListener("click", recordEvent);
}
</script>
</body></html>
HTML
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
end
it "triggers mouse events" do
subject.find("//a").first.click
subject.find("//li").map(&:text).should == %w(mousedown mouseup click)
end
end
context "form events app" do context "form events app" do
before(:all) do before(:all) do
@app = lambda do |env| @app = lambda do |env|
@ -564,6 +602,8 @@ describe Capybara::Driver::Webkit do
element.addEventListener("keyup", recordEvent); element.addEventListener("keyup", recordEvent);
element.addEventListener("change", recordEvent); element.addEventListener("change", recordEvent);
element.addEventListener("blur", recordEvent); element.addEventListener("blur", recordEvent);
element.addEventListener("mousedown", recordEvent);
element.addEventListener("mouseup", recordEvent);
element.addEventListener("click", recordEvent); element.addEventListener("click", recordEvent);
} }
</script> </script>
@ -597,12 +637,12 @@ describe Capybara::Driver::Webkit do
it "triggers radio input events" do it "triggers radio input events" do
subject.find("//input[@type='radio']").first.set(true) subject.find("//input[@type='radio']").first.set(true)
subject.find("//li").map(&:text).should == %w(click change) subject.find("//li").map(&:text).should == %w(mousedown mouseup click change)
end end
it "triggers checkbox events" do it "triggers checkbox events" do
subject.find("//input[@type='checkbox']").first.set(true) subject.find("//input[@type='checkbox']").first.set(true)
subject.find("//li").map(&:text).should == %w(click change) subject.find("//li").map(&:text).should == %w(mousedown mouseup click change)
end end
end end

View file

@ -96,7 +96,21 @@ Capybara = {
return this.nodes[index].submit(); return this.nodes[index].submit();
}, },
mousedown: function(index) {
var mousedownEvent = document.createEvent('MouseEvents');
mousedownEvent.initMouseEvent('mousedown', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
this.nodes[index].dispatchEvent(mousedownEvent);
},
mouseup: function(index) {
var mouseupEvent = document.createEvent('MouseEvents');
mouseupEvent.initMouseEvent('mouseup', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
this.nodes[index].dispatchEvent(mouseupEvent);
},
click: function (index) { click: function (index) {
this.mousedown(index);
this.mouseup(index);
var clickEvent = document.createEvent('MouseEvents'); var clickEvent = document.createEvent('MouseEvents');
clickEvent.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); clickEvent.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
this.nodes[index].dispatchEvent(clickEvent); this.nodes[index].dispatchEvent(clickEvent);
@ -168,11 +182,15 @@ Capybara = {
} else if (type === "checkbox" || type === "radio") { } else if (type === "checkbox" || type === "radio") {
node.checked = (value === "true"); node.checked = (value === "true");
this.trigger(index, "mousedown");
this.trigger(index, "mouseup");
this.trigger(index, "click"); this.trigger(index, "click");
this.trigger(index, "change"); this.trigger(index, "change");
} else if (type === "file") { } else if (type === "file") {
this.lastAttachedFile = value; this.lastAttachedFile = value;
this.trigger(index, "mousedown");
this.trigger(index, "mouseup");
this.trigger(index, "click"); this.trigger(index, "click");
} else { } else {