@@ -449,19 +449,7 @@ public function getElementIdentifier(string $sessionIdentifier, string $xpathQue
449449 $ elementIdentifierPromise = $ responsePromise
450450 ->then (
451451 function (ResponseInterface $ response ) {
452- $ responseBody = (string ) $ response ->getBody ();
453- preg_match (
454- '/(element(?:-[a-z\d]{4}){4}[a-z\d]{8})[":\s]+([a-z\d]{8}(?:-[a-z\d]{4}){4}[a-z\d]{8})/Ui ' ,
455- $ responseBody ,
456- $ matches
457- );
458-
459- if (!isset ($ matches [1 ], $ matches [2 ])) {
460- // todo: locate an error message or set it as "undefined error"
461- throw new RuntimeException ('Unable to locate an element identifier in the response. ' );
462- }
463-
464- $ elementIdentifier = [$ matches [1 ] => $ matches [2 ]];
452+ $ elementIdentifier = $ this ->extractElementIdentifier ($ response );
465453
466454 return $ elementIdentifier ;
467455 }
@@ -480,11 +468,42 @@ function (Throwable $rejectionReason) {
480468 /**
481469 * {@inheritDoc}
482470 */
483- public function getActiveElement (string $ sessionIdentifier ): PromiseInterface
471+ public function getActiveElementIdentifier (string $ sessionIdentifier ): PromiseInterface
484472 {
485- // TODO: Implement getActiveElement() method.
473+ $ requestUri = sprintf (
474+ 'http://%s:%d/wd/hub/session/%s/element/active ' ,
475+ $ this ->_options ['server ' ]['host ' ],
476+ $ this ->_options ['server ' ]['port ' ],
477+ $ sessionIdentifier
478+ );
486479
487- return reject (new RuntimeException ('Not implemented. ' ));
480+ $ requestHeaders = [
481+ 'Content-Type ' => 'application/json; charset=UTF-8 ' ,
482+ ];
483+
484+ $ responsePromise = $ this ->httpClient ->get ($ requestUri , $ requestHeaders );
485+
486+ $ elementIdentifierPromise = $ responsePromise
487+ ->then (
488+ function (ResponseInterface $ response ) {
489+ $ elementIdentifier = $ this ->extractElementIdentifier ($ response );
490+
491+ return $ elementIdentifier ;
492+ }
493+ )
494+ ->then (
495+ null ,
496+ function (Throwable $ rejectionReason ) {
497+ throw new RuntimeException (
498+ 'Unable to get an identifier of the active element. ' ,
499+ 0 ,
500+ $ rejectionReason
501+ );
502+ }
503+ )
504+ ;
505+
506+ return $ elementIdentifierPromise ;
488507 }
489508
490509 /**
@@ -494,7 +513,7 @@ public function getElementVisibility(string $sessionIdentifier, array $elementId
494513 {
495514 // todo: safer checks (or hide internals behind a transfer object/contract)
496515 $ elementHandle = array_key_first ($ elementIdentifier );
497- if (!isset ( $ elementIdentifier [ $ elementHandle] )) {
516+ if (!is_string ( $ elementHandle )) {
498517 throw new RuntimeException ('Unexpected format for the element identifier. ' );
499518 }
500519
@@ -796,4 +815,31 @@ private function deserializeResponse(ResponseInterface $response)
796815
797816 return $ bodyDeserialized ['value ' ];
798817 }
818+
819+ /**
820+ * Returns an element identifier, which has to be extracted from the response message (a surgical approach)
821+ *
822+ * @param ResponseInterface $response PSR-7 response message from the Selenium hub with action results
823+ *
824+ * @return array
825+ */
826+ private function extractElementIdentifier (ResponseInterface $ response ): array
827+ {
828+ $ responseBody = (string ) $ response ->getBody ();
829+
830+ preg_match (
831+ '/(element(?:-[a-z\d]{4}){4}[a-z\d]{8})[":\s]+([a-z\d]{8}(?:-[a-z\d]{4}){4}[a-z\d]{8})/Ui ' ,
832+ $ responseBody ,
833+ $ matches
834+ );
835+
836+ if (!isset ($ matches [1 ], $ matches [2 ])) {
837+ // todo: locate an error message or set it as "undefined error"
838+ throw new RuntimeException ('Unable to locate element identifier parts in the response. ' );
839+ }
840+
841+ $ elementIdentifier = [$ matches [1 ] => $ matches [2 ]];
842+
843+ return $ elementIdentifier ;
844+ }
799845}
0 commit comments