Skip to content

Commit

Permalink
Merge pull request #18 from DrMarcII/typing
Browse files Browse the repository at this point in the history
Fire keypress events when typing in HtmlPageLoader.
  • Loading branch information
DrMarcII committed Dec 16, 2014
2 parents 9853692 + 0bdbe53 commit e0fd686
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
49 changes: 31 additions & 18 deletions dart/lib/html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ abstract class HtmlPageLoaderElement implements PageLoaderElement {

@override
String toString() => '$runtimeType<${node.toString()}>';

void type(String keys) {
_fireKeyPressEvents(node, keys);
loader.sync();
}

// This doesn't work in Dartium due to:
// https://code.google.com/p/dart/issues/detail?id=13902
void _fireKeyPressEvents(Element element, String keys) {
for (int charCode in keys.codeUnits) {
element.dispatchEvent(
new KeyEvent('keypress', charCode: charCode).wrapped);
}
}
}

class _ElementPageLoaderElement extends HtmlPageLoaderElement {
Expand Down Expand Up @@ -232,32 +246,26 @@ class _ElementPageLoaderElement extends HtmlPageLoaderElement {

@override
void type(String keys) {
node.focus();
_fireKeyPressEvents(node, keys);
if (node is InputElement) {
_setInputValue(node, (node as InputElement).value + keys);
} else {
throw new PageLoaderException(
'HtmlPageLoader type method only supports InputElements');
var value = (node as InputElement).value + keys;
(node as InputElement).value = '';
node.dispatchEvent(new TextEvent('textInput', data: value));
}
node.blur();
loader.sync();
}

@override
void clear() {
if (node is InputElement) {
_setInputValue(node, '');
(node as InputElement).value = '';
node.dispatchEvent(new TextEvent('textInput', data: ''));
} else {
super.clear();
}
}

void _setInputValue(InputElement element, String value) {
node.focus();
bool attributeSet = node.attributes.containsKey('value');
node.setAttribute('value', value);
(node as InputElement).value = '';
node.dispatchEvent(new TextEvent('textInput', data: value));
node.blur();
loader.sync();
}
}

class _ShadowRootPageLoaderElement extends HtmlPageLoaderElement {
Expand All @@ -282,8 +290,6 @@ class _ShadowRootPageLoaderElement extends HtmlPageLoaderElement {
@override
PageLoaderAttributes get style => super.style;
@override
void type(String keys) => super.type(keys);
@override
PageLoaderAttributes get attributes => super.attributes;
@override
PageLoaderElement get shadowRoot => super.shadowRoot;
Expand Down Expand Up @@ -315,7 +321,14 @@ class _DocumentPageLoaderElement extends HtmlPageLoaderElement {
@override
PageLoaderAttributes get style => super.style;
@override
void type(String keys) => super.type(keys);
void type(String keys) {
// TODO(DrMarcII) consider whether this should be sent to
// document.activeElement to more closely match WebDriver behavior.
document.body.focus();
_fireKeyPressEvents(document.body, keys);
document.body.blur();
loader.sync();
}
}

class _ElementAttributes extends PageLoaderAttributes {
Expand Down
2 changes: 1 addition & 1 deletion dart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pageloader
version: 1.2.2+1
version: 1.2.3
author: Marc Fisher II
description: Supports the creation of page objects that can be shared between in-browser tests and WebDriver tests.
environment:
Expand Down
8 changes: 8 additions & 0 deletions dart/test/html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ void main() {
expect(page.text.attributes['value'], 'some text');
expect(handlerCalled, isTrue);
});

test('keypress events', () {
var data = 'my data';
var list = [];
html.document.body.onKeyPress.listen((evt) => list.add(evt.charCode));
plt.loader.globalContext.type(data);
expect(new String.fromCharCodes(list), equals(data));
});
});

plt.runTests();
Expand Down

0 comments on commit e0fd686

Please sign in to comment.