From ebb30b4eee53d896219d604b6e3d6db183b14aac Mon Sep 17 00:00:00 2001 From: Marc Fisher Date: Mon, 15 Dec 2014 15:50:50 -0800 Subject: [PATCH 1/3] Fire keypress events when typing in HtmlPageLoader. Note: this only works correctly when compiled to JS. --- dart/lib/html.dart | 44 ++++++++++++++++++++++++---------------- dart/pubspec.yaml | 2 +- dart/test/html_test.dart | 8 ++++++++ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/dart/lib/html.dart b/dart/lib/html.dart index 131f5045..3e09c346 100644 --- a/dart/lib/html.dart +++ b/dart/lib/html.dart @@ -187,6 +187,17 @@ abstract class HtmlPageLoaderElement implements PageLoaderElement { @override String toString() => '$runtimeType<${node.toString()}>'; + + void type(String keys) { + _fireKeyPressEvents(node, keys); + loader.sync(); + } + + void _fireKeyPressEvents(Element element, String keys) { + for (int charCode in keys.codeUnits) { + element.dispatchEvent(new KeyEvent('keypress', charCode: charCode).wrapped); + } + } } class _ElementPageLoaderElement extends HtmlPageLoaderElement { @@ -233,31 +244,28 @@ class _ElementPageLoaderElement extends HtmlPageLoaderElement { @override void type(String keys) { if (node is InputElement) { - _setInputValue(node, (node as InputElement).value + keys); + node.focus(); + _fireKeyPressEvents(node, keys); + var value = (node as InputElement).value + keys; + (node as InputElement).value = ''; + node.dispatchEvent(new TextEvent('textInput', data: value)); + node.blur(); } else { - throw new PageLoaderException( - 'HtmlPageLoader type method only supports InputElements'); + document.body.focus(); + _fireKeyPressEvents(node, keys); } + 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 { @@ -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; @@ -315,7 +321,11 @@ class _DocumentPageLoaderElement extends HtmlPageLoaderElement { @override PageLoaderAttributes get style => super.style; @override - void type(String keys) => super.type(keys); + void type(String keys) { + document.body.focus(); + _fireKeyPressEvents(document.body, keys); + loader.sync(); + } } class _ElementAttributes extends PageLoaderAttributes { diff --git a/dart/pubspec.yaml b/dart/pubspec.yaml index efe67f9f..7be99836 100644 --- a/dart/pubspec.yaml +++ b/dart/pubspec.yaml @@ -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: diff --git a/dart/test/html_test.dart b/dart/test/html_test.dart index 9ce53399..e407315c 100644 --- a/dart/test/html_test.dart +++ b/dart/test/html_test.dart @@ -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(); From 40d4a1297586fb2a2e4f102ce8abcb293f959943 Mon Sep 17 00:00:00 2001 From: Marc Fisher Date: Mon, 15 Dec 2014 15:54:45 -0800 Subject: [PATCH 2/3] Add comment about not working in Dartium with reference to bug. --- dart/lib/html.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dart/lib/html.dart b/dart/lib/html.dart index 3e09c346..2fc46ad1 100644 --- a/dart/lib/html.dart +++ b/dart/lib/html.dart @@ -193,9 +193,12 @@ abstract class HtmlPageLoaderElement implements PageLoaderElement { 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); + element.dispatchEvent( + new KeyEvent('keypress', charCode: charCode).wrapped); } } } From 0bdbe53b28824b2d9b60c747560ea74c81dffb9f Mon Sep 17 00:00:00 2001 From: Marc Fisher Date: Mon, 15 Dec 2014 16:06:00 -0800 Subject: [PATCH 3/3] Changes in response to comments. --- dart/lib/html.dart | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dart/lib/html.dart b/dart/lib/html.dart index 2fc46ad1..d566b689 100644 --- a/dart/lib/html.dart +++ b/dart/lib/html.dart @@ -246,17 +246,14 @@ class _ElementPageLoaderElement extends HtmlPageLoaderElement { @override void type(String keys) { + node.focus(); + _fireKeyPressEvents(node, keys); if (node is InputElement) { - node.focus(); - _fireKeyPressEvents(node, keys); var value = (node as InputElement).value + keys; (node as InputElement).value = ''; node.dispatchEvent(new TextEvent('textInput', data: value)); - node.blur(); - } else { - document.body.focus(); - _fireKeyPressEvents(node, keys); } + node.blur(); loader.sync(); } @@ -325,8 +322,11 @@ class _DocumentPageLoaderElement extends HtmlPageLoaderElement { PageLoaderAttributes get style => super.style; @override 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(); } }