io.wcm.qa.glnm.galen.specs.imagecomparison.ImageComparisonSpecDefinition Maven / Gradle / Ivy
Show all versions of io.wcm.qa.galenium.galen Show documentation
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2017 wcm.io
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.qa.glnm.galen.specs.imagecomparison;
import static java.util.Locale.ENGLISH;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.galenframework.validation.ValidationListener;
import io.wcm.qa.glnm.configuration.GaleniumConfiguration;
import io.wcm.qa.glnm.differences.base.Difference;
import io.wcm.qa.glnm.differences.generic.SortedDifferences;
import io.wcm.qa.glnm.galen.specs.page.GalenCorrection;
import io.wcm.qa.glnm.galen.specs.page.GalenCorrectionRect;
import io.wcm.qa.glnm.interaction.Mouse;
import io.wcm.qa.glnm.selectors.base.Selector;
import io.wcm.qa.glnm.util.BrowserUtil;
/**
* Factory for fileless image comparison specs.
*
* @since 1.0.0
*/
public class ImageComparisonSpecDefinition implements IcsDefinition {
private static final String DEFAULT_PAGE_SECTION_NAME = "Image Comparison";
private String allowedError;
private int allowedOffset;
private GalenCorrectionRect corrections;
private boolean cropIfOutside;
private SortedDifferences differences = new SortedDifferences();
private String elementName;
private String filename;
private String foldername;
private List objectsToIgnore = new ArrayList();
private String sectionName = DEFAULT_PAGE_SECTION_NAME;
private Selector selector;
private boolean zeroToleranceWarning;
/**
* Constructor for ImageComparisonSpecFactory.
*
* @param selector selector for main object
* @since 2.0.0
*/
public ImageComparisonSpecDefinition(Selector selector) {
this(selector, selector.elementName());
if (BrowserUtil.isChrome()) {
correctForSrollPosition(Mouse.getVerticalScrollPosition().intValue());
}
}
/**
* Constructor for ImageComparisonSpecFactory.
*
* @param selector selector for main object
* @param elementName object name to use
* @since 2.0.0
*/
public ImageComparisonSpecDefinition(Selector selector, String elementName) {
setSelector(selector);
setElementName(elementName);
setFilename(elementName.toLowerCase(ENGLISH) + ".png");
setSectionName(DEFAULT_PAGE_SECTION_NAME + " for " + getElementName());
}
/**
* addAll.
*
* @param toBeAppended differences to be appended
* @return true if this list changed as a result of the call
* @since 2.0.0
*/
public boolean addAll(Collection extends Difference> toBeAppended) {
return getDifferences().addAll(toBeAppended);
}
/**
* addDifference.
*
* @param difference appends a difference
* @since 2.0.0
*/
public void addDifference(Difference difference) {
getDifferences().add(difference);
}
/**
* addObjectToIgnore.
*
* @param selectorToIgnore the area of this object will be ignored in image comparison
* @since 2.0.0
*/
public void addObjectToIgnore(Selector selectorToIgnore) {
getObjectsToIgnore().add(selectorToIgnore);
}
/**
* Removes all differences added to this factory.
*
* @since 2.0.0
*/
public void clearDifferences() {
getDifferences().clear();
}
/** {@inheritDoc} */
@Override
public void correctForSrollPosition(int yCorrection) {
GalenCorrection top = GalenCorrection.adjust(-yCorrection);
setCorrections(new GalenCorrectionRect().withTop(top));
}
/**
* {@inheritDoc}
*
* Getter for the field allowedError
.
*/
@Override
public String getAllowedError() {
return allowedError;
}
/**
* {@inheritDoc}
*
* Getter for the field allowedOffset
.
*/
@Override
public int getAllowedOffset() {
return allowedOffset;
}
/**
* getComparator.
*
* @return a {@link java.util.Comparator} object.
* @since 2.0.0
*/
public Comparator getComparator() {
return this.differences.getComparator();
}
/**
* {@inheritDoc}
*
*
* Getter for the field differences
.
*
*/
@Override
public GalenCorrectionRect getCorrections() {
return corrections;
}
/**
* Getter for the field differences
.
*
* @return a {@link io.wcm.qa.glnm.differences.generic.SortedDifferences} object.
* @since 4.0.0
*/
public SortedDifferences getDifferences() {
return differences;
}
/**
* {@inheritDoc}
*
* Getter for the field elementName
.
*/
@Override
public String getElementName() {
return elementName;
}
/**
* {@inheritDoc}
*
* Getter for the field filename
.
*/
@Override
public String getFilename() {
return filename;
}
/**
* {@inheritDoc}
*
* Getter for the field foldername
.
*/
@Override
public String getFoldername() {
if (foldername != null) {
return foldername;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(GaleniumConfiguration.getExpectedImagesDirectory());
stringBuilder.append("/");
stringBuilder.append(getDifferences().asFilePath());
return stringBuilder.toString();
}
/**
* {@inheritDoc}
*
* Getter for the field objectsToIgnore
.
*/
@Override
public List getObjectsToIgnore() {
return objectsToIgnore;
}
/**
* {@inheritDoc}
*
* Getter for the field sectionName
.
*/
@Override
public String getSectionName() {
return sectionName;
}
/**
* {@inheritDoc}
*
* Getter for the field selector
.
*/
@Override
public Selector getSelector() {
return selector;
}
/**
* Getter for the field validationListener
.
*
* @return a {@link com.galenframework.validation.ValidationListener} object.
* @since 2.0.0
*/
public ValidationListener getValidationListener() {
return new IcValidationListener();
}
/** {@inheritDoc} */
@Override
public boolean isCropIfOutside() {
return cropIfOutside;
}
/** {@inheritDoc} */
@Override
public boolean isZeroToleranceWarning() {
return zeroToleranceWarning;
}
/**
* setAllowedErrorPercent.
*
* @param allowedErrorPercentage zero or negative will result in zero tolerance
* @since 2.0.0
*/
public void setAllowedErrorPercent(Double allowedErrorPercentage) {
if (allowedErrorPercentage > 0) {
this.allowedError = allowedErrorPercentage + "%";
}
else {
this.allowedError = StringUtils.EMPTY;
}
}
/**
* setAllowedErrorPixel.
*
* @param allowedErrorPixels zero or negative will result in zero tolerance
* @since 2.0.0
*/
public void setAllowedErrorPixel(Integer allowedErrorPixels) {
if (allowedErrorPixels > 0) {
this.allowedError = allowedErrorPixels + "px";
}
else {
this.allowedError = StringUtils.EMPTY;
}
}
/**
* Setter for the field allowedOffset
.
*
* @param allowedOffset a int.
* @since 2.0.0
*/
public void setAllowedOffset(int allowedOffset) {
this.allowedOffset = allowedOffset;
}
/**
* setComparator.
*
* @param comparator used to order differences in a consistent manner
* @since 2.0.0
*/
public void setComparator(Comparator comparator) {
this.differences.setComparator(comparator);
}
/**
* Setter for the field corrections
.
*
* @param corrections a {@link com.galenframework.specs.page.CorrectionsRect} object.
* @since 2.0.0
*/
public void setCorrections(GalenCorrectionRect corrections) {
this.corrections = corrections;
}
/**
* Setter for the field cropIfOutside
.
*
* @param cropIfOutside a boolean.
* @since 4.0.0
*/
public void setCropIfOutside(boolean cropIfOutside) {
this.cropIfOutside = cropIfOutside;
}
/**
* Setter for the field differences
.
*
* @param differences a {@link io.wcm.qa.glnm.differences.generic.SortedDifferences} object.
* @since 2.0.0
*/
public void setDifferences(SortedDifferences differences) {
this.differences = differences;
}
/**
* Setter for the field elementName
.
*
* @param elementName a {@link java.lang.String} object.
* @since 2.0.0
*/
public void setElementName(String elementName) {
this.elementName = elementName;
}
/**
* Setter for the field filename
.
*
* @param filename a {@link java.lang.String} object.
* @since 2.0.0
*/
public void setFilename(String filename) {
this.filename = filename;
}
/**
* Setter for the field foldername
.
*
* @param foldername a {@link java.lang.String} object.
* @since 2.0.0
*/
public void setFoldername(String foldername) {
this.foldername = foldername;
}
/**
* Setter for the field objectsToIgnore
.
*
* @param objectsToIgnore a {@link java.util.List} object.
* @since 2.0.0
*/
public void setObjectsToIgnore(List objectsToIgnore) {
this.objectsToIgnore = objectsToIgnore;
}
/**
* Setter for the field sectionName
.
*
* @param sectionName a {@link java.lang.String} object.
* @since 2.0.0
*/
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
/**
* Setter for the field selector
.
*
* @param selector a {@link io.wcm.qa.glnm.selectors.base.Selector} object.
* @since 2.0.0
*/
public void setSelector(Selector selector) {
this.selector = selector;
}
/**
* Setter for the field zeroToleranceWarning
.
*
* @param zeroToleranceWarning a boolean.
* @since 2.0.0
*/
public void setZeroToleranceWarning(boolean zeroToleranceWarning) {
this.zeroToleranceWarning = zeroToleranceWarning;
}
}