Skip to content
This repository
Browse code

add keyCode and which to keypress, keydown, and keyup

  • Loading branch information...
commit c6103e75c11f691731515f7c9c36c5b1a4a2c5cc 1 parent 4d954b7
authored March 07, 2012
129  spec/driver_spec.rb
@@ -1409,33 +1409,126 @@ def set_automatic_reload(value)
1409 1409
     end
1410 1410
   end
1411 1411
 
  1412
+  def key_app_body(event)
  1413
+    body = <<-HTML
  1414
+        <html>
  1415
+          <head><title>Form</title></head>
  1416
+          <body>
  1417
+            <div id="charcode_value"></div>
  1418
+            <div id="keycode_value"></div>
  1419
+            <div id="which_value"></div>
  1420
+            <input type="text" id="charcode" name="charcode" on#{event}="setcharcode" />
  1421
+            <script type="text/javascript">
  1422
+              var element = document.getElementById("charcode")
  1423
+              element.addEventListener("#{event}", setcharcode);
  1424
+              function setcharcode(event) {
  1425
+                var element = document.getElementById("charcode_value");
  1426
+                element.innerHTML = event.charCode;
  1427
+                element = document.getElementById("keycode_value");
  1428
+                element.innerHTML = event.keyCode;
  1429
+                element = document.getElementById("which_value");
  1430
+                element.innerHTML = event.which;
  1431
+              }
  1432
+            </script>
  1433
+          </body>
  1434
+        </html>
  1435
+    HTML
  1436
+    body
  1437
+  end
  1438
+
1412 1439
   context "keypress app" do
1413 1440
     before(:all) do
1414 1441
       @app = lambda do |env|
1415  
-        body = <<-HTML
1416  
-            <html>
1417  
-              <head><title>Form</title></head>
1418  
-              <body>
1419  
-                <div id="charcode_value"></div>
1420  
-                <input type="text" id="charcode" name="charcode" onkeypress="setcharcode" />
1421  
-                <script type="text/javascript">
1422  
-                  var element = document.getElementById("charcode")
1423  
-                  element.addEventListener("keypress", setcharcode);
1424  
-                  function setcharcode(event) {
1425  
-                    var element = document.getElementById("charcode_value");
1426  
-                    element.innerHTML = event.charCode;
1427  
-                  }
1428  
-                </script>
1429  
-              </body>
1430  
-            </html>
1431  
-        HTML
  1442
