@@ -293,6 +293,8 @@ function (ResponseInterface $response) {
293293 if ('{"value":null} ' !== $ responseBody ) {
294294 throw new RuntimeException ('URI navigation is not confirmed. ' );
295295 }
296+
297+ return null ;
296298 }
297299 )
298300 ->then (
@@ -331,9 +333,50 @@ public function getSource(string $sessionIdentifier): PromiseInterface
331333 */
332334 public function getElementIdentifier (string $ sessionIdentifier , string $ xpathQuery ): PromiseInterface
333335 {
334- // TODO: Implement getElementIdentifier() method.
336+ $ requestUri = sprintf (
337+ 'http://%s:%d/wd/hub/session/%s/element ' ,
338+ $ this ->_options ['server ' ]['host ' ],
339+ $ this ->_options ['server ' ]['port ' ],
340+ $ sessionIdentifier
341+ );
335342
336- return reject (new RuntimeException ('Not implemented. ' ));
343+ $ requestHeaders = [
344+ 'Content-Type ' => 'application/json; charset=UTF-8 ' ,
345+ ];
346+
347+ $ requestContents = json_encode (['using ' => 'xpath ' , 'value ' => $ xpathQuery ]);
348+
349+ $ responsePromise = $ this ->httpClient ->post ($ requestUri , $ requestHeaders , $ requestContents );
350+
351+ $ elementIdentifierPromise = $ responsePromise
352+ ->then (
353+ function (ResponseInterface $ response ) {
354+ $ responseBody = (string ) $ response ->getBody ();
355+ preg_match (
356+ '/(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 ' ,
357+ $ responseBody ,
358+ $ matches
359+ );
360+
361+ if (!isset ($ matches [1 ], $ matches [2 ])) {
362+ // todo: locate an error message or set it as "undefined error"
363+ throw new RuntimeException ('Unable to locate an element identifier in the response. ' );
364+ }
365+
366+ $ elementIdentifier = [$ matches [1 ] => $ matches [2 ]];
367+
368+ return $ elementIdentifier ;
369+ }
370+ )
371+ ->then (
372+ null ,
373+ function (Throwable $ rejectionReason ) {
374+ throw new RuntimeException ('Unable to get an element identifier (request). ' , 0 , $ rejectionReason );
375+ }
376+ )
377+ ;
378+
379+ return $ elementIdentifierPromise ;
337380 }
338381
339382 /**
0 commit comments