Skip to content

Commit

Permalink
Set text fields using native keypress events
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Mar 28, 2013
1 parent be308be commit a5c51b2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 79 deletions.
10 changes: 5 additions & 5 deletions spec/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1083,8 +1083,8 @@ def visit(url, driver=driver)

let(:keyevents) do
(%w{focus} +
newtext.length.times.collect { %w{keydown keypress keyup input} } +
%w{change}).flatten
newtext.length.times.collect { %w{keydown keypress input keyup} }
).flatten
end

%w(email number password search tel text url).each do | field_type |
Expand Down Expand Up @@ -1812,7 +1812,7 @@ def which_for(character)
it "returns a 0 charCode for the event" do
charCode_for("a").should == "0"
charCode_for("A").should == "0"
charCode_for("\r").should == "0"
charCode_for("\b").should == "0"
charCode_for(",").should == "0"
charCode_for("<").should == "0"
charCode_for("0").should == "0"
Expand All @@ -1821,7 +1821,7 @@ def which_for(character)
it "returns the keyCode for the event" do
keyCode_for("a").should == "65"
keyCode_for("A").should == "65"
keyCode_for("\r").should == "13"
keyCode_for("\b").should == "8"
keyCode_for(",").should == "188"
keyCode_for("<").should == "188"
keyCode_for("0").should == "48"
Expand All @@ -1830,7 +1830,7 @@ def which_for(character)
it "returns the which for the event" do
which_for("a").should == "65"
which_for("A").should == "65"
which_for("\r").should == "13"
which_for("\b").should == "8"
which_for(",").should == "188"
which_for("<").should == "188"
which_for("0").should == "48"
Expand Down
2 changes: 1 addition & 1 deletion spec/selenium_compatibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
};
var elements = document.getElementsByTagName("input");
var events = ["mousedown", "mouseup", "click", "keyup", "keydown",
"keypress", "focus", "blur"];
"keypress", "focus", "blur", "input", "change"];
for (var i = 0; i < elements.length; i++) {
for (var j = 0; j < events.length; j++) {
elements[i].addEventListener(events[j], recordEvent);
Expand Down
31 changes: 31 additions & 0 deletions src/JavascriptInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,34 @@ void JavascriptInvocation::hover(int absoluteX, int absoluteY) {
void JavascriptInvocation::hover(const QPoint &mousePos) {
mouseEvent(QEvent::MouseMove, mousePos, Qt::NoButton);
}

int JavascriptInvocation::keyCodeFor(const QChar &key) {
switch(key.unicode()) {
case 0x18:
return Qt::Key_Cancel;
case 0x08:
return Qt::Key_Backspace;
case 0x09:
return Qt::Key_Tab;
case 0x0A:
return Qt::Key_Return;
case 0x1B:
return Qt::Key_Escape;
case 0x7F:
return Qt::Key_Delete;
default:
int keyCode = key.toUpper().toLatin1();
if (keyCode >= Qt::Key_Space || keyCode <= Qt::Key_AsciiTilde)
return keyCode;
else
return Qt::Key_unknown;
}
}

void JavascriptInvocation::keypress(QChar key) {
int keyCode = keyCodeFor(key);
QKeyEvent event(QKeyEvent::KeyPress, keyCode, Qt::NoModifier, key);
QApplication::sendEvent(m_page, &event);
event = QKeyEvent(QKeyEvent::KeyRelease, keyCode, Qt::NoModifier, key);
QApplication::sendEvent(m_page, &event);
}
2 changes: 2 additions & 0 deletions src/JavascriptInvocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class JavascriptInvocation : public QObject {
Q_INVOKABLE bool clickTest(QWebElement element, int absoluteX, int absoluteY);
Q_INVOKABLE QVariantMap clickPosition(QWebElement element, int left, int top, int width, int height);
Q_INVOKABLE void hover(int absoluteX, int absoluteY);
Q_INVOKABLE void keypress(QChar);
QVariant getError();
void setError(QVariant error);
InvocationResult invoke(QWebFrame *);
Expand All @@ -34,5 +35,6 @@ class JavascriptInvocation : public QObject {
QVariant m_error;
void mouseEvent(QEvent::Type type, const QPoint & position, Qt::MouseButton button);
void hover(const QPoint &);
int keyCodeFor(const QChar &);
};

74 changes: 1 addition & 73 deletions src/capybara.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,29 +196,6 @@ Capybara = {
this.nodes[index].dispatchEvent(eventObject);
},

keypress: function(index, altKey, ctrlKey, shiftKey, metaKey, keyCode, charCode) {
var eventObject = document.createEvent("Events");
eventObject.initEvent('keypress', true, true);
eventObject.window = window;
eventObject.altKey = altKey;
eventObject.ctrlKey = ctrlKey;
eventObject.shiftKey = shiftKey;
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);
},

visible: function (index) {
var element = this.nodes[index];
while (element) {
Expand Down Expand Up @@ -248,49 +225,6 @@ Capybara = {
return true;
},

characterToKeyCode: function(character) {
var code = character.toUpperCase().charCodeAt(0);
var specialKeys = {
96: 192, //`
45: 189, //-
61: 187, //=
91: 219, //[
93: 221, //]
92: 220, //\
59: 186, //;
39: 222, //'
44: 188, //,
46: 190, //.
47: 191, ///
127: 46, //delete
126: 192, //~
33: 49, //!
64: 50, //@
35: 51, //#
36: 52, //$
37: 53, //%
94: 54, //^
38: 55, //&
42: 56, //*
40: 57, //(
41: 48, //)
95: 189, //_
43: 187, //+
123: 219, //{
125: 221, //}
124: 220, //|
58: 186, //:
34: 222, //"
60: 188, //<
62: 190, //>
63: 191 //?
};
if (specialKeys[code]) {
code = specialKeys[code];
}
return code;
},

set: function (index, value) {
var length, maxLength, node, strindex, textTypes, type;

Expand All @@ -310,14 +244,8 @@ Capybara = {

node.value = "";
for (strindex = 0; strindex < length; strindex++) {
node.value += value[strindex];
var keyCode = this.characterToKeyCode(value[strindex]);
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, "input");
CapybaraInvocation.keypress(value[strindex]);
}
this.trigger(index, "change");

} else if (type === "checkbox" || type === "radio") {
if (node.checked != (value === "true")) {
Expand Down

0 comments on commit a5c51b2

Please sign in to comment.