@@ -275,13 +275,13 @@ public function parse_element_locator(string $text): string {
275275 );
276276
277277 $ locator = [
278- 'text ' => str_replace ('\\" ' , '" ' , $ matches [1 ]),
278+ 'text ' => $ this -> transform_time_to_string ( str_replace ('\\" ' , '" ' , $ matches [1 ]) ),
279279 'selector ' => $ matches [2 ] ?? null ,
280280 ];
281281
282282 if (!empty ($ matches [3 ])) {
283283 $ locator [$ matches [3 ]] = (object ) [
284- 'text ' => str_replace ('\\" ' , '" ' , $ matches [4 ]),
284+ 'text ' => $ this -> transform_time_to_string ( str_replace ('\\" ' , '" ' , $ matches [4 ]) ),
285285 'selector ' => $ matches [5 ] ?? null ,
286286 ];
287287 }
@@ -608,4 +608,37 @@ protected function resize_app_window(int $width = 500, int $height = 720) {
608608
609609 $ this ->getSession ()->getDriver ()->resizeWindow ($ width + $ offset ['x ' ], $ height + $ offset ['y ' ]);
610610 }
611+
612+ /**
613+ * Given a string, search if it contains a time with the ## format and convert it to a timestamp or readable time.
614+ * Only allows 1 occurence, if the text contains more than one time sub-string it won't work as expected.
615+ * This function is similar to the arg_time_to_string transformation, but it allows the time to be a sub-text of the string.
616+ *
617+ * @param string $text
618+ * @return string Transformed text.
619+ */
620+ protected function transform_time_to_string (string $ text ): string {
621+ if (!preg_match ('/##(.*)##/ ' , $ text , $ matches )) {
622+ // No time found, return the original text.
623+ return $ text ;
624+ }
625+
626+ $ timepassed = explode ('## ' , $ matches [1 ]);
627+
628+ // If not a valid time string, then just return what was passed.
629+ if ((($ timestamp = strtotime ($ timepassed [0 ])) === false )) {
630+ return $ text ;
631+ }
632+
633+ $ count = count ($ timepassed );
634+ if ($ count === 2 ) {
635+ // If timestamp with specified strftime format, then return formatted date string.
636+ return str_replace ($ matches [0 ], userdate ($ timestamp , $ timepassed [1 ]), $ text );
637+ } else if ($ count === 1 ) {
638+ return str_replace ($ matches [0 ], $ timestamp , $ text );
639+ } else {
640+ // If not a valid time string, then just return what was passed.
641+ return $ text ;
642+ }
643+ }
611644}
0 commit comments