Skip to content

Commit

Permalink
add keyCode and which to keypress, keydown, and keyup
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Ryan committed Mar 8, 2012
1 parent 4d954b7 commit c6103e7
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 21 deletions.
129 changes: 111 additions & 18 deletions spec/driver_spec.rb
Expand Up @@ -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 changes: 14 additions & 3 deletions src/capybara.js
Expand Up @@ -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);
},

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit c6103e7

Please sign in to comment.