Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
This project versioning adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
### Fixed
- Accept array as possible input to `sendKeys()` method. (Unintentional BC break in 1.8.0.)

## 1.8.0 - 2020-02-10
### Added
Expand Down
2 changes: 1 addition & 1 deletion lib/Remote/RemoteWebElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public function sendKeys($value)
if ($local_file === null) {
if ($this->isW3cCompliant) {
$params = [
'text' => (string) $value,
'text' => WebDriverKeys::encode($value, true),
':id' => $this->id,
];
} else {
Expand Down
20 changes: 15 additions & 5 deletions lib/WebDriverKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ class WebDriverKeys
const COMMAND = self::META;

/**
* Encode input of `sendKeys()`.
* Encode input of `sendKeys()` to appropriate format according to protocol.
*
* @param string|array|int|float $keys
* @return array
* @param bool $isW3cCompliant
* @return array|string
*/
public static function encode($keys)
public static function encode($keys, $isW3cCompliant = false)
{
if (is_numeric($keys)) {
$keys = (string) $keys;
Expand All @@ -105,7 +107,11 @@ public static function encode($keys)
}

if (!is_array($keys)) {
return [];
if (!$isW3cCompliant) {
return [];
}

return '';
}

$encoded = [];
Expand All @@ -117,6 +123,10 @@ public static function encode($keys)
$encoded[] = (string) $key;
}

return $encoded;
if (!$isW3cCompliant) {
return $encoded;
}

return implode('', $encoded);
}
}
5 changes: 5 additions & 0 deletions tests/functional/RemoteWebElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public function testShouldSendKeysToFormElement()
$this->assertSame('foo bar', $textarea->getAttribute('value'));
$textarea->sendKeys(' baz');
$this->assertSame('foo bar baz', $textarea->getAttribute('value'));

// Send keys as array
$textarea->clear();
$textarea->sendKeys(['bat', 1, '3', ' ', 3, '7']);
$this->assertSame('bat13 37', $textarea->getAttribute('value'));
}

/**
Expand Down
39 changes: 27 additions & 12 deletions tests/unit/WebDriverKeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class WebDriverKeysTest extends TestCase
/**
* @dataProvider provideKeys
* @param mixed $keys
* @param array $expectedOutput
* @param array $expectedOssOutput
* @param string $expectedW3cOutput
*/
public function testShouldEncodeKeysToArrayOfStrings($keys, $expectedOutput)
public function testShouldEncodeKeysToFormatOfEachProtocol($keys, $expectedOssOutput, $expectedW3cOutput)
{
$this->assertSame($expectedOutput, WebDriverKeys::encode($keys));
$this->assertSame($expectedOssOutput, WebDriverKeys::encode($keys));
$this->assertSame($expectedW3cOutput, WebDriverKeys::encode($keys, true));
}

/**
Expand All @@ -25,19 +27,32 @@ public function testShouldEncodeKeysToArrayOfStrings($keys, $expectedOutput)
public function provideKeys()
{
return [
'empty string' => ['', ['']],
'simple string' => ['foo', ['foo']],
'string as an array' => [['foo'], ['foo']],
'string with modifier as an array' => [[WebDriverKeys::SHIFT, 'foo'], [WebDriverKeys::SHIFT, 'foo']],
'string with concatenated modifier' => [[WebDriverKeys::SHIFT . 'foo'], [WebDriverKeys::SHIFT . 'foo']],
'simple numeric value' => [3, ['3']],
'multiple numeric values' => [[1, 3.33], ['1', '3.33']],
'multiple mixed values ' => [['foo', WebDriverKeys::END, '1.234'], ['foo', WebDriverKeys::END, '1.234']],
'empty string' => ['', [''], ''],
'simple string' => ['foo', ['foo'], 'foo'],
'string as an array' => [['foo'], ['foo'], 'foo'],
'string with modifier as an array' => [
[WebDriverKeys::SHIFT, 'foo'],
[WebDriverKeys::SHIFT, 'foo'],
WebDriverKeys::SHIFT . 'foo',
],
'string with concatenated modifier' => [
[WebDriverKeys::SHIFT . 'foo'],
[WebDriverKeys::SHIFT . 'foo'],
WebDriverKeys::SHIFT . 'foo',
],
'simple numeric value' => [3, ['3'], '3'],
'multiple numeric values' => [[1, 3.33], ['1', '3.33'], '13.33'],
'multiple mixed values ' => [
['foo', WebDriverKeys::END, '1.234'],
['foo', WebDriverKeys::END, '1.234'],
'foo' . WebDriverKeys::END . '1.234',
],
'array of strings with modifiers should separate them with NULL character' => [
[[WebDriverKeys::SHIFT, 'foo'], [WebDriverKeys::META, 'bar']],
[WebDriverKeys::SHIFT . 'foo' . WebDriverKeys::NULL, WebDriverKeys::META . 'bar' . WebDriverKeys::NULL],
WebDriverKeys::SHIFT . 'foo' . WebDriverKeys::NULL . WebDriverKeys::META . 'bar' . WebDriverKeys::NULL,
],
'null' => [null, []],
'null' => [null, [], ''],
];
}
}