+        body = key_app_body("keypress")
1432 1443
         [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
1433 1444
       end
1434 1445
     end
1435 1446
 
1436 1447
     it "returns the charCode for the keypressed" do
1437 1448
       subject.find("//input")[0].set("a")
1438  
-      subject.find("//div")[0].text.should == "97"
  1449
+      subject.find("//div[@id='charcode_value']")[0].text.should == "97"
  1450
+    end
  1451
+
  1452
+    it "returns the keyCode for the keypressed" do
  1453
+      subject.find("//input")[0].set("a")
  1454
+      subject.find("//div[@id='keycode_value']")[0].text.should == "97"
  1455
+      subject.find("//input")[0].set("A")
  1456
+      subject.find("//div[@id='keycode_value']")[0].text.should == "65"
  1457
+      subject.find("//input")[0].set("\r")
  1458
+      subject.find("//div[@id='keycode_value']")[0].text.should == "13"
  1459
+    end
  1460
+
  1461
+    it "returns the which for the keypressed" do
  1462
+      subject.find("//input")[0].set("a")
  1463
+      subject.find("//div[@id='which_value']")[0].text.should == "97"
  1464
+      subject.find("//input")[0].set("A")
  1465
+      subject.find("//div[@id='which_value']")[0].text.should == "65"
  1466
+      subject.find("//input")[0].set("\r")
  1467
+      subject.find("//div[@id='which_value']")[0].text.should == "13"
  1468
+    end
  1469
+  end
  1470
+
  1471
+  context "keydown app" do
  1472
+    before(:all) do
  1473
+      @app = lambda do |env|
  1474
+        body = key_app_body("keydown")
  1475
+        [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
  1476
+      end
  1477
+    end
  1478
+
  1479
+    it "returns 0 charCode for the keyup" do
  1480
+      subject.find("//input")[0].set("a")
  1481
+      subject.find("//div[@id='charcode_value']")[0].text.should == "0"
  1482
+    end
  1483
+
  1484
+    it "returns the keyCode for the keyup" do
  1485
+      subject.find("//input")[0].set("a")
  1486
+      subject.find("//div[@id='keycode_value']")[0].text.should == "65"
  1487
+      subject.find("//input")[0].set("A")
  1488
+      subject.find("//div[@id='keycode_value']")[0].text.should == "65"
  1489
+      subject.find("//input")[0].set("\r")
  1490
+      subject.find("//div[@id='keycode_value']")[0].text.should == "13"
  1491
+    end
  1492
+
  1493
+    it "returns the which for the keyup" do
  1494
+      subject.find("//input")[0].set("a")
  1495
+      subject.find("//div[@id='which_value']")[0].text.should == "65"
  1496
+      subject.find("//input")[0].set("A")
  1497
+      subject.find("//div[@id='which_value']")[0].text.should == "65"
  1498
+      subject.find("//input")[0].set("\r")
  1499
+      subject.find("//div[@id='which_value']")[0].text.should == "13"
  1500
+    end
  1501
+  end
  1502
+
  1503
+  context "keyup app" do
  1504
+    before(:all) do
  1505
+      @app = lambda do |env|
  1506
+        body = key_app_body("keyup")
  1507
+        [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
  1508
+      end
  1509
+    end
  1510
+
  1511
+    it "returns 0 charCode for the keyup" do
  1512
+      subject.find("//input")[0].set("a")
  1513
+      subject.find("//div[@id='charcode_value']")[0].text.should == "0"
  1514
+    end
  1515
+
  1516
+    it "returns the keyCode for the keyup" do
  1517
+      subject.find("//input")[0].set("a")
  1518
+      subject.find("//div[@id='keycode_value']")[0].text.should == "65"
  1519
+      subject.find("//input")[0].set("A")
  1520
+      subject.find("//div[@id='keycode_value']")[0].text.should == "65"
  1521
+      subject.find("//input")[0].set("\r")
  1522
+      subject.find("//div[@id='keycode_value']")[0].text.should == "13"
  1523
+    end
  1524
+
  1525
+    it "returns the which for the keyup" do
  1526
+      subject.find("//input")[0].set("a")
  1527
+      subject.find("//div[@id='which_value']")[0].text.should == "65"
  1528
+      subject.find("//input")[0].set("A")
  1529
+      subject.find("//div[@id='which_value']")[0].text.should == "65"
  1530
+      subject.find("//input")[0].set("\r")
  1531
+      subject.find("//div[@id='which_value']")[0].text.should == "13"
1439 1532
     end
1440 1533
   end
1441 1534
 end
17  src/capybara.js
@@ -132,6 +132,16 @@ Capybara = {
132 132
     eventObject.metaKey = metaKey;
133 133
     eventObject.keyCode = keyCode;
134 134
     eventObject.charCode = charCode;
  135
+    eventObject.which = keyCode;
  136
+    this.nodes[index].dispatchEvent(eventObject);
  137
+  },
  138
+
  139
+  keyupdown: function(index, eventName, keyCode) {
  140
+    var eventObject = document.createEvent("HTMLEvents");
  141
+    eventObject.initEvent(eventName, true, true);
  142
+    eventObject.keyCode = keyCode;
  143
+    eventObject.which = keyCode;
  144
+    eventObject.charCode = 0;
135 145
     this.nodes[index].dispatchEvent(eventObject);
136 146
   },
137 147
 
@@ -173,9 +183,10 @@ Capybara = {
173 183
       node.value = "";
174 184
       for (strindex = 0; strindex < length; strindex++) {
175 185
         node.value += value[strindex];
176  
-        this.trigger(index, "keydown");
177  
-        this.keypress(index, false, false, false, false, 0, value.charCodeAt(strindex));
178  
-        this.trigger(index, "keyup");
  186
+        var keyCode = value[strindex].toUpperCase().charCodeAt(0);
  187
+        this.keyupdown(index, "keydown", keyCode);
  188
+        this.keypress(index, false, false, false, false, value.charCodeAt(strindex), value.charCodeAt(strindex));
  189
+        this.keyupdown(index, "keyup", keyCode);
179 190
       }
180 191
       this.trigger(index, "change");
181 192
       this.trigger(index, "blur");

0 notes on commit c6103e7

Please sign in to comment.
Something went wrong with that request. Please try again.