diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 1aeaf5c..da49fa1 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1,5 +1,14 @@
We love pull requests. Here's a quick guide:
+Dependencies
+
+Some of the tests depend on the `identify` command that comes with Imagemagick.
+Imagemagick can be installed via [homebrew](http://mxcl.github.com/homebrew/).
+
+ brew install imagemagick
+
+Contributing
+
1. Fork the repo.
2. Run the tests. We only take pull requests with passing tests, and it's great
diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb
index 100f697..69b8d98 100644
--- a/spec/driver_spec.rb
+++ b/spec/driver_spec.rb
@@ -529,6 +529,44 @@ describe Capybara::Driver::Webkit do
end
end
+ context "dom events" do
+ before(:all) do
+ @app = lambda do |env|
+ body = <<-HTML
+
+
+ Link
+
+
+
+ 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
before(:all) do
@app = lambda do |env|
@@ -564,6 +602,8 @@ describe Capybara::Driver::Webkit do
element.addEventListener("keyup", recordEvent);
element.addEventListener("change", recordEvent);
element.addEventListener("blur", recordEvent);
+ element.addEventListener("mousedown", recordEvent);
+ element.addEventListener("mouseup", recordEvent);
element.addEventListener("click", recordEvent);
}
@@ -597,12 +637,12 @@ describe Capybara::Driver::Webkit do
it "triggers radio input events" do
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 change click)
end
it "triggers checkbox events" do
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 change click)
end
end
diff --git a/src/capybara.js b/src/capybara.js
index 8bf028b..290383d 100644
--- a/src/capybara.js
+++ b/src/capybara.js
@@ -96,7 +96,21 @@ Capybara = {
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) {
+ this.mousedown(index);
+ this.mouseup(index);
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
this.nodes[index].dispatchEvent(clickEvent);
@@ -167,13 +181,13 @@ Capybara = {
this.trigger(index, "blur");
} else if (type === "checkbox" || type === "radio") {
- node.checked = (value === "true");
- this.trigger(index, "click");
- this.trigger(index, "change");
+ if (node.checked != (value === "true")) {
+ this.click(index)
+ }
} else if (type === "file") {
this.lastAttachedFile = value;
- this.trigger(index, "click");
+ this.click(index)
} else {
node.value = value;