Skip to content

Commit 6daf60a

Browse files
KevinBKozanokolesnyk
authored andcommitted
MQE-484: parameter array with data replacement does not generate uniqueness function correctly (#25)
- ParameterArray now correctly replaces "{{data.key}}" uniqueness. - Verification test creation - add support for consistent array declaration - change functionality for pressKey - add exception for improperly formatted parameter array
1 parent 07e5a06 commit 6daf60a

File tree

7 files changed

+211
-4
lines changed

7 files changed

+211
-4
lines changed

dev/tests/verification/Resources/BasicFunctionalCest.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ class BasicFunctionalCest
8989
$I->moveMouseOver(".functionalTestSelector");
9090
$I->openNewTab();
9191
$I->pauseExecution();
92-
$I->pressKey(".functionalTestSelector");
92+
$I->pressKey("#page", "a");
93+
$I->pressKey("#page", ['ctrl','a'],'new');
94+
$I->pressKey("#page", ['shift','111'],'1','x');
95+
$I->pressKey("#page", ['ctrl', 'a'],\Facebook\WebDriver\WebDriverKeys::DELETE);
9396
$I->reloadPage();
9497
$I->resetCookie("cookieInput");
9598
$I->resizeWindow(0, 0);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace Magento\AcceptanceTest\_generated\Backend;
3+
4+
use Magento\FunctionalTestingFramework\AcceptanceTester;
5+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
6+
use Magento\FunctionalTestingFramework\DataGenerator\Persist\DataPersistenceHandler;
7+
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
8+
use Yandex\Allure\Adapter\Annotation\Features;
9+
use Yandex\Allure\Adapter\Annotation\Stories;
10+
use Yandex\Allure\Adapter\Annotation\Title;
11+
use Yandex\Allure\Adapter\Annotation\Description;
12+
use Yandex\Allure\Adapter\Annotation\Parameter;
13+
use Yandex\Allure\Adapter\Annotation\Severity;
14+
use Yandex\Allure\Adapter\Model\SeverityLevel;
15+
use Yandex\Allure\Adapter\Annotation\TestCaseId;
16+
17+
/**
18+
*/
19+
class ParameterArrayCest
20+
{
21+
/**
22+
* @Parameter(name = "AcceptanceTester", value="$I")
23+
* @param AcceptanceTester $I
24+
* @return void
25+
*/
26+
public function ParameterArrayTest(AcceptanceTester $I)
27+
{
28+
$I->amGoingTo("create entity that has the mergeKey: simpleDataKey");
29+
$simpleParamData = DataObjectHandler::getInstance()->getObject("simpleParamData");
30+
$simpleDataKey = new DataPersistenceHandler($simpleParamData);
31+
$simpleDataKey->createEntity();
32+
$I->searchAndMultiSelectOption("#selector", ["name"]);
33+
$I->searchAndMultiSelectOption("#selector", [msq("simpleParamData")."prename"]);
34+
$I->searchAndMultiSelectOption("#selector", ["postname".msq("simpleParamData")]);
35+
$I->searchAndMultiSelectOption("#selector", [$simpleDataKey->getCreatedDataByName('name')]);
36+
$I->searchAndMultiSelectOption("#selector", ["name", $simpleDataKey->getCreatedDataByName('name')]);
37+
$I->searchAndMultiSelectOption("#selector", ['someKey' => $simpleDataKey->getCreatedDataByName('name')]);
38+
$I->searchAndMultiSelectOption("#selector", ['someKey' => "name"]);
39+
$I->searchAndMultiSelectOption("#selector", ['someKey' => msq("simpleParamData")."prename"]);
40+
$I->searchAndMultiSelectOption("#selector", ['someKey' => "postname".msq("simpleParamData")]);
41+
}
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="../../../../../../../../../../magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd">
11+
<cest name="ParameterArrayCest">
12+
<test name="ParameterArrayTest">
13+
<createData entity="simpleParamData" mergeKey="simpleDataKey"/>
14+
<searchAndMultiSelectOption selector="#selector" parameterArray="[{{simpleParamData.name}}]" mergeKey="xmlSimpleReplace"/>
15+
<searchAndMultiSelectOption selector="#selector" parameterArray="[{{simpleParamData.uniqueNamePre}}]" mergeKey="xmlPrefix"/>
16+
<searchAndMultiSelectOption selector="#selector" parameterArray="[{{simpleParamData.uniqueNamePost}}]" mergeKey="xmlSuffix"/>
17+
<searchAndMultiSelectOption selector="#selector" parameterArray="[$simpleDataKey.name$]" mergeKey="persistSimple"/>
18+
<searchAndMultiSelectOption selector="#selector" parameterArray="[{{simpleParamData.name}}, $simpleDataKey.name$]" mergeKey="persistXmlSimple"/>
19+
<searchAndMultiSelectOption selector="#selector" parameterArray="['someKey' => $simpleDataKey.name$]" mergeKey="literalKeyToPersist"/>
20+
<searchAndMultiSelectOption selector="#selector" parameterArray="['someKey' => {{simpleParamData.name}}]" mergeKey="literalKeyToStatic"/>
21+
<searchAndMultiSelectOption selector="#selector" parameterArray="['someKey' => {{simpleParamData.uniqueNamePre}}]" mergeKey="literalKeyToPrefixUnique"/>
22+
<searchAndMultiSelectOption selector="#selector" parameterArray="['someKey' => {{simpleParamData.uniqueNamePost}}]" mergeKey="literalKeyToSuffixUnique"/>
23+
</test>
24+
</cest>
25+
</config>

dev/tests/verification/TestModule/Cest/basicFunctionalCest.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@
7474
<moveMouseOver selector=".functionalTestSelector" mergeKey="moveMouseOverKey1"/>
7575
<openNewTab mergeKey="openNewTabKey1"/>
7676
<pauseExecution mergeKey="pauseExecutionKey1"/>
77-
<pressKey selector=".functionalTestSelector" mergeKey="pressKeyKey1"/>
77+
<pressKey selector="#page" userInput="a" mergeKey="pressKey1"/>
78+
<pressKey selector="#page" parameterArray="[['ctrl','a'],'new']" mergeKey="pressKey2"/>
79+
<pressKey selector="#page" parameterArray="[['shift','111'],'1','x']" mergeKey="pressKey3"/>
80+
<pressKey selector="#page" parameterArray="[['ctrl', 'a'], \Facebook\WebDriver\WebDriverKeys::DELETE]" mergeKey="pressKey4"/>
7881
<reloadPage mergeKey="reloadPageKey1"/>
7982
<resetCookie userInput="cookieInput" mergeKey="resetCookieKey1"/>
8083
<resizeWindow width="0" height="0" mergeKey="resizeWindowKey1" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataProfileSchema.xsd">
11+
<entity name="simpleParamData" type="data">
12+
<data key="name">name</data>
13+
<data key="uniqueNamePre" unique="prefix">prename</data>
14+
<data key="uniqueNamePost" unique="suffix">postname</data>
15+
</entity>
16+
</config>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace tests\verification\Tests;
7+
8+
use Magento\FunctionalTestingFramework\Test\Handlers\CestObjectHandler;
9+
use Magento\FunctionalTestingFramework\Util\TestGenerator;
10+
use PHPUnit\Framework\TestCase;
11+
use tests\verification\Util\FileDiffUtil;
12+
13+
class ParameterArrayTest extends TestCase
14+
{
15+
const PARAMETER_ARRAY_CEST = 'ParameterArrayCest';
16+
const RESOURCES_PATH = __DIR__ . '/../Resources';
17+
18+
/**
19+
* Tests flat generation of a hardcoded cest file with no external references.
20+
*/
21+
public function testParameterArrayGeneration()
22+
{
23+
$cest = CestObjectHandler::getInstance()->getObject(self::PARAMETER_ARRAY_CEST);
24+
$test = TestGenerator::getInstance(null, [$cest]);
25+
$test->createAllCestFiles();
26+
27+
$cestFile = $test->getExportDir() .
28+
DIRECTORY_SEPARATOR .
29+
self::PARAMETER_ARRAY_CEST .
30+
".php";
31+
32+
$this->assertTrue(file_exists($cestFile));
33+
34+
$fileDiffUtil = new FileDiffUtil(
35+
self::RESOURCES_PATH . DIRECTORY_SEPARATOR . self::PARAMETER_ARRAY_CEST . ".txt",
36+
$cestFile
37+
);
38+
39+
$diffResult = $fileDiffUtil->diffContents();
40+
$this->assertNull($diffResult, $diffResult);
41+
}
42+
}

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,13 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
375375
$time = $customActionAttributes['timeout'];
376376
}
377377

378-
if (isset($customActionAttributes['parameterArray'])) {
379-
$parameterArray = $customActionAttributes['parameterArray'];
378+
if (isset($customActionAttributes['parameterArray']) && $actionName != 'pressKey') {
379+
// validate the param array is in the correct format
380+
$this->validateParameterArray($customActionAttributes['parameterArray']);
381+
382+
$parameterArray = "[" . $this->addUniquenessToParamArray(
383+
$customActionAttributes['parameterArray']
384+
) . "]";
380385
}
381386

382387
if (isset($customActionAttributes['requiredAction'])) {
@@ -720,6 +725,29 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
720725
$testSteps .= $this->wrapFunctionCall($actor, $actionName, $selector, $parameterArray);
721726
break;
722727
case "pressKey":
728+
$parameterArray = $customActionAttributes['parameterArray'] ?? null;
729+
if ($parameterArray) {
730+
// validate the param array is in the correct format
731+
$this->validateParameterArray($parameterArray);
732+
733+
// trim off the outer braces and add commas for the regex match
734+
$params = "," . substr($parameterArray, 1, strlen($parameterArray) - 2) . ",";
735+
736+
// we are matching any nested arrays for a simultaneous press, any string literals, and any
737+
// explicit function calls from a class.
738+
preg_match_all('/(\[.*?\])|(\'.*?\')|(\\\\.*?\,)/', $params, $paramInput);
739+
740+
//clean up the input by trimming any extra commas
741+
$tmpParameterArray = [];
742+
foreach ($paramInput[0] as $params) {
743+
$tmpParameterArray[] = trim($params, ",");
744+
}
745+
746+
// put the array together as a string to be passed as args
747+
$parameterArray = implode(",", $tmpParameterArray);
748+
}
749+
$testSteps .= $this->wrapFunctionCall($actor, $actionName, $selector, $input, $parameterArray);
750+
break;
723751
case "selectOption":
724752
$testSteps .= $this->wrapFunctionCall($actor, $actionName, $selector, $input, $parameterArray);
725753
break;
@@ -1166,6 +1194,40 @@ private function generateTestsPhp($testsObject)
11661194
return $testPhp;
11671195
}
11681196

1197+
/**
1198+
* Detects uniqueness function calls on given attribute, and calls addUniquenessFunctionCall on matches.
1199+
* @param string $input
1200+
* @return string
1201+
*/
1202+
private function addUniquenessToParamArray($input)
1203+
{
1204+
$tempInput = trim($input, "[]");
1205+
$paramArray = explode(",", $tempInput);
1206+
$result = [];
1207+
1208+
foreach ($paramArray as $param) {
1209+
// Determine if param has key/value array notation
1210+
if (preg_match_all('/(.+)=>(.+)/', trim($param), $paramMatches)) {
1211+
$param1 = $this->addUniquenessToParamArray($paramMatches[1][0]);
1212+
$param2 = $this->addUniquenessToParamArray($paramMatches[2][0]);
1213+
$result[] = trim($param1) . " => " . trim($param2);
1214+
continue;
1215+
}
1216+
1217+
// Matches strings wrapped in ', we assume these are string literals
1218+
if (preg_match('/^(["\']).*\1$/m', trim($param))) {
1219+
$result[] = $param;
1220+
continue;
1221+
}
1222+
1223+
$replacement = $this->addUniquenessFunctionCall(trim($param));
1224+
1225+
$result[] = $replacement;
1226+
}
1227+
1228+
return implode(", ", $result);
1229+
}
1230+
11691231
/**
11701232
* Add uniqueness function call to input string based on regex pattern.
11711233
*
@@ -1300,4 +1362,18 @@ private function wrapFunctionCallWithReturnValue($returnVariable, $actor, $actio
13001362
return $this->resolveTestVariable($output, $args);
13011363
}
13021364
// @codingStandardsIgnoreEnd
1365+
1366+
/**
1367+
* Validates parameter array format, making sure user has enclosed string with square brackets.
1368+
*
1369+
* @param string $paramArray
1370+
* @return void
1371+
* @throws TestReferenceException
1372+
*/
1373+
private function validateParameterArray($paramArray)
1374+
{
1375+
if (substr($paramArray, 0, 1) != "[" || substr($paramArray, strlen($paramArray)-1, 1)!= "]") {
1376+
throw new TestReferenceException("parameterArray must begin with `[` and end with `]");
1377+
}
1378+
}
13031379
}

0 commit comments

Comments
 (0)