1515
1616namespace Facebook \WebDriver \Remote ;
1717
18+ use Facebook \WebDriver \WebDriver ;
1819use Facebook \WebDriver \WebDriverKeyboard ;
1920use Facebook \WebDriver \WebDriverKeys ;
2021
2324 */
2425class RemoteKeyboard implements WebDriverKeyboard
2526{
26- /**
27- * @var RemoteExecuteMethod
28- */
27+ /** @var RemoteExecuteMethod */
2928 private $ executor ;
29+ /** @var WebDriver */
30+ private $ driver ;
31+ /** @var bool */
32+ private $ isW3cCompliant ;
3033
3134 /**
32- * @param RemoteExecuteMethod $executor
35+ * @param bool $isW3cCompliant
3336 */
34- public function __construct (RemoteExecuteMethod $ executor )
37+ public function __construct (RemoteExecuteMethod $ executor, WebDriver $ driver , $ isW3cCompliant = false )
3538 {
3639 $ this ->executor = $ executor ;
40+ $ this ->driver = $ driver ;
41+ $ this ->isW3cCompliant = $ isW3cCompliant ;
3742 }
3843
3944 /**
@@ -43,9 +48,14 @@ public function __construct(RemoteExecuteMethod $executor)
4348 */
4449 public function sendKeys ($ keys )
4550 {
46- $ this ->executor ->execute (DriverCommand::SEND_KEYS_TO_ACTIVE_ELEMENT , [
47- 'value ' => WebDriverKeys::encode ($ keys ),
48- ]);
51+ if ($ this ->isW3cCompliant ) {
52+ $ activeElement = $ this ->driver ->switchTo ()->activeElement ();
53+ $ activeElement ->sendKeys ($ keys );
54+ } else {
55+ $ this ->executor ->execute (DriverCommand::SEND_KEYS_TO_ACTIVE_ELEMENT , [
56+ 'value ' => WebDriverKeys::encode ($ keys ),
57+ ]);
58+ }
4959
5060 return $ this ;
5161 }
@@ -59,9 +69,21 @@ public function sendKeys($keys)
5969 */
6070 public function pressKey ($ key )
6171 {
62- $ this ->executor ->execute (DriverCommand::SEND_KEYS_TO_ACTIVE_ELEMENT , [
63- 'value ' => [(string ) $ key ],
64- ]);
72+ if ($ this ->isW3cCompliant ) {
73+ $ this ->executor ->execute (DriverCommand::ACTIONS , [
74+ 'actions ' => [
75+ [
76+ 'type ' => 'key ' ,
77+ 'id ' => 'keyboard ' ,
78+ 'actions ' => [['type ' => 'keyDown ' , 'value ' => $ key ]],
79+ ],
80+ ],
81+ ]);
82+ } else {
83+ $ this ->executor ->execute (DriverCommand::SEND_KEYS_TO_ACTIVE_ELEMENT , [
84+ 'value ' => [(string ) $ key ],
85+ ]);
86+ }
6587
6688 return $ this ;
6789 }
@@ -75,9 +97,21 @@ public function pressKey($key)
7597 */
7698 public function releaseKey ($ key )
7799 {
78- $ this ->executor ->execute (DriverCommand::SEND_KEYS_TO_ACTIVE_ELEMENT , [
79- 'value ' => [(string ) $ key ],
80- ]);
100+ if ($ this ->isW3cCompliant ) {
101+ $ this ->executor ->execute (DriverCommand::ACTIONS , [
102+ 'actions ' => [
103+ [
104+ 'type ' => 'key ' ,
105+ 'id ' => 'keyboard ' ,
106+ 'actions ' => [['type ' => 'keyUp ' , 'value ' => $ key ]],
107+ ],
108+ ],
109+ ]);
110+ } else {
111+ $ this ->executor ->execute (DriverCommand::SEND_KEYS_TO_ACTIVE_ELEMENT , [
112+ 'value ' => [(string ) $ key ],
113+ ]);
114+ }
81115
82116 return $ this ;
83117 }
0 commit comments