Skip to content

Commit

Permalink
reporting fixes and enhacements
Browse files Browse the repository at this point in the history
.Assertions: adding expected and actual JSON files attachments
.pom: version bump
.ReportManager: fixing issue with environment file generation
.RestActions: exposing parseBodyToJson to be used by assertions and
verifications
.Verificaitons: adding expected and actual JSON files attachments
  • Loading branch information
MohabMohie committed Oct 15, 2019
1 parent eb288bc commit 5b9f8ff
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.mohabmohie</groupId>
<artifactId>SHAFT_ENGINE</artifactId>
<version>3.1.20191007</version>
<version>3.1.20191015</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Selenium Hybrid Automation Framework for Testing [SHAFT]</description>
<url>https://github.com/MohabMohie/SHAFT_ENGINE</url>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/shaft/api/RestActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,11 @@ private static int identifyBodyObjectType(Object body) {
}
}

private static InputStream parseBodyToJson(Response response) {
public static InputStream parseBodyToJson(Response response) {
return parseBodyToJson(response.getBody());
}

private static InputStream parseBodyToJson(Object body) {
public static InputStream parseBodyToJson(Object body) {
JSONParser parser = new JSONParser();
try {
org.json.simple.JSONObject actualJsonObject = null;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/shaft/tools/io/ReportManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ private static void writeEnvironmentVariablesToAllureResultsDirectory() {
String propertyKey = ((String) (props.keySet().toArray())[i]).trim();
String propertyValue = props.getProperty(propertyKey).trim();

// excluding empty values and system properties (all system properties have "."
// in their names
if (!propertyValue.equals("") && !propertyKey.contains(".")) {
// excluding empty values, system properties (all system properties have "." in
// their names), and any git branch issues
if (!propertyValue.equals("") && !propertyKey.contains(".") && !propertyKey.contains(">>>")
&& !propertyKey.contains("<<<")) {
String parameter = "<parameter>" + "<key>" + propertyKey + "</key>" + "<value>" + propertyValue
+ "</value>" + "</parameter>";
if (propertyKey.equals(SHAFT_ENGINE_VERSION_PROPERTY_NAME)) {
Expand Down
101 changes: 66 additions & 35 deletions src/main/java/com/shaft/validation/Assertions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.shaft.validation;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -24,6 +26,8 @@

//TODO: Add optional message to be added to the log of the assertion to describe what it does

//TODO: Add attachments for JSON assertions in case of pass or fail both the expected and the actual

public class Assertions {
private static int attemptsBeforeThrowingElementNotFoundException = Integer
.parseInt(System.getProperty("attemptsBeforeThrowingElementNotFoundException").trim());
Expand Down Expand Up @@ -156,13 +160,17 @@ private static void pass(String actionName, WebDriver driver, By elementLocator,
* {@link Assertions#assertEquals(Object , Object , AssertionComparisonType , AssertionType)}
* instead.
*
* @param expectedValue the expected value (test data) of this assertion
* @param actualValue the actual value (calculated data) of this assertion
* @param assertionComparisonType 1 is literalComparison, 2 is regexComparison, 3 is
* containsComparison, 4 is caseInsensitiveComparison
* @param assertionType either 'true' for a positive assertion that the objects
* are equal, or 'false' for a negative assertion that the
* objects are not equal
* @param expectedValue the expected value (test data) of this
* assertion
* @param actualValue the actual value (calculated data) of this
* assertion
* @param assertionComparisonType 1 is literalComparison, 2 is regexComparison,
* 3 is containsComparison, 4 is
* caseInsensitiveComparison
* @param assertionType either 'true' for a positive assertion that
* the objects are equal, or 'false' for a
* negative assertion that the objects are not
* equal
*/
public static void assertEquals(Object expectedValue, Object actualValue, int assertionComparisonType,
Boolean assertionType) {
Expand Down Expand Up @@ -369,18 +377,21 @@ public static void assertElementExists(WebDriver driver, By elementLocator, Asse
* {@link Assertions#assertElementAttribute(WebDriver , By , String , String , AssertionComparisonType , AssertionType)}
* instead.
*
* @param driver the current instance of Selenium webdriver
* @param elementLocator the locator of the webElement under test (By xpath,
* id, selector, name ...etc)
* @param elementAttribute the desired attribute of the webElement under test
* @param expectedValue the expected value (test data) of this assertion
* @param assertionComparisonType 1 is literalComparison, 2 is regexComparison, 3 is
* containsComparison, 4 is caseInsensitiveComparison
* @param assertionType either 'true' for a positive assertion that the
* element attribute actual value matches the expected
* value, or 'false' for a negative assertion that the
* element attribute actual value doesn't match the
* expected value
* @param driver the current instance of Selenium webdriver
* @param elementLocator the locator of the webElement under test (By
* xpath, id, selector, name ...etc)
* @param elementAttribute the desired attribute of the webElement under
* test
* @param expectedValue the expected value (test data) of this
* assertion
* @param assertionComparisonType 1 is literalComparison, 2 is regexComparison,
* 3 is containsComparison, 4 is
* caseInsensitiveComparison
* @param assertionType either 'true' for a positive assertion that
* the element attribute actual value matches the
* expected value, or 'false' for a negative
* assertion that the element attribute actual
* value doesn't match the expected value
*/
public static void assertElementAttribute(WebDriver driver, By elementLocator, String elementAttribute,
String expectedValue, int assertionComparisonType, Boolean assertionType) {
Expand Down Expand Up @@ -556,17 +567,19 @@ public static void assertElementCSSProperty(WebDriver driver, By elementLocator,
* {@link Assertions#assertBrowserAttribute(WebDriver , String , String , AssertionComparisonType , AssertionType )}
* instead.
*
* @param driver the current instance of Selenium webdriver
* @param browserAttribute the desired attribute of the browser window under
* test
* @param expectedValue the expected value (test data) of this assertion
* @param assertionComparisonType 1 is literalComparison, 2 is regexComparison, 3 is
* containsComparison, 4 is caseInsensitiveComparison
* @param assertionType either 'true' for a positive assertion that the
* browser attribute actual value matches the expected
* value, or 'false' for a negative assertion that the
* browser attribute actual value doesn't match the
* expected value
* @param driver the current instance of Selenium webdriver
* @param browserAttribute the desired attribute of the browser window
* under test
* @param expectedValue the expected value (test data) of this
* assertion
* @param assertionComparisonType 1 is literalComparison, 2 is regexComparison,
* 3 is containsComparison, 4 is
* caseInsensitiveComparison
* @param assertionType either 'true' for a positive assertion that
* the browser attribute actual value matches the
* expected value, or 'false' for a negative
* assertion that the browser attribute actual
* value doesn't match the expected value
*/
public static void assertBrowserAttribute(WebDriver driver, String browserAttribute, String expectedValue,
int assertionComparisonType, Boolean assertionType) {
Expand Down Expand Up @@ -936,27 +949,45 @@ public static void assertJSONFileContent(Response response, String referenceJson
+ referenceJsonFilePath + "], jsonPathToTargetArray [" + jsonPathToTargetArray
+ "], comparisonType [" + comparisonType + "], and assertionType [" + assertionType + "].");
}

Boolean comparisonResult = RestActions.compareJSON(response, referenceJsonFilePath, comparisonType,
jsonPathToTargetArray);
if (comparisonResult) {

// prepare attachments
List<Object> expectedValueAttachment = null;
try {
expectedValueAttachment = Arrays.asList("Validation Test Data", "Expected Value",
RestActions.parseBodyToJson(new FileReader(referenceJsonFilePath)));
} catch (FileNotFoundException e) {
// do nothing because the test would have already failed at the compareJSON
// stage
}
List<Object> actualValueAttachment = Arrays.asList("Validation Test Data", "Actual Value",
RestActions.parseBodyToJson(response));

List<List<Object>> attachments = new ArrayList<>();
attachments.add(expectedValueAttachment);
attachments.add(actualValueAttachment);

if (Boolean.TRUE.equals(comparisonResult)) {
if (assertionType.getValue()) {
// comparison passed and is expected to pass
pass("Assertion Passed; the actual API response does match the expected JSON file at this path \""
+ referenceJsonFilePath + "\".");
+ referenceJsonFilePath + "\".", attachments);
} else {
// comparison passed and is expected to fail
fail("Assertion Failed; the actual API response does match the expected JSON file at this path \""
+ referenceJsonFilePath + "\".");
+ referenceJsonFilePath + "\".", attachments);
}
} else {
if (assertionType.getValue()) {
// comparison failed and is expected to pass
fail("Assertion Failed; the actual API response does not match the expected JSON file at this path \""
+ referenceJsonFilePath + "\".");
+ referenceJsonFilePath + "\".", attachments);
} else {
// comparison failed and is expected to fail
pass("Assertion Passed; the actual API response does not match the expected JSON file at this path \""
+ referenceJsonFilePath + "\".");
+ referenceJsonFilePath + "\".", attachments);
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/shaft/validation/Verifications.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.shaft.validation;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -982,7 +984,24 @@ public static void verifyJSONFileContent(Response response, String referenceJson
}
Boolean comparisonResult = RestActions.compareJSON(response, referenceJsonFilePath, comparisonType,
jsonPathToTargetArray);
if (comparisonResult) {

// prepare attachments
List<Object> expectedValueAttachment = null;
try {
expectedValueAttachment = Arrays.asList("Validation Test Data", "Expected Value",
RestActions.parseBodyToJson(new FileReader(referenceJsonFilePath)));
} catch (FileNotFoundException e) {
// do nothing because the test would have already failed at the compareJSON
// stage
}
List<Object> actualValueAttachment = Arrays.asList("Validation Test Data", "Actual Value",
RestActions.parseBodyToJson(response));

List<List<Object>> attachments = new ArrayList<>();
attachments.add(expectedValueAttachment);
attachments.add(actualValueAttachment);

if (Boolean.TRUE.equals(comparisonResult)) {
if (verificationType.getValue()) {
// comparison passed and is expected to pass
verificationSuccesses.append(
Expand All @@ -1007,7 +1026,7 @@ public static void verifyJSONFileContent(Response response, String referenceJson
+ referenceJsonFilePath + "\".");
}
}
reportVerificationResults("verifyJSONFileContent", null, null);
reportVerificationResults("verifyJSONFileContent", null, null, attachments);
}

}

0 comments on commit 5b9f8ff

Please sign in to comment.