Skip to content

Commit

Permalink
Merge pull request #1931
Browse files Browse the repository at this point in the history
* only publish javadocs if a new release is successful

* fix expected exceptions

* migrate visual validations to new core
  • Loading branch information
MohabMohie authored Mar 10, 2025
1 parent 60f74dc commit 4efe6b3
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publishJavaDocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: JavaDocs Publisher
on:
workflow_run:
workflows: [ "Maven Central Continuous Delivery" ]
types: [ requested ]
types: [ completed ]
branches:
- 'main'
workflow_dispatch:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.shaft.driver.internal.DriverFactory;

import com.shaft.driver.SHAFT;
import com.shaft.gui.internal.exceptions.MultipleElementsFoundException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Browser;
import org.openqa.selenium.support.ui.FluentWait;
Expand Down Expand Up @@ -35,6 +36,7 @@ private ArrayList<Class<? extends Exception>> getExpectedExceptions(boolean isVa
expectedExceptions.add(org.openqa.selenium.StaleElementReferenceException.class);
expectedExceptions.add(org.openqa.selenium.JavascriptException.class);
expectedExceptions.add(org.openqa.selenium.ElementClickInterceptedException.class);
expectedExceptions.add(MultipleElementsFoundException.class);

if (isValidToCheckForVisibility) {
expectedExceptions.add(org.openqa.selenium.ElementNotInteractableException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public static String getCallingClassFullName() {
StackTraceElement[] callingStack = Thread.currentThread().getStackTrace();
var getCallingClassFullName = new StringBuilder();
for (var i = 1; i < callingStack.length; i++) {
if (!callingStack[i].getClassName().contains("shaft")) {
if (!callingStack[i].getClassName().contains("shaft") && !callingStack[i].getClassName().equals("org.openqa.selenium.support.ui.FluentWait")) {
getCallingClassFullName.append(callingStack[i].getClassName());
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ private void performValidation() {
new ValidationsHelper().validateTrue(validationCategory, condition, validationType, customReportMessage);
case "elementExists" ->
new ValidationsHelper2(validationCategory).validateElementExists(driver.get(), locator, validationType);
case "elementMatches" ->
new ValidationsHelper().validateElementMatches(validationCategory, driver.get(), locator, visualValidationEngine, validationType, customReportMessage);
case "elementMatches" -> new ValidationsHelper2(validationCategory).validateElementMatches(driver.get(), locator, visualValidationEngine, validationType);
case "elementAttributeEquals" ->
new ValidationsHelper2(validationCategory).validateElementAttribute(driver.get(), locator, elementAttribute, String.valueOf(expectedValue), validationComparisonType, validationType);
case "elementPropertyEquals" ->
Expand Down
118 changes: 0 additions & 118 deletions src/main/java/com/shaft/validation/internal/ValidationsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
import com.shaft.driver.SHAFT;
import com.shaft.gui.browser.internal.BrowserActionsHelper;
import com.shaft.gui.element.internal.ElementActionsHelper;
import com.shaft.gui.internal.image.ImageProcessingActions;
import com.shaft.gui.internal.image.ScreenshotManager;
import com.shaft.properties.internal.Properties;
import com.shaft.tools.io.internal.FailureReporter;
import com.shaft.tools.io.internal.ReportManagerHelper;
import com.shaft.validation.ValidationEnums.*;
import io.restassured.response.Response;
import org.apache.logging.log4j.Level;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Browser;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -167,12 +163,6 @@ private static void pass(WebDriver driver, ValidationCategory validationCategory
ValidationState.PASSED, null, null);
}

private static void pass(ValidationCategory validationCategory, Number expectedValue, Number actualValue,
Object comparativeRelationType, ValidationType validationType) {
reportValidationState(null, validationCategory, String.valueOf(expectedValue), String.valueOf(actualValue), comparativeRelationType, validationType,
ValidationState.PASSED, null, null);
}

private static void fail(WebDriver driver, ValidationCategory validationCategory, String expectedValue, String actualValue,
Object validationComparisonType, ValidationType validationType, @SuppressWarnings("SameParameterValue") Throwable failureReason, List<List<Object>> externalAttachments) {
// reset state in case of failure to force reporting the failure
Expand All @@ -189,54 +179,6 @@ private static void fail(WebDriver driver, ValidationCategory validationCategory
ValidationState.FAILED, failureReason, null);
}

private static void fail(ValidationCategory validationCategory, Number expectedValue, Number actualValue,
Object comparativeRelationType, ValidationType validationType) {
// reset state in case of failure to force reporting the failure
ReportManagerHelper.setDiscreteLogging(discreetLoggingState);
reportValidationState(null, validationCategory, String.valueOf(expectedValue), String.valueOf(actualValue), comparativeRelationType, validationType,
ValidationState.FAILED, null, null);
}

private static void reportValidationResultOfBrowserAttribute(WebDriver driver, Object[] args) {
String[] expectedAttributeStates = (String[]) args[0];
String propertySeparator = (String) args[1];
String attributeClosure = (String) args[2];
int comparisonResult = (int) args[3];
String propertyName = (String) args[5];
String expectedValue = (String) args[6];
String actualValue = (String) args[7];
ValidationComparisonType validationComparisonType = (ValidationComparisonType) args[8];
ValidationType validationType = (ValidationType) args[9];
ValidationCategory validationCategory = (ValidationCategory) args[10];
if (validationType.getValue()) {
// expecting element attribute to have the correct value
if (comparisonResult == 1) {
// match
pass(driver, validationCategory, expectedAttributeStates[0] + " '" + expectedValue + propertySeparator + propertyName
+ attributeClosure, actualValue, validationComparisonType,
validationType);
} else {
// no match, or unhandled issue
fail(driver, validationCategory, expectedAttributeStates[0] + " '" + expectedValue + propertySeparator + propertyName
+ attributeClosure, actualValue, validationComparisonType,
validationType, null);
}
} else {
// expecting element attribute to not have the correct value
if (comparisonResult == 1) {
// match
pass(driver, validationCategory, expectedAttributeStates[1] + " '" + expectedValue + propertySeparator + propertyName
+ attributeClosure, actualValue, validationComparisonType,
validationType);
} else {
// no match, or unhandled issue
fail(driver, validationCategory, expectedAttributeStates[1] + " '" + expectedValue + propertySeparator + propertyName
+ attributeClosure, actualValue, validationComparisonType,
validationType, null);
}
}
}

static boolean isExpectedOrActualValueLong(String expectedValue, String actualValue) {
boolean isExpectedOrActualValueLong = false;
if (actualValue == null && expectedValue != null) {
Expand Down Expand Up @@ -363,64 +305,4 @@ protected void validateResponseFileSchema(ValidationCategory validationCategory,
fail(null, validationCategory, reportedExpectedValue.toString(), String.valueOf(comparisonResult).toUpperCase(), comparisonType, validationType, null, attachments);
}
}

protected void validateElementMatches(ValidationCategory validationCategory, WebDriver driver, By elementLocator, VisualValidationEngine visualValidationEngine, ValidationType validationType,
String customReportMessage) {
lastUsedElementLocator = elementLocator;
//TODO: remove this temporary fix when this bug is fixed with shutterbug
//https://github.com/assertthat/selenium-shutterbug/issues/105
if (Properties.web.targetBrowserName().equalsIgnoreCase(Browser.SAFARI.browserName())) {
visualValidationEngine = VisualValidationEngine.EXACT_OPENCV;
}
processCustomLogMessage(customReportMessage);
StringBuilder reportedExpectedResult = new StringBuilder();
reportedExpectedResult.append("Element should ");
Boolean expectedResult = validationType.getValue();
if (!expectedResult) {
reportedExpectedResult.append("not ");
}
reportedExpectedResult.append("match the reference screenshot");
List<List<Object>> attachments = new ArrayList<>();
byte[] referenceImage = ImageProcessingActions.getReferenceImage(elementLocator);
if (!Arrays.equals(new byte[0], referenceImage)) {
ReportManagerHelper.logDiscrete("Reference image found.", Level.INFO);
List<Object> expectedValueAttachment = Arrays.asList("Validation Test Data", "Reference Screenshot",
referenceImage);
attachments.add(expectedValueAttachment);
} else {
ReportManagerHelper.logDiscrete("Reference image not found, attempting to capture new reference.", Level.INFO);
}
if (elementActionsHelper.getElementsCount(driver, elementLocator) == 1) {
byte[] elementScreenshot;
Boolean actualResult;

elementScreenshot = new ScreenshotManager().takeElementScreenshot(driver, elementLocator);
actualResult = ImageProcessingActions.compareAgainstBaseline(driver, elementLocator, elementScreenshot, ImageProcessingActions.VisualValidationEngine.valueOf(visualValidationEngine.name()));

List<Object> actualValueAttachment = Arrays.asList("Validation Test Data", "Actual Screenshot",
elementScreenshot);
attachments.add(actualValueAttachment);

if (visualValidationEngine.equals(VisualValidationEngine.EXACT_SHUTTERBUG) && !actualResult) {
//if shutterbug and failed, get differences screenshot
byte[] shutterbugDifferencesImage = ImageProcessingActions.getShutterbugDifferencesImage(elementLocator);
if (!Arrays.equals(new byte[0], shutterbugDifferencesImage)) {
List<Object> differencesAttachment = Arrays.asList("Validation Test Data", "Differences",
shutterbugDifferencesImage);
attachments.add(differencesAttachment);
}
}
if (expectedResult.equals(actualResult)) {
pass(driver, validationCategory, reportedExpectedResult.toString(), String.valueOf(actualResult).toUpperCase(), visualValidationEngine, validationType, attachments);
} else {
fail(driver, validationCategory, reportedExpectedResult.toString(), String.valueOf(actualResult).toUpperCase(), visualValidationEngine, validationType, null, attachments);
}
} else {
byte[] pageScreenshot = new ScreenshotManager().takeScreenshot(driver, null);
List<Object> actualValueAttachment = Arrays.asList("Validation Test Data", "Actual Screenshot",
pageScreenshot);
attachments.add(actualValueAttachment);
fail(driver, validationCategory, reportedExpectedResult.toString(), "Element not found".toUpperCase(), visualValidationEngine, validationType, null, attachments);
}
}
}
Loading

0 comments on commit 4efe6b3

Please sign in to comment.