diff --git a/lib/capybara/driver/webkit/node.rb b/lib/capybara/driver/webkit/node.rb index 5cafec7..c923328 100644 --- a/lib/capybara/driver/webkit/node.rb +++ b/lib/capybara/driver/webkit/node.rb @@ -5,7 +5,12 @@ class Capybara::Driver::Webkit end def [](name) - invoke "attribute", name + value = invoke("attribute", name) + if name == 'checked' + value == 'true' + else + value + end end def value diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index c930253..38295d2 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -148,6 +148,8 @@ describe Capybara::Driver::Webkit do
+ + +
- - HTML [200, @@ -204,11 +192,6 @@ describe Capybara::Driver::Webkit do input.value.should == "newvalue" end - it "triggers input events" do - subject.find("//textarea").first.set("newvalue") - subject.find("//li").map(&:text).should == %w(focus keydown keyup change blur) - end - it "sets a select's value" do select = subject.find("//select").first select.set("Monkey") @@ -255,6 +238,103 @@ describe Capybara::Driver::Webkit do toppings_select.value.should include("Apple", "Banana", "Cherry") end + + let(:checked_box) { subject.find("//input[@name='checkedbox']").first } + let(:unchecked_box) { subject.find("//input[@name='uncheckedbox']").first } + + it "knows a checked box is checked" do + checked_box['checked'].should be_true + end + + it "knows an unchecked box is unchecked" do + unchecked_box['checked'].should_not be_true + end + + it "checks an unchecked box" do + unchecked_box.set(true) + unchecked_box['checked'].should be_true + end + + it "unchecks a checked box" do + checked_box.set(false) + checked_box['checked'].should_not be_true + end + + it "leaves a checked box checked" do + checked_box.set(true) + checked_box['checked'].should be_true + end + + it "leaves an unchecked box unchecked" do + unchecked_box.set(false) + unchecked_box['checked'].should_not be_true + end + end + + context "form events app" do + before(:all) do + @app = lambda do |env| + body = <<-HTML + +
+ + + + + +
+ + + + HTML + [200, + { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, + [body]] + end + end + + it "triggers text input events" do + subject.find("//input[@type='text']").first.set("newvalue") + subject.find("//li").map(&:text).should == %w(focus keydown keyup change blur) + end + + it "triggers textarea input events" do + subject.find("//textarea").first.set("newvalue") + subject.find("//li").map(&:text).should == %w(focus keydown keyup change blur) + end + + it "triggers password input events" do + subject.find("//input[@type='password']").first.set("newvalue") + subject.find("//li").map(&:text).should == %w(focus keydown keyup change blur) + end + + it "triggers radio input events" do + subject.find("//input[@type='radio']").first.set(true) + subject.find("//li").map(&:text).should == %w(click) + end + + it "triggers checkbox events" do + subject.find("//input[@type='checkbox']").first.set(true) + subject.find("//li").map(&:text).should == %w(click) + end end context "mouse app" do diff --git a/src/capybara.js b/src/capybara.js index f2b1559..9d1c7b9 100644 --- a/src/capybara.js +++ b/src/capybara.js @@ -31,7 +31,11 @@ Capybara = { }, attribute: function (index, name) { - return this.nodes[index].getAttribute(name); + if (name == "checked") { + return this.nodes[index].checked; + } else { + return this.nodes[index].getAttribute(name); + } }, tagName: function(index) { @@ -65,12 +69,21 @@ Capybara = { }, set: function(index, value) { - this.trigger(index, "focus"); - this.nodes[index].value = value; - this.trigger(index, "keydown"); - this.trigger(index, "keyup"); - this.trigger(index, "change"); - this.trigger(index, "blur"); + var node = this.nodes[index]; + var type = (node.type || node.tagName).toLowerCase(); + if (type == "text" || type == "textarea" || type == "password") { + this.trigger(index, "focus"); + node.value = value; + this.trigger(index, "keydown"); + this.trigger(index, "keyup"); + this.trigger(index, "change"); + this.trigger(index, "blur"); + } else if(type == "checkbox" || type == "radio") { + node.checked = (value == "true"); + this.trigger(index, "click"); + } else { + node.value = value; + } }, selectOption: function(index) {