Skip to content

Commit 173f9b3

Browse files
committed
MQE-683: [Deprecation] Only use more nested assertion syntax
- Heavy updates to script - Update to AssertElementContainsAttribute action
1 parent 35818bb commit 173f9b3

File tree

4 files changed

+71
-32
lines changed

4 files changed

+71
-32
lines changed

src/Magento/FunctionalTestingFramework/Test/Objects/ActionObject.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ActionObject
5959
const ASSERTION_ATTRIBUTES = ["expectedResult" => "expected", "actualResult" => "actual"];
6060
const ASSERTION_TYPE_ATTRIBUTE = "type";
6161
const ASSERTION_VALUE_ATTRIBUTE = "value";
62+
const ASSERTION_ELEMENT_ATTRIBUTES = ["selector", "attribute"];
6263
const DELETE_DATA_MUTUAL_EXCLUSIVE_ATTRIBUTES = ["url", "createDataKey"];
6364
const EXTERNAL_URL_AREA_INVALID_ACTIONS = ['amOnPage'];
6465
const FUNCTION_CLOSURE_ACTIONS = ['waitForElementChange'];
@@ -310,11 +311,17 @@ public function trimAssertionAttributes()
310311
}
311312

312313
// Flatten nested Elements's type and value into key=>value entries
314+
// Also, add selector/value attributes if they are present in nested Element
313315
foreach ($this->actionAttributes as $key => $subAttributes) {
316+
foreach (self::ASSERTION_ELEMENT_ATTRIBUTES as $ATTRIBUTE) {
317+
if (isset($subAttributes[$ATTRIBUTE])) {
318+
$this->actionAttributes[$ATTRIBUTE] = $subAttributes[$ATTRIBUTE];
319+
}
320+
}
314321
if (in_array($key, $relevantKeys)) {
315322
$prefix = ActionObject::ASSERTION_ATTRIBUTES[$key];
316323
$this->actionAttributes[$prefix . ucfirst(ActionObject::ASSERTION_TYPE_ATTRIBUTE)] =
317-
$subAttributes[ActionObject::ASSERTION_TYPE_ATTRIBUTE];
324+
$subAttributes[ActionObject::ASSERTION_TYPE_ATTRIBUTE] ?? "NO_TYPE";
318325
$this->actionAttributes[$prefix] =
319326
$subAttributes[ActionObject::ASSERTION_VALUE_ATTRIBUTE];
320327
unset($this->actionAttributes[$key]);

src/Magento/FunctionalTestingFramework/Test/etc/Actions/assertActions.xsd

+14-10
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
<!-- Complex Types -->
9393

9494
<!-- ASSERTION TYPES -->
95-
<!-- REMOVE expected/expectedType and actual/actualType in MQE-683-->
9695
<xs:complexType name="assertionType">
9796
<xs:choice maxOccurs="unbounded">
9897
<xs:element name="expectedResult" type="expectedResultType" minOccurs="0"/>
@@ -111,7 +110,7 @@
111110
</xs:documentation>
112111
</xs:annotation>
113112
<xs:choice maxOccurs="unbounded">
114-
<xs:element name="expectedResult" type="expectedResultType" minOccurs="0"/>
113+
<xs:element name="expectedResult" type="expectedElementContainsType" minOccurs="0"/>
115114
</xs:choice>
116115
<xs:attribute type="xs:string" name="expectedValue">
117116
<xs:annotation>
@@ -120,14 +119,6 @@
120119
</xs:documentation>
121120
</xs:annotation>
122121
</xs:attribute>
123-
<xs:attribute name="selector" use="required"/>
124-
<xs:attribute type="xs:string" name="attribute" use="required">
125-
<xs:annotation>
126-
<xs:documentation>
127-
Attribute in given element to be assert against.
128-
</xs:documentation>
129-
</xs:annotation>
130-
</xs:attribute>
131122
<xs:attributeGroup ref="commonActionAttributes"/>
132123
</xs:complexType>
133124

@@ -599,6 +590,19 @@
599590
</xs:extension>
600591
</xs:simpleContent>
601592
</xs:complexType>
593+
<xs:complexType name="expectedElementContainsType">
594+
<xs:annotation>
595+
<xs:documentation>
596+
Element containing the Expected value and selector/attributeName.
597+
</xs:documentation>
598+
</xs:annotation>
599+
<xs:simpleContent>
600+
<xs:extension base="xs:string">
601+
<xs:attribute type="xs:string" name="selector" use="required"/>
602+
<xs:attribute type="xs:string" name="attribute" use="required"/>
603+
</xs:extension>
604+
</xs:simpleContent>
605+
</xs:complexType>
602606
<xs:complexType name="actualResultType">
603607
<xs:annotation>
604608
<xs:documentation>

src/Magento/FunctionalTestingFramework/Upgrade/UpdateAssertionSchema.php

+48-17
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public function execute(InputInterface $input)
5454
preg_match_all('/<assert[^>]*\/>/', $contents, $potentialAssertions);
5555
$newAssertions = [];
5656
$index = 0;
57+
if (empty($potentialAssertions[0])) {
58+
continue;
59+
}
5760
foreach ($potentialAssertions[0] as $potentialAssertion) {
5861
$newAssertions[$index] = $this->convertOldAssertionToNew($potentialAssertion);
5962
$index++;
@@ -98,8 +101,7 @@ private function convertOldAssertionToNew($assertion)
98101
$stepKey = "";
99102

100103
// regex to grab values
101-
$grabValueRegex = '/(stepKey|actual|actualType|expected|expectedType|delta|message)="([^"]*)"/';
102-
104+
$grabValueRegex = '/(stepKey|actual|actualType|expected|expectedType|delta|message|selector|attribute|expectedValue|before|after|remove)=(\'[^\']*\'|"[^"]*")/';
103105
// Make 3 arrays in $grabbedParts:
104106
// 0 contains stepKey="value"
105107
// 1 contains stepKey
@@ -110,40 +112,69 @@ private function convertOldAssertionToNew($assertion)
110112
$sortedParts[$grabbedParts[1][$i]] = $grabbedParts[2][$i];
111113
}
112114

113-
// Build new String
114-
$newString = "<$assertType ";
115+
// Build new String, trim ' and "
116+
$trimmedParts = [];
117+
$newString = "<$assertType";
115118
$subElements = ["actual" => [], "expected" => []];
116119
foreach ($sortedParts as $type => $value) {
117-
if (in_array($type, ["stepKey", "delta", "message"])) {
120+
$value = rtrim(ltrim($value, '"'), '"');
121+
$value = rtrim(ltrim($value, "'"), "'");
122+
$trimmedParts[$type] = $value;
123+
if (in_array($type, ["stepKey", "delta", "message", "before", "after", "remove"])) {
118124
if ($type == "stepKey") {
119125
$stepKey = $value;
120126
}
121-
$newString .= "$type=\"$value\"";
127+
$newString .= " $type=\"$value\"";
122128
continue;
123129
}
124130
if ($type == "actual") {
125131
$subElements["actual"]["value"] = $value;
126132
} elseif ($type == "actualType") {
127133
$subElements["actual"]["type"] = $value;
128-
} elseif ($type == "expected") {
134+
} elseif ($type == "expected" || $type = "expectedValue") {
129135
$subElements["expected"]["value"] = $value;
130136
} elseif ($type == "expectedType") {
131137
$subElements["expected"]["type"] = $value;
132138
}
133139
}
134140
$newString .= ">\n";
135-
foreach ($subElements as $type => $subElement) {
136-
if (!isset($subElement['value']) || !isset($subElement['type'])) {
137-
//don't have all the info we need to rebuild
138-
$this->errors[] = "UNABLE TO FULLY REBUILD ASSERTION, PLEASE MANUALLY CHECK FORMAT " .
139-
"($assertType \"$stepKey\" in $this->currentFile)";
140-
continue;
141+
// Guess value type if not set in either case
142+
if (!isset($subElements["actual"]['type']) && isset($subElements["actual"]["value"])) {
143+
$subElements["actual"]['type'] = $this->guessValueType($subElements["actual"]["value"]);
144+
}
145+
if (!isset($subElements["expected"]['type']) && isset($subElements["expected"]["value"])) {
146+
$subElements["expected"]['type'] = $this->guessValueType($subElements["expected"]["value"]);
147+
}
148+
// Massage subElements with data for edge cases
149+
if ($assertType == 'assertElementContainsAttribute') {
150+
// Assert type is very edge-cased, completely different schema
151+
$value = $subElements['expected']['value'];
152+
$selector = $trimmedParts['selector'];
153+
$attribute = $trimmedParts['attribute'];
154+
$newString .= "\t\t\t<expectedResult selector=\"$selector\" attribute=\"$attribute\">$value</expectedResult>\n";
155+
} else {
156+
foreach ($subElements as $type => $subElement) {
157+
if (empty($subElement)) {
158+
continue;
159+
}
160+
$value = $subElement['value'];
161+
$typeValue = $subElement['type'];
162+
if (empty($value)) {
163+
$this->errors[] = "POTENTIAL ANOMALOUS OUPUT DETECTED, PLEASE MANUALLY CHECK OUTPUT " .
164+
"($assertType \"$stepKey\" in $this->currentFile)";
165+
}
166+
$newString .= "\t\t\t<{$type}Result type=\"$typeValue\">$value</{$type}Result>\n";
141167
}
142-
$value = $subElement['value'];
143-
$typeValue = $subElement['type'];
144-
$newString .= "<{$type}Result type=\"$typeValue\">$value</{$type}Result>\n";
145168
}
146-
$newString .= "</$assertType>";
169+
$newString .= " </$assertType>";
147170
return $newString;
148171
}
172+
173+
private function guessValueType($string) {
174+
preg_match('/\$[a-zA-Z0-9]*/', $string, $matches);
175+
if (isset($matches[0]) && $matches[0] == $string) {
176+
return "variable";
177+
}
178+
return "string";
179+
}
149180
}

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,6 @@ public function generateStepsPhp($actionObjects, $generationScope = TestGenerato
617617
} elseif (isset($customActionAttributes['url'])) {
618618
$input = $this->addUniquenessFunctionCall($customActionAttributes['url']);
619619
$url = $this->addUniquenessFunctionCall($customActionAttributes['url']);
620-
} elseif (isset($customActionAttributes['expectedValue'])) {
621-
//For old Assert backwards Compatibility, remove when deprecating
622-
$assertExpected = $this->addUniquenessFunctionCall($customActionAttributes['expectedValue']);
623620
} elseif (isset($customActionAttributes['regex'])) {
624621
$input = $this->addUniquenessFunctionCall($customActionAttributes['regex']);
625622
}
@@ -1234,7 +1231,7 @@ public function generateStepsPhp($actionObjects, $generationScope = TestGenerato
12341231
$actionObject,
12351232
$selector,
12361233
$this->wrapWithDoubleQuotes($attribute),
1237-
$assertExpected
1234+
$this->wrapWithDoubleQuotes($assertExpected)
12381235
);
12391236
break;
12401237
case "assertEmpty":

0 commit comments

Comments
 (0)