@@ -125,40 +125,38 @@ public function keyboardPressKeyOnElementSingle(string $char, ?string $selector)
125
125
];
126
126
127
127
// Convert provided character sequence to special keys.
128
- if (is_string ($ char )) {
129
- if (strlen ($ char ) < 1 ) {
130
- throw new \Exception ('keyPress($char) was invoked but the $char parameter was empty. ' );
128
+ if (strlen ($ char ) < 1 ) {
129
+ throw new \Exception ('keyPress($char) was invoked but the $char parameter was empty. ' );
130
+ }
131
+ // Consider provided characters string longer then 1 to be a keyboard key.
132
+ elseif (strlen ($ char ) > 1 ) {
133
+ if (!array_key_exists (strtolower ($ char ), $ keys )) {
134
+ throw new \RuntimeException (sprintf ('Unsupported key "%s" provided ' , $ char ));
131
135
}
132
- // Consider provided characters string longer then 1 to be a keyboard key.
133
- elseif (strlen ($ char ) > 1 ) {
134
- if (!array_key_exists (strtolower ($ char ), $ keys )) {
135
- throw new \RuntimeException (sprintf ('Unsupported key "%s" provided ' , $ char ));
136
- }
137
-
138
- // Special case for tab key triggered in window without target element
139
- // focused: Syn (JS library that provides synthetic events) can tab only
140
- // from another element that can receive focus, so we inject such
141
- // element as a very first element after opening <body> tag. This
142
- // element is visually hidden, but compatible with screen readers. Then
143
- // we trigger key on this element to make sure that an element that
144
- // supposed to get the very first focus from tab index actually gets it.
145
- // Note that injecting element and triggering key press on it does not
146
- // make it focused itself.
147
- if (is_null ($ selector ) && $ char === 'tab ' ) {
148
- $ selector = '#injected-focusable ' ;
149
-
150
- $ script = <<<JS
151
- (function() {
152
- if (document.querySelectorAll('body #injected-focusable').length === 0) {
153
- document.querySelector('body').insertAdjacentHTML('afterbegin', '<a id="injected-focusable" style="position: absolute;width: 1px;height: 1px;margin: -1px;padding: 0;overflow: hidden;clip: rect(0,0,0,0);border: 0;"></a>');
154
- }
155
- })();
156
- JS ;
157
- $ this ->getSession ()->getDriver ()->evaluateScript ($ script );
158
- }
159
-
160
- $ char = $ keys [strtolower ($ char )];
136
+
137
+ // Special case for tab key triggered in window without target element
138
+ // focused: Syn (JS library that provides synthetic events) can tab only
139
+ // from another element that can receive focus, so we inject such
140
+ // element as a very first element after opening <body> tag. This
141
+ // element is visually hidden, but compatible with screen readers. Then
142
+ // we trigger key on this element to make sure that an element that
143
+ // supposed to get the very first focus from tab index actually gets it.
144
+ // Note that injecting element and triggering key press on it does not
145
+ // make it focused itself.
146
+ if (is_null ($ selector ) && $ char === 'tab ' ) {
147
+ $ selector = '#injected-focusable ' ;
148
+
149
+ $ script = <<<JS
150
+ (function() {
151
+ if (document.querySelectorAll('body #injected-focusable').length === 0) {
152
+ document.querySelector('body').insertAdjacentHTML('afterbegin', '<a id="injected-focusable" style="position: absolute;width: 1px;height: 1px;margin: -1px;padding: 0;overflow: hidden;clip: rect(0,0,0,0);border: 0;"></a>');
153
+ }
154
+ })();
155
+ JS ;
156
+ $ this ->getSession ()->getDriver ()->evaluateScript ($ script );
161
157
}
158
+
159
+ $ char = $ keys [strtolower ($ char )];
162
160
}
163
161
164
162
$ selector = $ selector ?: 'html ' ;
0 commit comments