From df392683957e32f5d9257e2c4a5312a06830dd36 Mon Sep 17 00:00:00 2001 From: Marc Schwieterman Date: Sun, 15 Jan 2012 18:49:44 -0500 Subject: [PATCH 1/3] add imagemagick dependency to contributing guide Several tests in driver_rendering_spec.rb require the identify command that is part of Imagemagick. Without it they fail with output similar to the following. MiniMagick::Error: Command ("identify -ping /var/folders/y3/vfvjgwm91f1fmt2syh21czx40000gn/T/mini_magick20120115-49056-pwh5nl-0.png") failed: {:output=>"sh: identify: command not found\n", :status_code=>127} --- CONTRIBUTING.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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 From 16c16372c86896b060cfa5c1741d95b0653cc3a4 Mon Sep 17 00:00:00 2001 From: Matthew Mongeau Date: Fri, 27 Jan 2012 16:46:14 -0500 Subject: [PATCH 2/3] Trigger mousedown and mouseup events --- spec/driver_spec.rb | 44 ++++++++++++++++++++++++++++++++++++++++++-- src/capybara.js | 18 ++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 467a803..74bfaf1 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 click change) 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 click change) end end diff --git a/src/capybara.js b/src/capybara.js index 8bf028b..f665351 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); @@ -168,11 +182,15 @@ Capybara = { } else if (type === "checkbox" || type === "radio") { node.checked = (value === "true"); + this.trigger(index, "mousedown"); + this.trigger(index, "mouseup"); this.trigger(index, "click"); this.trigger(index, "change"); } else if (type === "file") { this.lastAttachedFile = value; + this.trigger(index, "mousedown"); + this.trigger(index, "mouseup"); this.trigger(index, "click"); } else { From 51c4dfe1414a9b1620dfb61db456646f51a782dd Mon Sep 17 00:00:00 2001 From: Matthew Mongeau Date: Fri, 27 Jan 2012 18:03:38 -0500 Subject: [PATCH 3/3] Simulate browsers events more closely --- spec/driver_spec.rb | 4 ++-- src/capybara.js | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 74bfaf1..6ce04fd 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -637,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(mousedown mouseup 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(mousedown mouseup 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 f665351..290383d 100644 --- a/src/capybara.js +++ b/src/capybara.js @@ -181,17 +181,13 @@ Capybara = { this.trigger(index, "blur"); } else if (type === "checkbox" || type === "radio") { - node.checked = (value === "true"); - this.trigger(index, "mousedown"); - this.trigger(index, "mouseup"); - 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, "mousedown"); - this.trigger(index, "mouseup"); - this.trigger(index, "click"); + this.click(index) } else { node.value = value;