Set text fields using native keypress events
This commit is contained in:
parent
be308beec0
commit
a5c51b23bd
|
@ -1083,8 +1083,8 @@ describe Capybara::Webkit::Driver do
|
|||
|
||||
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 |
|
||||
|
@ -1812,7 +1812,7 @@ describe Capybara::Webkit::Driver do
|
|||
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"
|
||||
|
@ -1821,7 +1821,7 @@ describe Capybara::Webkit::Driver do
|
|||
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"
|
||||
|
@ -1830,7 +1830,7 @@ describe Capybara::Webkit::Driver do
|
|||
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"
|
||||
|
|
|
@ -18,7 +18,7 @@ describe Capybara::Webkit, 'compatibility with selenium' do
|
|||
};
|
||||
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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 *);
|
||||
|
@ -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 &);
|
||||
};
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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")) {
|
||||
|
|
Loading…
Reference in New Issue