Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add keyCode and which to keypress, keydown, and keyup

  • Loading branch information...
commit c6103e75c11f691731515f7c9c36c5b1a4a2c5cc 1 parent 4d954b7
William Pleasant-Ryan willryan authored
Showing with 125 additions and 21 deletions.
  1. +111 −18 spec/driver_spec.rb
  2. +14 −3 src/capybara.js
129 spec/driver_spec.rb
View
@@ -1409,33 +1409,126 @@ def set_automatic_reload(value)
end
end
+ def key_app_body(event)
+ body = <<-HTML
+ <html>
+ <head><title>Form</title></head>
+ <body>
+ <div id="charcode_value"></div>
+ <div id="keycode_value"></div>
+ <div id="which_value"></div>
+ <input type="text" id="charcode" name="charcode" on#{event}="setcharcode" />
+ <script type="text/javascript">
+ var element = document.getElementById("charcode")
+ element.addEventListener("#{event}", setcharcode);
+ function setcharcode(event) {
+ var element = document.getElementById("charcode_value");
+ element.innerHTML = event.charCode;
+ element = document.getElementById("keycode_value");
+ element.innerHTML = event.keyCode;
+ element = document.getElementById("which_value");
+ element.innerHTML = event.which;
+ }
+ </script>
+ </body>
+ </html>
+ HTML
+ body
+ end
+
context "keypress app" do
before(:all) do
@app = lambda do |env|
- body = <<-HTML
- <html>
- <head><title>Form</title></head>
- <body>
- <div id="charcode_value"></div>
- <input type="text" id="charcode" name="charcode" onkeypress="setcharcode" />
- <script type="text/javascript">
- var element = document.getElementById("charcode")
- element.addEventListener("keypress", setcharcode);
- function setcharcode(event) {
- var element = document.getElementById("charcode_value");
- element.innerHTML = event.charCode;
- }
- </script>
- </body>
- </html>
- HTML
+ body = key_app_body("keypress")
[200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
end
end
it "returns the charCode for the keypressed" do
subject.find("//input")[0].set("a")
- subject.find("//div")[0].text.should == "97"
+ subject.find("//div[@id='charcode_value']")[0].text.should == "97"
+ end
+
+ it "returns the keyCode for the keypressed" do
+ subject.find("//input")[0].set("a")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "97"
+ subject.find("//input")[0].set("A")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("\r")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "13"
+ end
+
+ it "returns the which for the keypressed" do
+ subject.find("//input")[0].set("a")
+ subject.find("//div[@id='which_value']")[0].text.should == "97"
+ subject.find("//input")[0].set("A")
+ subject.find("//div[@id='which_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("\r")
+ subject.find("//div[@id='which_value']")[0].text.should == "13"
+ end
+ end
+
+ context "keydown app" do
+ before(:all) do
+ @app = lambda do |env|
+ body = key_app_body("keydown")
+ [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
+ end
+ end
+
+ it "returns 0 charCode for the keyup" do
+ subject.find("//input")[0].set("a")
+ subject.find("//div[@id='charcode_value']")[0].text.should == "0"
+ end
+
+ it "returns the keyCode for the keyup" do
+ subject.find("//input")[0].set("a")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("A")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("\r")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "13"
+ end
+
+ it "returns the which for the keyup" do
+ subject.find("//input")[0].set("a")
+ subject.find("//div[@id='which_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("A")
+ subject.find("//div[@id='which_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("\r")
+ subject.find("//div[@id='which_value']")[0].text.should == "13"
+ end
+ end
+
+ context "keyup app" do
+ before(:all) do
+ @app = lambda do |env|
+ body = key_app_body("keyup")
+ [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
+ end
+ end
+
+ it "returns 0 charCode for the keyup" do
+ subject.find("//input")[0].set("a")
+ subject.find("//div[@id='charcode_value']")[0].text.should == "0"
+ end
+
+ it "returns the keyCode for the keyup" do
+ subject.find("//input")[0].set("a")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("A")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("\r")
+ subject.find("//div[@id='keycode_value']")[0].text.should == "13"
+ end
+
+ it "returns the which for the keyup" do
+ subject.find("//input")[0].set("a")
+ subject.find("//div[@id='which_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("A")
+ subject.find("//div[@id='which_value']")[0].text.should == "65"
+ subject.find("//input")[0].set("\r")
+ subject.find("//div[@id='which_value']")[0].text.should == "13"
end
end
end
17 src/capybara.js
View
@@ -132,6 +132,16 @@ Capybara = {
eventObject.metaKey = metaKey;
eventObject.keyCode = keyCode;
eventObject.charCode = charCode;
+ eventObject.which = keyCode;
+ this.nodes[index].dispatchEvent(eventObject);
+ },
+
+ keyupdown: function(index, eventName, keyCode) {
+ var eventObject = document.createEvent("HTMLEvents");
+ eventObject.initEvent(eventName, true, true);
+ eventObject.keyCode = keyCode;
+ eventObject.which = keyCode;
+ eventObject.charCode = 0;
this.nodes[index].dispatchEvent(eventObject);
},
@@ -173,9 +183,10 @@ Capybara = {
node.value = "";
for (strindex = 0; strindex < length; strindex++) {
node.value += value[strindex];
- this.trigger(index, "keydown");
- this.keypress(index, false, false, false, false, 0, value.charCodeAt(strindex));
- this.trigger(index, "keyup");
+ var keyCode = value[strindex].toUpperCase().charCodeAt(0);
+ this.keyupdown(index, "keydown", keyCode);
+ this.keypress(index, false, false, false, false, value.charCodeAt(strindex), value.charCodeAt(strindex));
+ this.keyupdown(index, "keyup", keyCode);
}
this.trigger(index, "change");
this.trigger(index, "blur");
Please sign in to comment.
Something went wrong with that request. Please try again.