Skip to content

Commit f6eb3ab

Browse files
committed
MQE-1234: Allow XML Parser to read XML comment into comment action
- bugfix + new verification tests
1 parent 20ae1d7 commit f6eb3ab

File tree

7 files changed

+239
-19
lines changed

7 files changed

+239
-19
lines changed

dev/tests/verification/Resources/PersistenceCustomFieldsTest.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class PersistenceCustomFieldsTestCest
2525
public function _before(AcceptanceTester $I)
2626
{
2727
$createData1Fields['firstname'] = "Mac";
28-
// stepKey: createData1Firstname
28+
2929
$createData1Fields['lastname'] = "Doe";
30-
// stepKey: createData1Lastname
30+
3131
$I->comment("[createData1] create 'DefaultPerson' entity");
3232
PersistedObjectHandler::getInstance()->createEntity(
3333
"createData1",
@@ -38,7 +38,7 @@ class PersistenceCustomFieldsTestCest
3838
);
3939

4040
$createData2Fields['firstname'] = PersistedObjectHandler::getInstance()->retrieveEntityField('createData1', 'firstname', 'hook');
41-
// stepKey: createData2Firstname
41+
4242
$I->comment("[createData2] create 'uniqueData' entity");
4343
PersistedObjectHandler::getInstance()->createEntity(
4444
"createData2",
@@ -60,9 +60,9 @@ class PersistenceCustomFieldsTestCest
6060
public function PersistenceCustomFieldsTest(AcceptanceTester $I)
6161
{
6262
$createdDataFields['favoriteIndex'] = "1";
63-
// stepKey: createdDataFavoriteIndex
63+
6464
$createdDataFields['middlename'] = "Kovacs";
65-
// stepKey: createdDataMiddlename
65+
6666
$I->comment("[createdData] create 'simpleData' entity");
6767
PersistedObjectHandler::getInstance()->createEntity(
6868
"createdData",
@@ -73,9 +73,9 @@ class PersistenceCustomFieldsTestCest
7373
);
7474

7575
$createdData3Fields['firstname'] = "Takeshi";
76-
// stepKey: createdData3Firstname
76+
7777
$createdData3Fields['lastname'] = "Kovacs";
78-
// stepKey: createdData3Lastname
78+
7979
$I->comment("[createdData3] create 'UniquePerson' entity");
8080
PersistedObjectHandler::getInstance()->createEntity(
8181
"createdData3",
@@ -87,7 +87,7 @@ class PersistenceCustomFieldsTestCest
8787

8888
$I->comment("Entering Action Group [createdAG] PersistenceActionGroup");
8989
$createDataAG1CreatedAGFields['firstname'] = "string1";
90-
// stepKey: createDataAG1FirstnameCreatedAG
90+
9191
$I->comment("[createDataAG1CreatedAG] create 'simpleData' entity");
9292
PersistedObjectHandler::getInstance()->createEntity(
9393
"createDataAG1CreatedAG",
@@ -98,7 +98,7 @@ class PersistenceCustomFieldsTestCest
9898
);
9999

100100
$createDataAG2CreatedAGFields['firstname'] = "Jane";
101-
// stepKey: createDataAG2FirstnameCreatedAG
101+
102102
$I->comment("[createDataAG2CreatedAG] create 'simpleData' entity");
103103
PersistedObjectHandler::getInstance()->createEntity(
104104
"createDataAG2CreatedAG",
@@ -109,7 +109,7 @@ class PersistenceCustomFieldsTestCest
109109
);
110110

111111
$createDataAG3CreatedAGFields['firstname'] = PersistedObjectHandler::getInstance()->retrieveEntityField('createdData3', 'firstname', 'test');
112-
// stepKey: createDataAG3FirstnameCreatedAG
112+
113113
$I->comment("[createDataAG3CreatedAG] create 'simpleData' entity");
114114
PersistedObjectHandler::getInstance()->createEntity(
115115
"createDataAG3CreatedAG",

dev/tests/verification/Resources/functionalSuiteHooks.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ class functionalSuiteHooks extends \Codeception\GroupObject
5151

5252
// initialize the webdriver session
5353
$webDriver->_initializeSession();
54-
$webDriver->amOnPage("some.url");
54+
$webDriver->amOnPage("some.url"); // stepKey: before
5555
$createFields['someKey'] = "dataHere";
5656
PersistedObjectHandler::getInstance()->createEntity(
5757
"create",
5858
"suite",
5959
"createThis",
6060
$createFields
6161
);
62-
$webDriver->click(PersistedObjectHandler::getInstance()->retrieveEntityField('create', 'data', 'suite'));
62+
$webDriver->click(PersistedObjectHandler::getInstance()->retrieveEntityField('create', 'data', 'suite')); // stepKey: clickWithData
6363
print("Entering Action Group [AC] actionGroupWithTwoArguments");
64-
$webDriver->see("John", msq("uniqueData") . "John");
64+
$webDriver->see("John", msq("uniqueData") . "John"); // stepKey: seeFirstNameAC
6565
print("Exiting Action Group [AC] actionGroupWithTwoArguments");
6666

6767
// reset configuration and close session
@@ -120,10 +120,10 @@ class functionalSuiteHooks extends \Codeception\GroupObject
120120

121121
// initialize the webdriver session
122122
$webDriver->_initializeSession();
123-
$webDriver->amOnPage("some.url");
123+
$webDriver->amOnPage("some.url"); // stepKey: after
124124
$webDriver->deleteEntityByUrl("deleteThis");
125125
print("Entering Action Group [AC] actionGroupWithTwoArguments");
126-
$webDriver->see("John", msq("uniqueData") . "John");
126+
$webDriver->see("John", msq("uniqueData") . "John"); // stepKey: seeFirstNameAC
127127
print("Exiting Action Group [AC] actionGroupWithTwoArguments");
128128
} catch (\Exception $exception) {
129129
print $exception->getMessage();
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Group;
4+
5+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
6+
7+
/**
8+
* Group class is Codeception Extension which is allowed to handle to all internal events.
9+
* This class itself can be used to listen events for test execution of one particular group.
10+
* It may be especially useful to create fixtures data, prepare server, etc.
11+
*
12+
* INSTALLATION:
13+
*
14+
* To use this group extension, include it to "extensions" option of global Codeception config.
15+
*/
16+
class functionalSuiteWithComments extends \Codeception\GroupObject
17+
{
18+
public static $group = 'functionalSuiteWithComments';
19+
private $testCount = 1;
20+
private $preconditionFailure = null;
21+
private $currentTestRun = 0;
22+
private static $HOOK_EXECUTION_INIT = "\n/******** Beginning execution of functionalSuiteWithComments suite %s block ********/\n";
23+
private static $HOOK_EXECUTION_END = "\n/******** Execution of functionalSuiteWithComments suite %s block complete ********/\n";
24+
25+
public function _before(\Codeception\Event\TestEvent $e)
26+
{
27+
// increment test count per execution
28+
$this->currentTestRun++;
29+
$this->executePreConditions();
30+
31+
if ($this->preconditionFailure != null) {
32+
//if our preconditions fail, we need to mark all the tests as incomplete.
33+
$e->getTest()->getMetadata()->setIncomplete("SUITE PRECONDITION FAILED:" . PHP_EOL . $this->preconditionFailure);
34+
}
35+
}
36+
37+
38+
private function executePreConditions()
39+
{
40+
if ($this->currentTestRun == 1) {
41+
print sprintf(self::$HOOK_EXECUTION_INIT, "before");
42+
43+
try {
44+
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
45+
46+
// close any open sessions
47+
if ($webDriver->webDriver != null) {
48+
$webDriver->webDriver->close();
49+
$webDriver->webDriver = null;
50+
}
51+
52+
// initialize the webdriver session
53+
$webDriver->_initializeSession();
54+
print("Comment in Before");
55+
$webDriver->amOnPage("some.url"); // stepKey: before
56+
$createFields['someKey'] = "dataHere";
57+
PersistedObjectHandler::getInstance()->createEntity(
58+
"create",
59+
"suite",
60+
"createThis",
61+
$createFields
62+
);
63+
print("<click stepKey=\"comment with element\" userInput=\"helloworld\"/>");
64+
$webDriver->click(PersistedObjectHandler::getInstance()->retrieveEntityField('create', 'data', 'suite')); // stepKey: clickWithData
65+
print("Entering Action Group [AC] actionGroupWithTwoArguments");
66+
$webDriver->see("John", msq("uniqueData") . "John"); // stepKey: seeFirstNameAC
67+
print("Exiting Action Group [AC] actionGroupWithTwoArguments");
68+
69+
// reset configuration and close session
70+
$this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver')->_resetConfig();
71+
$webDriver->webDriver->close();
72+
$webDriver->webDriver = null;
73+
74+
} catch (\Exception $exception) {
75+
$this->preconditionFailure = $exception->getMessage();
76+
}
77+
78+
print sprintf(self::$HOOK_EXECUTION_END, "before");
79+
}
80+
}
81+
82+
public function _after(\Codeception\Event\TestEvent $e)
83+
{
84+
$this->executePostConditions($e);
85+
}
86+
87+
88+
private function executePostConditions(\Codeception\Event\TestEvent $e)
89+
{
90+
if ($this->currentTestRun == $this->testCount) {
91+
print sprintf(self::$HOOK_EXECUTION_INIT, "after");
92+
93+
try {
94+
// Find out if Test in Suite failed, will cause potential failures in suite after
95+
$cest = $e->getTest();
96+
97+
//Access private TestResultObject to find stack and if there are any errors (as opposed to failures)
98+
$testResultObject = call_user_func(\Closure::bind(
99+
function () use ($cest) {
100+
return $cest->getTestResultObject();
101+
},
102+
$cest
103+
));
104+
$errors = $testResultObject->errors();
105+
106+
if (!empty($errors)) {
107+
foreach ($errors as $error) {
108+
if ($error->failedTest()->getTestMethod() == $cest->getName()) {
109+
// Do not attempt to run _after if failure was in the _after block
110+
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
111+
print("LAST TEST IN SUITE FAILED, TEST AFTER MAY NOT BE SUCCESSFUL\n");
112+
}
113+
}
114+
}
115+
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
116+
117+
// close any open sessions
118+
if ($webDriver->webDriver != null) {
119+
$webDriver->webDriver->close();
120+
$webDriver->webDriver = null;
121+
}
122+
123+
// initialize the webdriver session
124+
$webDriver->_initializeSession();
125+
print("afterBlock");
126+
} catch (\Exception $exception) {
127+
print $exception->getMessage();
128+
}
129+
130+
PersistedObjectHandler::getInstance()->clearSuiteObjects();
131+
print sprintf(self::$HOOK_EXECUTION_END, "after");
132+
}
133+
}
134+
}

dev/tests/verification/Tests/SuiteGenerationTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,65 @@ public function testSuiteGenerationWithExtends()
327327
}
328328
}
329329

330+
/**
331+
* Test comments generated during suite generation
332+
*/
333+
public function testSuiteCommentsGeneration()
334+
{
335+
$groupName = 'functionalSuiteWithComments';
336+
337+
$expectedContents = [
338+
'IncludeTestCest.php'
339+
];
340+
341+
// Generate the Suite
342+
SuiteGenerator::getInstance()->generateSuite($groupName);
343+
344+
// Validate log message and add group name for later deletion
345+
TestLoggingUtil::getInstance()->validateMockLogStatement(
346+
'info',
347+
"suite generated",
348+
['suite' => $groupName, 'relative_path' => "_generated" . DIRECTORY_SEPARATOR . $groupName]
349+
);
350+
self::$TEST_GROUPS[] = $groupName;
351+
352+
// Validate Yaml file updated
353+
$yml = Yaml::parse(file_get_contents(self::CONFIG_YML_FILE));
354+
$this->assertArrayHasKey($groupName, $yml['groups']);
355+
356+
$suiteResultBaseDir = self::GENERATE_RESULT_DIR .
357+
DIRECTORY_SEPARATOR .
358+
$groupName .
359+
DIRECTORY_SEPARATOR;
360+
361+
// Validate tests have been generated
362+
$dirContents = array_diff(scandir($suiteResultBaseDir), ['..', '.']);
363+
364+
foreach ($expectedContents as $expectedFile) {
365+
$this->assertTrue(in_array($expectedFile, $dirContents));
366+
}
367+
368+
//assert group file created and contains correct contents
369+
$groupFile = PROJECT_ROOT .
370+
DIRECTORY_SEPARATOR .
371+
"src" .
372+
DIRECTORY_SEPARATOR .
373+
"Magento" .
374+
DIRECTORY_SEPARATOR .
375+
"FunctionalTestingFramework" .
376+
DIRECTORY_SEPARATOR .
377+
"Group" .
378+
DIRECTORY_SEPARATOR .
379+
$groupName .
380+
".php";
381+
382+
$this->assertTrue(file_exists($groupFile));
383+
$this->assertFileEquals(
384+
self::RESOURCES_PATH . DIRECTORY_SEPARATOR . $groupName . ".txt",
385+
$groupFile
386+
);
387+
}
388+
330389
/**
331390
* revert any changes made to config.yml
332391
* remove _generated directory

dev/tests/verification/_suite/functionalSuite.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,28 @@
2929
<test name="ExcludeTest2"/>
3030
</exclude>
3131
</suite>
32+
<suite name="functionalSuiteWithComments">
33+
<include>
34+
<!-- Comment Block-->
35+
<test name="IncludeTest"/>
36+
</include>
37+
<before>
38+
<!-- Comment in Before-->
39+
<amOnPage url="some.url" stepKey="before"/>
40+
<createData entity="createThis" stepKey="create">
41+
<!--Comment in Nested Element-->
42+
<field key="someKey">dataHere</field>
43+
</createData>
44+
<!-- <click stepKey="comment with element" userInput="helloworld"/> -->
45+
<click stepKey="clickWithData" userInput="$create.data$"/>
46+
<actionGroup ref="actionGroupWithTwoArguments" stepKey="AC">
47+
<!--Comment in AG Args-->
48+
<argument name="somePerson" value="simpleData"/>
49+
<argument name="anotherPerson" value="uniqueData"/>
50+
</actionGroup>
51+
</before>
52+
<after>
53+
<comment userInput="afterBlock" stepKey="afterBlock"/>
54+
</after>
55+
</suite>
3256
</suites>

src/Magento/FunctionalTestingFramework/Suite/Generators/GroupClassGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,11 @@ private function buildHookMustacheArray($hookObj)
172172
private function buildWebDriverActionsMustacheArray($action, $actionEntries)
173173
{
174174
$step = TestGenerator::getInstance()->generateStepsPhp([$action], TestGenerator::SUITE_SCOPE, 'webDriver');
175-
$rawPhp = str_replace(["\t", "\n"], "", $step);
176-
$multipleCommands = explode(";", $rawPhp, -1);
175+
$rawPhp = str_replace(["\t"], "", $step);
176+
$multipleCommands = explode(PHP_EOL, $rawPhp, -1);
177+
$multipleCommands = array_filter($multipleCommands);
177178
foreach ($multipleCommands as $command) {
178-
$actionEntries = $this->replaceReservedTesterFunctions($command . ";", $actionEntries, 'webDriver');
179+
$actionEntries = $this->replaceReservedTesterFunctions($command . PHP_EOL, $actionEntries, 'webDriver');
179180
}
180181

181182
return $actionEntries;
@@ -192,6 +193,7 @@ private function buildWebDriverActionsMustacheArray($action, $actionEntries)
192193
*/
193194
private function replaceReservedTesterFunctions($formattedStep, $actionEntries, $actor)
194195
{
196+
$formattedStep = rtrim($formattedStep);
195197
foreach (self::REPLACEMENT_ACTIONS as $testAction => $replacement) {
196198
$testActionCall = "\${$actor}->{$testAction}";
197199
if (substr($formattedStep, 0, strlen($testActionCall)) == $testActionCall) {

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class TestGenerator
4848
'updateData',
4949
'getData',
5050
'magentoCLI',
51-
'generateDate'
51+
'generateDate',
52+
'field'
5253
];
5354
const STEP_KEY_ANNOTATION = " // stepKey: %s";
5455

0 commit comments

Comments
 (0)