@@ -212,15 +212,8 @@ public function getTabIdentifiers(string $sessionIdentifier): PromiseInterface
212212 $ responsePromise ->then (
213213 function (ResponseInterface $ response ) use ($ tabLookupDeferred ) {
214214 try {
215- $ responseBody = (string ) $ response ->getBody ();
216- $ bodyDeserialized = json_decode ($ responseBody , true );
215+ $ tabIdentifiers = $ this ->deserializeResponse ($ response );
217216
218- if (!array_key_exists ('value ' , $ bodyDeserialized ) || !is_array ($ bodyDeserialized ['value ' ])) {
219- // todo: locate an error message or set it as "undefined error"
220- throw new RuntimeException ('Unable to locate tab identifiers in the response. ' );
221- }
222-
223- $ tabIdentifiers = $ bodyDeserialized ['value ' ];
224217 $ tabLookupDeferred ->resolve ($ tabIdentifiers );
225218 } catch (Throwable $ exception ) {
226219 $ reason = new RuntimeException (
@@ -295,7 +288,7 @@ function (ResponseInterface $response) {
295288 ->then (
296289 null ,
297290 function (Throwable $ rejectionReason ) {
298- throw new RuntimeException ('Unable to open an URI (request) . ' , 0 , $ rejectionReason );
291+ throw new RuntimeException ('Unable to open an URI. ' , 0 , $ rejectionReason );
299292 }
300293 )
301294 ;
@@ -366,7 +359,7 @@ function (ResponseInterface $response) {
366359 ->then (
367360 null ,
368361 function (Throwable $ rejectionReason ) {
369- throw new RuntimeException ('Unable to get an element identifier (request) . ' , 0 , $ rejectionReason );
362+ throw new RuntimeException ('Unable to get an element identifier. ' , 0 , $ rejectionReason );
370363 }
371364 )
372365 ;
@@ -456,7 +449,7 @@ function (ResponseInterface $response) {
456449 ->then (
457450 null ,
458451 function (Throwable $ rejectionReason ) {
459- throw new RuntimeException ('Unable to confirm mouse move action (request) . ' , 0 , $ rejectionReason );
452+ throw new RuntimeException ('Unable to confirm mouse move action. ' , 0 , $ rejectionReason );
460453 }
461454 )
462455 ;
@@ -493,7 +486,7 @@ function (ResponseInterface $response) {
493486 ->then (
494487 null ,
495488 function (Throwable $ rejectionReason ) {
496- throw new RuntimeException ('Unable to confirm mouse click action (request) . ' , 0 , $ rejectionReason );
489+ throw new RuntimeException ('Unable to confirm mouse click action. ' , 0 , $ rejectionReason );
497490 }
498491 )
499492 ;
@@ -506,9 +499,37 @@ function (Throwable $rejectionReason) {
506499 */
507500 public function getScreenshot (string $ sessionIdentifier ): PromiseInterface
508501 {
509- // TODO: Implement getScreenshot() method.
502+ $ requestUri = sprintf (
503+ 'http://%s:%d/wd/hub/session/%s/screenshot ' ,
504+ $ this ->_options ['server ' ]['host ' ],
505+ $ this ->_options ['server ' ]['port ' ],
506+ $ sessionIdentifier
507+ );
510508
511- return reject (new RuntimeException ('Not implemented. ' ));
509+ $ requestHeaders = [
510+ 'Content-Type ' => 'application/json; charset=UTF-8 ' ,
511+ ];
512+
513+ $ responsePromise = $ this ->httpClient ->get ($ requestUri , $ requestHeaders );
514+
515+ $ imageContentsPromise = $ responsePromise
516+ ->then (
517+ function (ResponseInterface $ response ) {
518+ $ imageContentsEncoded = $ this ->deserializeResponse ($ response );
519+ $ imageContents = base64_decode ($ imageContentsEncoded );
520+
521+ return $ imageContents ;
522+ }
523+ )
524+ ->then (
525+ null ,
526+ function (Throwable $ rejectionReason ) {
527+ throw new RuntimeException ('Unable to get a screenshot. ' , 0 , $ rejectionReason );
528+ }
529+ )
530+ ;
531+
532+ return $ imageContentsPromise ;
512533 }
513534
514535 /**
@@ -556,23 +577,43 @@ private function requestMouseActions(string $sessionIdentifier, array $mouseActi
556577 /**
557578 * Ensures that a related action is properly executed (confirmed) by the remote server, triggers an error otherwise.
558579 *
559- * Is used when no specific context to check is required (some methods will use more advanced confirmation checks
560- * instead this "default").
580+ * It is used when no specific context is required to confirm successful command execution (some methods will use
581+ * more advanced confirmation checks instead of this "default").
561582 *
562583 * @param ResponseInterface $response PSR-7 response message from the Selenium hub with action results
563584 * @param string $errorMessage Will be used if an error is registered during confirmation check
564585 *
565586 * @return void
566587 *
567- * @throws RuntimeException Whenever an error has occurred during confirmation check for a remote action
588+ * @throws RuntimeException Whenever an error has been occurred during confirmation check for a remote action
568589 */
569590 private function onCommandConfirmation (ResponseInterface $ response , string $ errorMessage ): void
570591 {
571- $ responseBody = ( string ) $ response -> getBody ( );
592+ $ responseValueNode = $ this -> deserializeResponse ( $ response );
572593
573594 // todo: locate an error message or set it as "undefined error"
574- if (' {"value": null} ' !== $ responseBody ) {
595+ if (null !== $ responseValueNode ) {
575596 throw new RuntimeException ($ errorMessage );
576597 }
577598 }
599+
600+ /**
601+ * Returns a "value" node contents, which will be extracted from the PSR-7 response message
602+ *
603+ * @param ResponseInterface $response PSR-7 response message from the Selenium hub with action results
604+ *
605+ * @return mixed
606+ */
607+ private function deserializeResponse (ResponseInterface $ response )
608+ {
609+ $ responseBody = (string ) $ response ->getBody ();
610+ $ bodyDeserialized = json_decode ($ responseBody , true );
611+
612+ if (!array_key_exists ('value ' , $ bodyDeserialized )) {
613+ // todo: locate an error message or set it as "undefined error"
614+ throw new RuntimeException ('Unable to locate "value" node (response deserialization). ' );
615+ }
616+
617+ return $ bodyDeserialized ['value ' ];
618+ }
578619}
0 commit comments