Skip to content

Commit 75af11a

Browse files
committed
Do not throw fatal error when null is passed to sendKeys (fixes php-webdriver#419)
1 parent 6fb60b0 commit 75af11a

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ This project versioning adheres to [Semantic Versioning](http://semver.org/).
77
- Cookies retrieved using `getCookieNamed()` and `getCookies()` methods of `WebDriverOptions` are now encapsulated in `Cookie` object instead of an plain array. The object implements `ArrayAccess` interface to provide backward compatibility.
88
- `ext-zip` is now specified as required dependency in composer.json (but the extension was already required by the code, though).
99

10+
### Fixed
11+
- Do not throw fatal error when `null` is passed to `sendKeys()`.
12+
1013
## 1.3.0 - 2017-01-13
1114
### Added
1215
- Added `getCapabilities()` method of `RemoteWebDriver`, to retrieve actual capabilities acknowledged by the remote driver on startup.

lib/Interactions/WebDriverCompositeAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class WebDriverCompositeAction implements WebDriverAction
2424
{
2525
/**
26-
* @var array
26+
* @var WebDriverAction[]
2727
*/
2828
private $actions = [];
2929

lib/WebDriverKeys.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,28 @@ class WebDriverKeys
8888

8989
/**
9090
* Encode input of `sendKeys()`.
91-
* @param string|array $keys
91+
* @param string|array|int|float $keys
9292
* @return array
9393
*/
9494
public static function encode($keys)
9595
{
9696
if (is_numeric($keys)) {
97-
$keys = '' . $keys;
97+
$keys = (string) $keys;
9898
}
9999

100100
if (is_string($keys)) {
101101
$keys = [$keys];
102102
}
103103

104+
if (!is_array($keys)) {
105+
return [];
106+
}
107+
104108
$encoded = [];
105109
foreach ($keys as $key) {
106110
if (is_array($key)) {
107-
// handle modified keys
108-
$key = implode('', $key) . self::NULL;
111+
// handle key modifiers
112+
$key = implode('', $key) . self::NULL; // the NULL clears the input state (eg. previous modifiers)
109113
}
110114
$encoded[] = (string) $key;
111115
}

tests/unit/WebDriverKeysTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace Facebook\WebDriver;
17+
18+
/**
19+
* @covers Facebook\WebDriver\WebDriverKeys
20+
*/
21+
class WebDriverKeysTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @dataProvider provideKeys
25+
* @param mixed $keys
26+
* @param array $expectedOutput
27+
*/
28+
public function testShouldEncodeKeysToArrayOfStrings($keys, $expectedOutput)
29+
{
30+
$this->assertSame($expectedOutput, WebDriverKeys::encode($keys));
31+
}
32+
33+
/**
34+
* @return array[]
35+
*/
36+
public function provideKeys()
37+
{
38+
return [
39+
'empty string' => ['', ['']],
40+
'simple string' => ['foo', ['foo']],
41+
'string as an array' => [['foo'], ['foo']],
42+
'string with modifier as an array' => [[WebDriverKeys::SHIFT, 'foo'], [WebDriverKeys::SHIFT, 'foo']],
43+
'string with concatenated modifier' => [[WebDriverKeys::SHIFT . 'foo'], [WebDriverKeys::SHIFT . 'foo']],
44+
'simple numeric value' => [3, ['3']],
45+
'multiple numeric values' => [[1, 3.33], ['1', '3.33']],
46+
'multiple mixed values ' => [['foo', WebDriverKeys::END, '1.234'], ['foo', WebDriverKeys::END, '1.234']],
47+
'array of strings with modifiers should separate them with NULL character' => [
48+
[[WebDriverKeys::SHIFT, 'foo'], [WebDriverKeys::META, 'bar']],
49+
[WebDriverKeys::SHIFT . 'foo' . WebDriverKeys::NULL, WebDriverKeys::META . 'bar' . WebDriverKeys::NULL],
50+
],
51+
'null' => [null, []],
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)