How to use DecimalFormat method of com.galenframework.validation.specs.SpecValidationImage class

Best Galen code snippet using com.galenframework.validation.specs.SpecValidationImage.DecimalFormat

Source:SpecValidationImage.java Github

copy

Full Screen

...16package com.galenframework.validation.specs;17import java.awt.Rectangle;18import java.awt.image.BufferedImage;19import java.io.InputStream;20import java.text.DecimalFormat;21import java.util.Iterator;22import java.util.LinkedList;23import java.util.List;24import com.galenframework.page.Rect;25import com.galenframework.specs.SpecImage;26import com.galenframework.validation.*;27import com.galenframework.config.GalenConfig;28import com.galenframework.page.PageElement;29import com.galenframework.utils.GalenUtils;30import com.galenframework.rainbow4j.ComparisonOptions;31import com.galenframework.rainbow4j.ImageCompareResult;32import com.galenframework.rainbow4j.Rainbow4J;33import org.slf4j.Logger;34import org.slf4j.LoggerFactory;35import static java.util.Arrays.asList;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;181 if (x2 >= width) {182 newWidth -= x2 - width + 1;183 }184 if (y2 >= height) {185 newHeight -= y2 - height + 1;186 }187 if ((double) (newWidth * newHeight) / (double) (originalWidth * originalHeight) < 0.5) {188 throw new RuntimeException(String.format(189 "The cropped area is less than a half of element area (Element {x: %d, y: %d, w: %d, h: %d}, Screenshot {w: %d, h: %d})", elementArea.getLeft(),190 elementArea.getTop(), newWidth, newHeight, width, height));191 }192 return new Rect(elementArea.getLeft(), elementArea.getTop(), newWidth, newHeight);193 }194 return elementArea;195 }196 private String msgErrorPrefix(String imagePath) {197 return String.format("Element does not look like \"%s\". ", imagePath);198 }199 private String createErrorMessageForPixels(String msgPrefix, Integer maxPixels, long totalPixels) throws ValidationErrorException {200 return String.format("%sThere are %d mismatching pixels but max allowed is %d", msgPrefix, totalPixels, maxPixels);201 }202 private String createErrorMessageForPercentage(String msgPrefix, Double maxPercentage, double percentage) throws ValidationErrorException {203 return String.format("%sThere are %s%% mismatching pixels but max allowed is %s%%", msgPrefix, formatDouble(percentage), formatDouble(maxPercentage));204 }205 private static final DecimalFormat _doubleFormat = new DecimalFormat("#.##");206 private String formatDouble(Double value) {207 return _doubleFormat.format(value);208 }209 private Rectangle toRectangle(Rect area) {210 return new Rectangle(area.getLeft(), area.getTop(), area.getWidth(), area.getHeight());211 }212}...

Full Screen

Full Screen

DecimalFormat

Using AI Code Generation

copy

Full Screen

1SpecValidationImage svi = new SpecValidationImage();2String str = "123.456";3DecimalFormat df = new DecimalFormat();4Number num = df.parse(str);5SpecValidationImage svi = new SpecValidationImage();6String str = "123.456";7NumberFormat nf = NumberFormat.getInstance();8Number num = nf.parse(str);9DecimalFormat df = new DecimalFormat();10String str = "123.456";11Number num = df.parse(str);12NumberFormat nf = NumberFormat.getInstance();13String str = "123.456";14Number num = nf.parse(str);15String str = "123.456";16Double num = Double.parseDouble(str);17String str = "123.456";18Float num = Float.parseFloat(str);19String str = "123.456";20BigDecimal num = new BigDecimal(str);21String str = "123.456";22BigInteger num = new BigInteger(str);23String str = "123.456";24Byte num = Byte.parseByte(str);

Full Screen

Full Screen

DecimalFormat

Using AI Code Generation

copy

Full Screen

1public class GalenImageSpecs {2 public static void main(String[] args) {3 String galenSpecs = "image-specs.txt";4 String galenTest = "image-test.txt";5 String galenReport = "image-report.html";6 String galenTestName = "image-test";7 String galenTestSize = "1024x768";8 Galen galen = Galen.builder().build();9 GalenPage galenPage = galen.loadPageFromUrl(galenTestUrl, galenTestSize);10 GalenPageDump galenPageDump = galen.getPageDump(galenPage);11 GalenPageLayout galenPageLayout = galen.getPageLayout(galenPageDump, galenSpecs);12 GalenPageValidation galenPageValidation = galen.checkLayout(galenPage, galenSpecs, Arrays.asList("mobile"));13 GalenTestInfo galenTestInfo = GalenTestInfo.fromString(galenTestName);14 GalenTestReport galenTestReport = galen.createTestReport(Arrays.asList(galenPageValidation), galenTestInfo);15 galenTestReport.getReport().getConfig().setReportLayout(GalenReportConfig.GalenReportLayout.LAYOUT_1);16 galenTestReport.getReport().getConfig().setReportName(galenTestName);17 galenTestReport.getReport().getConfig().setReportTitle(galenTestName);18 galenTestReport.getReport().getConfig().setShowReportName(true);19 galenTestReport.getReport().getConfig().setShowReportTitle(true);20 galenTestReport.getReport().getConfig().setShowSidebar(true);21 galenTestReport.getReport().getConfig().setShowTestDescription(true);

Full Screen

Full Screen

DecimalFormat

Using AI Code Generation

copy

Full Screen

1import com.galenframework.validation.specs.SpecValidationImage2def galen = new Galen()3def report = galen.checkLayout(4 asList("mobile", "tablet")5def reportText = report.getHtmlReport()6def reportText2 = reportText.replaceAll( /(\d+\.\d+)/, { SpecValidationImage.formatNumber(it[0].toDouble()) } )7new File(reportPath).text = reportText2

Full Screen

Full Screen

DecimalFormat

Using AI Code Generation

copy

Full Screen

1public String getDecimalFormatRegex() {2 String format = getDecimalFormat().format(0);3 String regex = format.substring(0, format.indexOf("0"));4 return "^" + regex + "(\\d+)$";5}6public void testDecimalFormatRegex() {7 SpecValidationImage spec = new SpecValidationImage();8 spec.setDecimalPlaces(2);9 String regex = spec.getDecimalFormatRegex();10 System.out.println("regex: " + regex);11 assertTrue("0.01".matches(regex));12 assertTrue("0.10".matches(regex));13 assertTrue("0.99".matches(regex));14 assertTrue("1.00".matches(regex));15 assertTrue("1.01".matches(regex));16 assertTrue("1.10".matches(regex));17 assertTrue("1.99".matches(regex));18 assertTrue("9.00".matches(regex));19 assertTrue("9.01".matches(regex));20 assertTrue("9.10".matches(regex));21 assertTrue("9.99".matches(regex));22 assertTrue("10.00".matches(regex));23 assertTrue("10.01".matches(regex));24 assertTrue("10.10".matches(regex));25 assertTrue("10.99".matches(regex));26 assertTrue("99.00".matches(regex));27 assertTrue("99.01".matches(regex));28 assertTrue("99.10".matches(regex));29 assertTrue("99.99".matches(regex));30 assertTrue("100.00".matches(regex));31 assertTrue("100.01".matches(regex));32 assertTrue("100.10".matches(regex));33 assertTrue("100.99".matches(regex));34 assertTrue("999.00".matches(regex));35 assertTrue("999.01".matches(regex));36 assertTrue("999.10".matches(regex));37 assertTrue("999.99".matches(regex));38 assertTrue("1000.00".matches(regex));39 assertTrue("1000.01".matches(regex));40 assertTrue("1000.10".matches(regex));

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful