add chords support to keyboard.dart + test
diff --git a/lib/src/keyboard.dart b/lib/src/keyboard.dart
index fa211bb..4994c3e 100644
--- a/lib/src/keyboard.dart
+++ b/lib/src/keyboard.dart
@@ -74,6 +74,22 @@
Keyboard._(driver) : super(driver, '');
+ /// Simulate pressing many keys at once as a 'chord'.
+ Future sendChord(Iterable<String> chordToSend) async {
+ await sendKeys(createChord(chordToSend));
+ }
+
+ /// Creates a string representation of a chord suitable for use in WebDriver.
+ String createChord(Iterable<String> chord) {
+ StringBuffer chordString = new StringBuffer();
+ for (String s in chord) {
+ chordString.write(s);
+ }
+ chordString.write(nullChar);
+
+ return chordString.toString();
+ }
+
/// Send [keysToSend] to the active element.
Future sendKeys(String keysToSend) async {
await _post('keys', {
diff --git a/test/src/keyboard.dart b/test/src/keyboard.dart
index 3b303fb..f1ae076 100644
--- a/test/src/keyboard.dart
+++ b/test/src/keyboard.dart
@@ -14,6 +14,8 @@
library webdriver.keyboard_test;
+import 'dart:io';
+
import 'package:test/test.dart';
import 'package:webdriver/core.dart';
@@ -23,9 +25,20 @@
group('Keyboard', () {
WebDriver driver;
WebElement textInput;
+ String ctrlCmdKey = '';
setUp(() async {
- driver = await createTestDriver();
+ if (Platform.isMacOS) {
+ ctrlCmdKey = Keyboard.command;
+ } else {
+ ctrlCmdKey = Keyboard.control;
+ }
+
+ // Note: Firefox used as Chrome + Mac OSX prevents use of control/meta
+ // in chords.
+ // https://code.google.com/p/selenium/issues/detail?id=5919
+ driver = await createTestDriver(
+ additionalCapabilities: Capabilities.firefox);
await driver.get(testPagePath);
textInput =
await driver.findElement(const By.cssSelector('input[type=text]'));
@@ -49,5 +62,14 @@
await driver.keyboard.sendKeys('abc${Keyboard.tab}def');
expect(await textInput.attributes['value'], 'abc');
});
+
+ test('sendChord -- CTRL+X', () async {
+ await driver.keyboard.sendKeys('abcdef');
+ expect(await textInput.attributes['value'], 'abcdef');
+ await driver.keyboard.sendChord([ctrlCmdKey, 'a']);
+ await driver.keyboard.sendChord([ctrlCmdKey, 'x']);
+ await driver.keyboard.sendKeys('xxx');
+ expect(await textInput.attributes['value'], 'xxx');
+ });
});
}