Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/exclude elements notification #410

Merged
merged 6 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ All notable changes to AET will be documented in this file.
- [PR-430](https://github.com/Cognifide/aet/pull/430) Upgraded HTML codesniffer to latest release (3.2.0)
- [PR-385](https://github.com/Cognifide/aet/pull/385) Fixed ChefDK and vagrant-berkshelf versions
- [PR-404](https://github.com/Cognifide/aet/pull/404) Added missing tooltip for conditional tests

- [PR-408](https://github.com/Cognifide/aet/pull/408) Advanced Screen Comparision button layout fix
- [PR-410](https://github.com/Cognifide/aet/pull/410) Notification that displays when exclude-elements are not found on page now shows what specific elements were not found([#372](https://github.com/Cognifide/aet/issues/372))

## Version 3.1.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.Serializable;

public class ExcludedElement implements Serializable {

private static final long serialVersionUID = 692282363549228800L;

private final Point point;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@

import java.io.Serializable;
import java.util.List;
import java.util.Set;

public class LayoutExclude implements Serializable {

private static final long serialVersionUID = -6496109702966509444L;

private final List<ExcludedElement> excludedElements;
private final Set<String> notFoundElements;

public LayoutExclude(
List<ExcludedElement> excludedElements) {
public LayoutExclude(List<ExcludedElement> excludedElements, Set<String> notFoundElements) {
this.excludedElements = excludedElements;
this.notFoundElements = notFoundElements;
}

public List<ExcludedElement> getExcludedElements() {
return excludedElements;
}

public Set<String> getNotFoundElements() {
return notFoundElements;
}
}
6 changes: 5 additions & 1 deletion core/jobs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,15 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
Expand All @@ -56,6 +64,8 @@ public class ScreenCollector extends WebElementsLocatorParams implements Collect

private static final String PNG_FORMAT = "png";

private static final String CSS_SELECTOR_SEPARATOR = ",";

private final WebDriver webDriver;

private final ArtifactsDAO artifactsDAO;
Expand All @@ -70,22 +80,6 @@ public class ScreenCollector extends WebElementsLocatorParams implements Collect
this.artifactsDAO = artifactsDAO;
}

private List<ExcludedElement> getExcludeElementsFromWebElements(List<WebElement> webElements) {
List<ExcludedElement> excludeExcludedElements = new ArrayList<>(webElements.size());

Point screenshotOffset = isSelectorPresent() ?
webDriver.findElement(getLocator()).getLocation() : new Point(0, 0);
for (WebElement webElement : webElements) {
Point point = webElement.getLocation()
.moveBy(-screenshotOffset.getX(), -screenshotOffset.getY());

excludeExcludedElements.add(new ExcludedElement(
new java.awt.Point(point.getX(), point.getY()),
new java.awt.Dimension(webElement.getSize().width, webElement.getSize().height)));
}
return excludeExcludedElements;
}

@Override
public CollectorStepResult collect() throws ProcessingException {
byte[] screenshot = takeScreenshot();
Expand All @@ -98,11 +92,7 @@ public CollectorStepResult collect() throws ProcessingException {
String resultId = artifactsDAO.saveArtifact(properties, screenshotStream, CONTENT_TYPE);

if (excludeCssSelector != null) {
List<ExcludedElement> excludeExcludedElements = getExcludeElementsFromWebElements(
webDriver.findElements(By.cssSelector(excludeCssSelector)));
stepResult = CollectorStepResult
.newCollectedResult(resultId,
new Payload((new LayoutExclude(excludeExcludedElements))));
stepResult = getResultWithExcludeSelectors(resultId);
} else {
stepResult = CollectorStepResult.newCollectedResult(resultId);
}
Expand All @@ -124,6 +114,65 @@ private boolean isPatternAndResultMD5Identical(byte[] screenshot) {
}
}

private CollectorStepResult getResultWithExcludeSelectors(String resultId) {
ArrayList<String> cssSelectors = new ArrayList<>(
Arrays.asList(excludeCssSelector.split(CSS_SELECTOR_SEPARATOR)));

Map<String, List<WebElement>> elements = searchElementsOnPage(cssSelectors);

List<WebElement> foundExcludeElements = getFoundWebElements(elements);
Set<String> notFoundSelectors = getNotFoundSelectors(elements);

List<ExcludedElement> excludedElements = getExcludeElementsFromWebElements(
foundExcludeElements);
return CollectorStepResult.newCollectedResult(resultId,
new Payload((new LayoutExclude(excludedElements, notFoundSelectors))));
}

private HashMap<String, List<WebElement>> searchElementsOnPage(ArrayList<String> cssSelectors) {
HashMap<String, List<WebElement>> elements = new HashMap<>();

cssSelectors.forEach(selector -> {
List<WebElement> foundElements = webDriver.findElements(By.cssSelector(selector));
if (!CollectionUtils.isEmpty(foundElements)) {
elements.put(selector, foundElements);
} else {
elements.put(selector, Collections.emptyList());
}
});

return elements;
}

private List<WebElement> getFoundWebElements(Map<String, List<WebElement>> elements) {
return elements.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

private Set<String> getNotFoundSelectors(Map<String, List<WebElement>> elements) {
return elements.entrySet().stream()
.filter(entry -> entry.getValue().isEmpty())
.map(Entry::getKey)
.collect(Collectors.toSet());
}

private List<ExcludedElement> getExcludeElementsFromWebElements(List<WebElement> webElements) {
List<ExcludedElement> excludeExcludedElements = new ArrayList<>(webElements.size());

Point screenshotOffset = isSelectorPresent() ?
webDriver.findElement(getLocator()).getLocation() : new Point(0, 0);
for (WebElement webElement : webElements) {
Point point = webElement.getLocation()
.moveBy(-screenshotOffset.getX(), -screenshotOffset.getY());

excludeExcludedElements.add(new ExcludedElement(
new java.awt.Point(point.getX(), point.getY()),
new java.awt.Dimension(webElement.getSize().width, webElement.getSize().height)));
}
return excludeExcludedElements;
}

@Override
public void setParameters(Map<String, String> params) throws ParametersException {
if (StringUtils.isNotBlank(params.get(XPATH_PARAM)) || StringUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.SecureRandom;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -64,7 +67,7 @@ public class LayoutComparator implements ComparatorJob {

private boolean excludeFunctionIsOn;

private boolean excludeElementsNotFound;
private Set<String> notFoundExcludeElements;

LayoutComparator(ComparatorProperties comparatorProperties, ArtifactsDAO artifactsDAO) {
this.properties = comparatorProperties;
Expand Down Expand Up @@ -105,9 +108,8 @@ public ComparatorStepResult compare() throws ProcessingException {
if (!CollectionUtils.isEmpty(excludedElements)) {
hideElementsInImg(patternImg, excludedElements);
hideElementsInImg(collectedImg, excludedElements);
} else {
excludeElementsNotFound = true;
}
notFoundExcludeElements = exclude.getNotFoundElements();
});

imageComparisonResult = ImageComparison.compare(patternImg, collectedImg);
Expand Down Expand Up @@ -144,8 +146,10 @@ private ComparatorStepResult saveArtifacts(ImageComparisonResult imageComparison
result = new ComparatorStepResult(maskArtifactId, Status.FAILED, true);
}

if (excludeElementsNotFound) {
addExcludeMessageToResult(result, "Elements to exclude are not found on page");
if (!CollectionUtils.isEmpty(notFoundExcludeElements)) {
addExcludeMessageToResult(result,
"The following elements to be excluded have not been found on page: ",
String.join(", ", notFoundExcludeElements));
}

addPixelDifferenceDataToResult(result, imageComparisonResult);
Expand Down Expand Up @@ -203,8 +207,10 @@ private void addPixelDifferenceDataToResult(ComparatorStepResult result,
Double.toString(imageComparisonResult.getPercentagePixelDifference()));
}

private void addExcludeMessageToResult(ComparatorStepResult result, String message) {
private void addExcludeMessageToResult(ComparatorStepResult result, String message,
String notFoundCssElements) {
result.addData("excludeMessage", message);
result.addData("notFoundCssElements", notFoundCssElements);
}

private void addTimestampToResult(ComparatorStepResult result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* AET
*
* Copyright (C) 2013 Cognifide Limited
*
* 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.
*/
package com.cognifide.aet.job.common.collectors.screen;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.when;

import com.cognifide.aet.communication.api.metadata.exclude.LayoutExclude;
import com.cognifide.aet.job.api.collector.CollectorProperties;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.cognifide.aet.vs.ArtifactsDAO;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;

@RunWith(MockitoJUnitRunner.class)
public class ScreenCollectorTest {

private static final String SELECTOR_1 = ".dynamic1";

private static final String SELECTOR_2 = ".dynamic2";

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private WebElement webElement1;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private WebElement webElement2;

@Mock
private RemoteWebDriver webDriver;

@Mock
private CollectorProperties collectorProperties;

@Mock
private ArtifactsDAO artifactsDAO;

@InjectMocks
private ScreenCollector screenCollector;

private byte[] screenshot = {1, 2};

@Before
public void setup() throws Exception {
when(collectorProperties.getPatternId()).thenReturn(null);
when(webDriver.getScreenshotAs(OutputType.BYTES)).thenReturn(screenshot);

when(webElement1.getLocation().moveBy(anyInt(), anyInt())).thenReturn(new Point(0, 0));
when(webElement2.getLocation().moveBy(anyInt(), anyInt())).thenReturn(new Point(0, 0));

setExcludeElementsParam(String.format("%s,%s", SELECTOR_1, SELECTOR_2));
}

@Test
public void collectScreenExcludeElements_findAll() throws Exception {
when(webDriver.findElements(By.cssSelector(SELECTOR_1)))
.thenReturn(Collections.singletonList(webElement1));
when(webDriver.findElements(By.cssSelector(SELECTOR_2)))
.thenReturn(Collections.singletonList(webElement2));

LayoutExclude exclude = screenCollector.collect().getPayload().getLayoutExclude();

assertThat(exclude.getExcludedElements()).hasSize(2);
assertThat(exclude.getNotFoundElements()).isNullOrEmpty();
}

@Test
public void collectScreenExcludeElements_findNone() throws Exception {
when(webDriver.findElements(By.cssSelector(SELECTOR_1)))
.thenReturn(Collections.emptyList());
when(webDriver.findElements(By.cssSelector(SELECTOR_2)))
.thenReturn(Collections.emptyList());

LayoutExclude exclude = screenCollector.collect().getPayload().getLayoutExclude();

assertThat(exclude.getExcludedElements()).isEmpty();
assertThat(exclude.getNotFoundElements()).hasSize(2);
}

@Test
public void collectScreenExcludeElements_findSome() throws Exception {
when(webDriver.findElements(By.cssSelector(SELECTOR_1)))
.thenReturn(Collections.singletonList(webElement1));
when(webDriver.findElements(By.cssSelector(SELECTOR_2)))
.thenReturn(Collections.emptyList());

LayoutExclude exclude = screenCollector.collect().getPayload().getLayoutExclude();

assertThat(exclude.getExcludedElements()).hasSize(1);
assertThat(exclude.getNotFoundElements()).hasSize(1);
}

private void setExcludeElementsParam(String excludeCssElements) throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put("exclude-elements", excludeCssElements);
screenCollector.setParameters(params);
}
}
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,12 @@
<version>1.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
Expand Down
Loading