Best Galen code snippet using com.galenframework.validation.specs.SpecValidationImage.ImageCheck
Source:SpecValidationImage.java
...36public class SpecValidationImage extends SpecValidation<SpecImage> {37 private final static Logger LOG = LoggerFactory.getLogger(SpecValidationImage.class);38 private static final String NO_ERROR_MESSAGE = null;39 private static final ImageCompareResult NO_RESULT = null;40 private static class ImageCheck {41 private final String imagePath;42 private final double difference;43 private final ImageCompareResult result;44 private final String errorMessage;45 public ImageCheck(String imagePath, double difference, ImageCompareResult result, String errorMessage) {46 this.imagePath = imagePath;47 this.difference = difference;48 this.result = result;49 this.errorMessage = errorMessage;50 }51 }52 @Override53 public ValidationResult check(PageValidation pageValidation, String objectName, SpecImage spec) throws ValidationErrorException {54 PageElement pageElement = pageValidation.findPageElement(objectName);55 checkAvailability(pageElement, objectName);56 final BufferedImage pageImage = pageValidation.getPage().getScreenshotImage();57 int tolerance = GalenConfig.getConfig().getImageSpecDefaultTolerance();58 if (spec.getTolerance() != null && spec.getTolerance() >= 0) {59 tolerance = spec.getTolerance();60 }61 ComparisonOptions options = new ComparisonOptions();62 options.setIgnoreRegions(convertIgnoreObjectsToRegions(pageValidation, spec));63 options.setStretchToFit(spec.isStretch());64 options.setOriginalFilters(spec.getOriginalFilters());65 options.setSampleFilters(spec.getSampleFilters());66 options.setMapFilters(spec.getMapFilters());67 options.setTolerance(tolerance);68 options.setAnalyzeOffset(spec.getAnalyzeOffset());69 Rect elementArea = pageElement.getArea();70 List<String> realPaths = new LinkedList<>();71 for (String imagePossiblePath : spec.getImagePaths()) {72 if (imagePossiblePath.contains("*") || imagePossiblePath.contains("#")) {73 realPaths.addAll(GalenUtils.findFilesOrResourcesMatchingSearchExpression(imagePossiblePath));74 } else {75 realPaths.add(imagePossiblePath);76 }77 }78 if (realPaths.isEmpty()) {79 throw new ValidationErrorException("There are no images found").withValidationObject(new ValidationObject(pageElement.getArea(), objectName));80 }81 int largestPossibleDifference = elementArea.getHeight() * elementArea.getWidth() * 2;82 ImageCheck minCheck = new ImageCheck(realPaths.get(0), largestPossibleDifference, NO_RESULT, NO_ERROR_MESSAGE);83 Iterator<String> it = realPaths.iterator();84 try {85 while (minCheck.difference > 0 && it.hasNext()) {86 String imagePath = it.next();87 ImageCheck imageCheck = checkImages(spec, pageImage, options, elementArea, imagePath);88 if (imageCheck.difference <= minCheck.difference) {89 minCheck = imageCheck;90 }91 }92 } catch (ValidationErrorException ex) {93 LOG.trace("Validation errors during image compare.", ex);94 ex.withValidationObject(new ValidationObject(pageElement.getArea(), objectName));95 throw ex;96 } catch (Exception ex) {97 LOG.trace("Unknown errors during image compare", ex);98 throw new ValidationErrorException(ex).withValidationObject(new ValidationObject(pageElement.getArea(), objectName));99 }100 List<ValidationObject> objects = asList(new ValidationObject(pageElement.getArea(), objectName));101 if (minCheck.difference > 0) {102 throw new ValidationErrorException(minCheck.errorMessage)103 .withValidationObjects(objects)104 .withImageComparison(new ImageComparison(105 minCheck.result.getOriginalFilteredImage(),106 minCheck.result.getSampleFilteredImage(),107 minCheck.result.getComparisonMap()));108 }109 return new ValidationResult(spec, objects);110 }111 private List<Rectangle> convertIgnoreObjectsToRegions(PageValidation pageValidation, SpecImage spec) {112 List<Rectangle> ignoreRegions = new LinkedList<>();113 if (spec.getIgnoredObjectExpressions() != null) {114 for (String objectSearchExpression : spec.getIgnoredObjectExpressions()) {115 List<String> ignoreObjects = pageValidation.getPageSpec().findAllObjectsMatchingStrictStatements(objectSearchExpression);116 if (ignoreObjects != null) {117 for (String objectName: ignoreObjects) {118 PageElement pageElement = pageValidation.findPageElement(objectName);119 if (pageElement.isPresent() && pageElement.isVisible()) {120 ignoreRegions.add(pageElement.getArea().toAwtRectangle());121 }122 }123 }124 }125 }126 return ignoreRegions;127 }128 private ImageCheck checkImages(SpecImage spec, BufferedImage pageImage, ComparisonOptions options, Rect elementArea, String imagePath)129 throws ValidationErrorException {130 BufferedImage sampleImage;131 try {132 InputStream stream = GalenUtils.findFileOrResourceAsStream(imagePath);133 sampleImage = Rainbow4J.loadImage(stream);134 } catch (Exception ex) {135 LOG.error("Unknown errors during image check.", ex);136 throw new ValidationErrorException("Couldn't load image: " + spec.getImagePaths().get(0));137 }138 Rectangle sampleArea = spec.getSelectedArea() != null ? toRectangle(spec.getSelectedArea()) : new Rectangle(0, 0, sampleImage.getWidth(),139 sampleImage.getHeight());140 if (elementArea.getLeft() >= pageImage.getWidth() || elementArea.getTop() >= pageImage.getHeight()) {141 throw new RuntimeException(String.format(142 "The page element is located outside of the screenshot. (Element {x: %d, y: %d, w: %d, h: %d}, Screenshot {w: %d, h: %d})", elementArea.getLeft(),143 elementArea.getTop(), elementArea.getWidth(), elementArea.getHeight(), pageImage.getWidth(), pageImage.getHeight()));144 }145 if (spec.isCropIfOutside() || isOnlyOnePixelOutsideScreenshot(elementArea, pageImage)) {146 elementArea = cropElementAreaIfOutside(elementArea, pageImage.getWidth(), pageImage.getHeight());147 }148 ImageCompareResult result = Rainbow4J.compare(pageImage, sampleImage, toRectangle(elementArea), sampleArea, options);149 double difference = 0.0;150 String errorMessage = null;151 SpecImage.ErrorRate errorRate = spec.getErrorRate();152 if (errorRate == null) {153 errorRate = GalenConfig.getConfig().getImageSpecDefaultErrorRate();154 }155 if (errorRate.getType() == SpecImage.ErrorRateType.PERCENT) {156 difference = result.getPercentage() - errorRate.getValue();157 if (difference > 0) {158 errorMessage = createErrorMessageForPercentage(msgErrorPrefix(spec.getImagePaths().get(0)), errorRate.getValue(), result.getPercentage());159 }160 } else {161 difference = result.getTotalPixels() - errorRate.getValue();162 if (difference > 0) {163 errorMessage = createErrorMessageForPixels(msgErrorPrefix(spec.getImagePaths().get(0)), errorRate.getValue().intValue(), result.getTotalPixels());164 }165 }166 return new ImageCheck(imagePath, difference, result, errorMessage);167 }168 private boolean isOnlyOnePixelOutsideScreenshot(Rect elementArea, BufferedImage pageImage) {169 int dx = elementArea.getLeft() + elementArea.getWidth() - pageImage.getWidth();170 int dy = elementArea.getTop() + elementArea.getHeight() - pageImage.getHeight();171 return Math.max(dx, dy) == 1;172 }173 private Rect cropElementAreaIfOutside(Rect elementArea, int width, int height) {174 int x2 = elementArea.getLeft() + elementArea.getWidth();175 int y2 = elementArea.getTop() + elementArea.getHeight();176 int originalWidth = elementArea.getWidth();177 int originalHeight = elementArea.getHeight();178 if (originalWidth > 0 && originalHeight > 0) {179 int newWidth = originalWidth;180 int newHeight = originalHeight;...
ImageCheck
Using AI Code Generation
1import com.galenframework.reports.model.LayoutReport;2import com.galenframework.reports.model.LayoutReportBuilder;3import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener;4import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter;5import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter.LayoutReportBuilderListenerAdapterAdapter;6import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter.LayoutReportBuilderListenerAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapter;7import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter.LayoutReportBuilderListenerAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapter;8import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter.LayoutReportBuilderListenerAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapter;9import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter.LayoutReportBuilderListenerAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapterAdapter;10import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter.LayoutReportBuilderListenerAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapterAdapterAdapter;11import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter.LayoutReportBuilderListenerAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapterAdapterAdapter;12import com.galenframework.reports.model.LayoutReportBuilder.LayoutReportBuilderListener.LayoutReportBuilderListenerAdapter.LayoutReportBuilderListenerAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapterAdapter.LayoutReportBuilderListenerAdapterAdapterAdapterAdapterAdapterAdapterAdapter;13import com.galenframework.reports
ImageCheck
Using AI Code Generation
1import com.galenframework.reports.model.LayoutReport2import com.galenframework.validation.ValidationObject3import com.galenframework.validation.ValidationError4import com.galenframework.validation.ValidationResult5import com.galenframework.validation.ValidationListener6import com.galenframework.validation.ValidationListener7import com.galenframework.validation.specs.SpecValidationImage8import com.galenframework.validation.ValidationError9import com.galenframework.validation.ValidationObject10import com.gal
ImageCheck
Using AI Code Generation
1SpecValidationImage specValidationImage = new SpecValidationImage();2specValidationImage.ImageCheck("C:\\Users\\myuser\\Desktop\\image1.png", "C:\\Users\\myuser\\Desktop\\image2.png", 0.5);3SpecValidationImage specValidationImage = new SpecValidationImage();4specValidationImage.ImageCheck("C:\\Users\\myuser\\Desktop\\image1.png", "C:\\Users\\myuser\\Desktop\\image2.png", 0.5);5SpecValidationImage specValidationImage = new SpecValidationImage();6specValidationImage.ImageCheck("C:\\Users\\myuser\\Desktop\\image1.png", "C:\\Users\\myuser\\Desktop\\image2.png", 0.5);7SpecValidationImage specValidationImage = new SpecValidationImage();8specValidationImage.ImageCheck("C:\\Users\\myuser\\Desktop\\image1.png", "C:\\Users\\myuser\\Desktop\\image2.png", 0.5);9SpecValidationImage specValidationImage = new SpecValidationImage();10specValidationImage.ImageCheck("C:\\Users\\myuser\\Desktop\\image1.png", "C:\\Users\\myuser\\Desktop\\image2.png", 0.5);
ImageCheck
Using AI Code Generation
1test "Check if the image is displayed on the page" {2 image("img").check(ImageCheck.isDisplayed());3}4test "Check if the image is displayed on the page" {5 image("img").check(ImageCheck.isDisplayed());6}7test "Check if the image is in the specified area" {8 image("img").check(ImageCheck.isInArea(0, 0, 100, 100));9}10test "Check if the image is in the specified area" {11 image("img").check(ImageCheck.isInArea(0, 0, 100, 100));12}13test "Check if the image is in the specified area" {14 image("img").check(ImageCheck.isInArea(0, 0, 100, 100));15}16test "Check if the image is in the specified area" {17 image("img").check(ImageCheck.isInArea(0, 0, 100, 100));18}19test "Check if the image is in the specified area" {20 image("img").check(ImageCheck.isInArea(0, 0, 100, 100));21}22test "Check if the image is in the specified area" {23 image("img").check(ImageCheck.isInArea(0, 0, 100, 100));24}25test "Check if the image is in the specified area" {26 image("img").check(ImageCheck.isInArea(0, 0, 100, 100));27}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!