@@ -37,6 +37,7 @@ class TestGenerator
37
37
const TEST_SCOPE = 'test ' ;
38
38
const HOOK_SCOPE = 'hook ' ;
39
39
const SUITE_SCOPE = 'suite ' ;
40
+ const PRESSKEY_ARRAY_ANCHOR_KEY = '987654321098765432109876543210 ' ;
40
41
41
42
/**
42
43
* Path to the export dir.
@@ -960,24 +961,7 @@ public function generateStepsPhp($actionObjects, $generationScope = TestGenerato
960
961
case "pressKey " :
961
962
$ parameterArray = $ customActionAttributes ['parameterArray ' ] ?? null ;
962
963
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 );
981
965
}
982
966
$ testSteps .= $ this ->wrapFunctionCall (
983
967
$ actor ,
@@ -1642,6 +1626,62 @@ private function addUniquenessToParamArray($input)
1642
1626
return implode (", " , $ result );
1643
1627
}
1644
1628
1629
+ private function processPressKey ($ input )
1630
+ {
1631
+ // validate the param array is in the correct format
1632
+ $ input = trim ($ input );
1633
+ $ this ->validateParameterArray ($ input );
1634
+ // trim off the outer braces
1635
+ $ input = substr ($ input , 1 , strlen ($ input ) - 2 );
1636
+
1637
+ $ result = [];
1638
+ $ arrayResult = [];
1639
+ $ count = 0 ;
1640
+
1641
+ // matches arrays
1642
+ preg_match_all ('/[\[][^\]]*?[\]]/ ' , $ input , $ paramInput );
1643
+ if (!empty ($ paramInput )) {
1644
+ foreach ($ paramInput [0 ] as $ param ) {
1645
+ $ arrayResult [static ::PRESSKEY_ARRAY_ANCHOR_KEY . $ count ] = '[ ' . trim ($ this ->addUniquenessToParamArray ($ param )) . '] ' ;
1646
+ $ input = str_replace ($ param , static ::PRESSKEY_ARRAY_ANCHOR_KEY . $ count , $ input );
1647
+ $ count ++;
1648
+ }
1649
+ }
1650
+
1651
+ $ paramArray = explode (", " , $ input );
1652
+ foreach ($ paramArray as $ param ) {
1653
+ // matches strings wrapped in ', we assume these are string literals
1654
+ if (preg_match ('/^[\s]*( \'.*? \')[\s]*$/ ' , $ param )) {
1655
+ $ result [] = trim ($ param );
1656
+ continue ;
1657
+ }
1658
+
1659
+ // matches \ for Facebook WebDriverKeys classes
1660
+ if (substr (trim ($ param ), 0 , 1 ) === '\\' ) {
1661
+ $ result [] = trim ($ param );
1662
+ continue ;
1663
+ }
1664
+
1665
+ // Matches numbers
1666
+ if (preg_match ('/^[\s]*(\d+?)[\s]*$/ ' , $ param )) {
1667
+ $ result [] = $ param ;
1668
+ continue ;
1669
+ }
1670
+
1671
+ $ replacement = $ this ->addUniquenessFunctionCall (trim ($ param ));
1672
+
1673
+ $ result [] = $ replacement ;
1674
+ }
1675
+
1676
+ $ result = implode (', ' , $ result );
1677
+ if (!empty ($ arrayResult )) {
1678
+ foreach ($ arrayResult as $ key => $ value ) {
1679
+ $ result = str_replace ($ key , $ value , $ result );
1680
+ }
1681
+ }
1682
+ return $ result ;
1683
+ }
1684
+
1645
1685
/**
1646
1686
* Add uniqueness function call to input string based on regex pattern.
1647
1687
*
0 commit comments