@@ -37,6 +37,7 @@ class TestGenerator
3737 const TEST_SCOPE = 'test ' ;
3838 const HOOK_SCOPE = 'hook ' ;
3939 const SUITE_SCOPE = 'suite ' ;
40+ const PRESSKEY_ARRAY_ANCHOR_KEY = '987654321098765432109876543210 ' ;
4041
4142 /**
4243 * Path to the export dir.
@@ -960,24 +961,7 @@ public function generateStepsPhp($actionObjects, $generationScope = TestGenerato
960961 case "pressKey " :
961962 $ parameterArray = $ customActionAttributes ['parameterArray ' ] ?? null ;
962963 if ($ parameterArray ) {
963- // validate the param array is in the correct format
964- $ this ->validateParameterArray ($ parameterArray );
965-
966- // trim off the outer braces and add commas for the regex match
967- $ params = ", " . substr ($ parameterArray , 1 , strlen ($ parameterArray ) - 2 ) . ", " ;
968-
969- // we are matching any nested arrays for a simultaneous press, any string literals, and any
970- // explicit function calls from a class.
971- preg_match_all ('/(\[.*?\])|( \'.*? \')|( \\\\.*?\,)/ ' , $ params , $ paramInput );
972-
973- //clean up the input by trimming any extra commas
974- $ tmpParameterArray = [];
975- foreach ($ paramInput [0 ] as $ params ) {
976- $ tmpParameterArray [] = trim ($ params , ", " );
977- }
978-
979- // put the array together as a string to be passed as args
980- $ parameterArray = implode (", " , $ tmpParameterArray );
964+ $ parameterArray = $ this ->processPressKey ($ parameterArray );
981965 }
982966 $ testSteps .= $ this ->wrapFunctionCall (
983967 $ actor ,
@@ -1642,6 +1626,70 @@ private function addUniquenessToParamArray($input)
16421626 return implode (", " , $ result );
16431627 }
16441628
1629+ /**
1630+ * Process pressKey parameterArray attribute for uniqueness function call and necessary data resolutions
1631+ *
1632+ * @param string $input
1633+ * @return string
1634+ */
1635+ private function processPressKey ($ input )
1636+ {
1637+ // validate the param array is in the correct format
1638+ $ input = trim ($ input );
1639+ $ this ->validateParameterArray ($ input );
1640+ // trim off the outer braces
1641+ $ input = substr ($ input , 1 , strlen ($ input ) - 2 );
1642+
1643+ $ result = [];
1644+ $ arrayResult = [];
1645+ $ count = 0 ;
1646+
1647+ // matches all arrays and replaces them with placeholder to prevent later param manipulation
1648+ preg_match_all ('/[\[][^\]]*?[\]]/ ' , $ input , $ paramInput );
1649+ if (!empty ($ paramInput )) {
1650+ foreach ($ paramInput [0 ] as $ param ) {
1651+ $ arrayResult [self ::PRESSKEY_ARRAY_ANCHOR_KEY . $ count ] =
1652+ '[ ' . trim ($ this ->addUniquenessToParamArray ($ param )) . '] ' ;
1653+ $ input = str_replace ($ param , self ::PRESSKEY_ARRAY_ANCHOR_KEY . $ count , $ input );
1654+ $ count ++;
1655+ }
1656+ }
1657+
1658+ $ paramArray = explode (", " , $ input );
1659+ foreach ($ paramArray as $ param ) {
1660+ // matches strings wrapped in ', we assume these are string literals
1661+ if (preg_match ('/^[\s]*( \'.*? \')[\s]*$/ ' , $ param )) {
1662+ $ result [] = trim ($ param );
1663+ continue ;
1664+ }
1665+
1666+ // matches \ for Facebook WebDriverKeys classes
1667+ if (substr (trim ($ param ), 0 , 1 ) === '\\' ) {
1668+ $ result [] = trim ($ param );
1669+ continue ;
1670+ }
1671+
1672+ // matches numbers
1673+ if (preg_match ('/^[\s]*(\d+?)[\s]*$/ ' , $ param )) {
1674+ $ result [] = $ param ;
1675+ continue ;
1676+ }
1677+
1678+ $ replacement = $ this ->addUniquenessFunctionCall (trim ($ param ));
1679+
1680+ $ result [] = $ replacement ;
1681+ }
1682+
1683+ $ result = implode (', ' , $ result );
1684+ // reinsert arrays into result
1685+ if (!empty ($ arrayResult )) {
1686+ foreach ($ arrayResult as $ key => $ value ) {
1687+ $ result = str_replace ($ key , $ value , $ result );
1688+ }
1689+ }
1690+ return $ result ;
1691+ }
1692+
16451693 /**
16461694 * Add uniqueness function call to input string based on regex pattern.
16471695 *
0 commit comments