puppeteer/third_party/phantomjs/test/module/webpage/keypress-event.js

68 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-05-11 07:06:41 +00:00
test(function () {
var webpage = require('webpage');
var page = webpage.create();
page.evaluate(function() {
window.addEventListener('keypress', function(event) {
window.loggedEvent = window.loggedEvent || [];
window.loggedEvent.push(event.which);
2017-05-11 07:06:41 +00:00
}, false);
});
page.sendEvent('keypress', page.event.key.C);
var loggedEvent = page.evaluate(function() {
return window.loggedEvent;
});
assert_equals(loggedEvent.length, 1);
assert_equals(loggedEvent[0], page.event.key.C);
2017-05-11 07:06:41 +00:00
// Send keypress events to an input element and observe the effect.
page.content = '<input type="text">';
page.evaluate(function() {
document.querySelector('input').focus();
});
function getText() {
return page.evaluate(function() {
return document.querySelector('input').value;
});
}
page.sendEvent('keypress', page.event.key.A);
assert_equals(getText(), 'A');
page.sendEvent('keypress', page.event.key.B);
assert_equals(getText(), 'AB');
page.sendEvent('keypress', page.event.key.Backspace);
assert_equals(getText(), 'A');
page.sendEvent('keypress', page.event.key.Backspace);
assert_equals(getText(), '');
page.sendEvent('keypress', 'XYZ');
assert_equals(getText(), 'XYZ');
// Special character: A with umlaut
page.sendEvent('keypress', 'ä');
assert_equals(getText(), 'XYZä');
// 0x02000000 is the Shift modifier.
page.sendEvent('keypress', page.event.key.Home, null, null, 0x02000000);
page.sendEvent('keypress', page.event.key.Delete);
assert_equals(getText(), '');
// Joel: This works, but it causes you to lose your clipboard when running the tests.
2017-05-11 07:06:41 +00:00
// Cut and Paste
// 0x04000000 is the Control modifier.
// page.sendEvent('keypress', 'ABCD');
// assert_equals(getText(), 'ABCD');
// page.sendEvent('keypress', page.event.key.Home, null, null, 0x02000000);
// page.sendEvent('keypress', page.event.key.Cut);
// assert_equals(getText(), '');
// page.sendEvent('keypress', page.event.key.Paste);
// assert_equals(getText(), 'ABCD');
2017-05-11 07:06:41 +00:00
}, "key press events");