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

Bugfix/resolution and subimage issues #518

Merged
merged 19 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ All notable changes to AET will be documented in this file.
- [PR-480](https://github.com/Cognifide/aet/pull/480) Test summary stats on the main report page. ([#474](https://github.com/Cognifide/aet/issues/474))
- [PR-459](https://github.com/Cognifide/aet/pull/459) Print more meaningful error messages when suite.xml is malformed ([#436](https://github.com/Cognifide/aet/issues/436))
- [PR-517](https://github.com/Cognifide/aet/pull/517) Single URL rerun fixed for named URLs ([#487](https://github.com/Cognifide/aet/issues/487))
- [PR-518](https://github.com/Cognifide/aet/pull/518) Minimum site height ([#384](https://github.com/Cognifide/aet/issues/384))

## Version 3.2.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,28 @@
import com.cognifide.aet.job.common.SeleniumWaitHelper;
import com.cognifide.aet.job.common.modifiers.WebElementsLocatorParams;
import com.cognifide.aet.vs.ArtifactsDAO;
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;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -41,20 +58,6 @@
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;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;

public class ScreenCollector extends WebElementsLocatorParams implements CollectorJob {

Expand Down Expand Up @@ -212,8 +215,7 @@ private byte[] getImagePart(byte[] fullPage, WebElement webElement)
BufferedImage fullImg = ImageIO.read(in);
Point point = webElement.getLocation();
Dimension size = webElement.getSize();
BufferedImage screenshotSection = fullImg.getSubimage(point.getX(), point.getY(),
size.getWidth(), size.getHeight());
BufferedImage screenshotSection = getSubImage(fullImg, point, size);
return bufferedImageToByteArray(screenshotSection);
} catch (IOException e) {
throw new ProcessingException("Unable to create image from taken screenshot", e);
Expand All @@ -222,6 +224,18 @@ private byte[] getImagePart(byte[] fullPage, WebElement webElement)
}
}

BufferedImage getSubImage(BufferedImage fullImg, Point point, Dimension size) {
int width = calculateMeasure(fullImg.getWidth(), size.getWidth(), point.getX());
int height = calculateMeasure(fullImg.getHeight(), size.getHeight(), point.getY());

return fullImg.getSubimage(point.getX(), point.getY(), width, height);
}

private int calculateMeasure(int fullImgMeasure, int subImgMeasure, int point) {
int result = Math.min(subImgMeasure, fullImgMeasure - point);
return Math.max(result, 1);
}

private byte[] bufferedImageToByteArray(BufferedImage bufferedImage) throws ProcessingException {
try (ByteArrayOutputStream temporaryStream = new ByteArrayOutputStream()) {
ImageIO.write(bufferedImage, PNG_FORMAT, temporaryStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import com.cognifide.aet.job.common.utils.Sampler;
import java.util.Map;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import org.apache.commons.lang3.math.NumberUtils;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Window;
import org.slf4j.Logger;
Expand All @@ -44,6 +44,7 @@ public class ResolutionModifier implements CollectorJob {

private static final int HEIGHT_MAX_SIZE = 35000;
private static final int INITIAL_HEIGHT = 300;
private static final int MINIMUM_HEIGHT = 1;
private static final int HEIGHT_NOT_DEFINED = 0;
private static final int HEIGHT_NOT_CALCULATED = -1;
private static final int DEFAULT_SAMPLING_WAIT_PERIOD = 100;
Expand Down Expand Up @@ -108,6 +109,9 @@ private void setResolution() throws ProcessingException {
height = HEIGHT_MAX_SIZE;
} else if (height == HEIGHT_NOT_CALCULATED) {
throw new ProcessingException("Failed to calculate height, could not parse javascript result to integer");
} else if (height < MINIMUM_HEIGHT) {
LOG.warn("Detected page height is 0, setting height to 1");
height = MINIMUM_HEIGHT;
}
}
LOG.info("Setting resolution to {}x{} ", width, height);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* AET
* <p>
* Copyright (C) 2013 Cognifide Limited
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;


import static org.assertj.core.api.Assertions.assertThat;

@RunWith(MockitoJUnitRunner.class)
public class ScreenCollectorImageTest {

private static final Color black = new Color(0);

@InjectMocks
private ScreenCollector screenCollector;

private BufferedImage testImage;


@Before
public void setup() throws Exception {
String path = getClass().getResource("/screens/48x48.png").getFile();
File image = new File(path);
testImage = ImageIO.read(image);
}

@Test
public void testSubImageSize() {
Point point = new org.openqa.selenium.Point(12, 12);
Dimension dimension = new Dimension(24, 24);

BufferedImage subImage = screenCollector.getSubImage(testImage, point, dimension);

assertThat(subImage.getHeight()).isEqualTo(24);
assertThat(subImage.getWidth()).isEqualTo(24);

}

@Test
public void testSubImageContent() {
Point point = new org.openqa.selenium.Point(12, 12);
Dimension dimension = new Dimension(24, 24);

BufferedImage subImage = screenCollector.getSubImage(testImage, point, dimension);
boolean onlyBlack = true;

for (int i = 0; i < subImage.getHeight(); i++) {
for (int j = 0; j < subImage.getWidth(); j++) {
onlyBlack &= black.equals(new Color(subImage.getRGB(i, j)));
}
}
assertThat(onlyBlack).isTrue();
}

@Test
public void testSubImageSize_whenElementIsHidden() {
Point point = new Point(0, 0);
Dimension dimension = new Dimension(0, 0);

BufferedImage subImage = screenCollector.getSubImage(testImage, point, dimension);

assertThat(subImage.getHeight()).isEqualTo(1);
assertThat(subImage.getWidth()).isEqualTo(1);
}

@Test
public void testSubImageSize_whenElementIsBiggerThanImage() {
Point point = new Point(20, 20);
Dimension dimension = new Dimension(48, 48);

BufferedImage subImage = screenCollector.getSubImage(testImage, point, dimension);

assertThat(subImage.getHeight()).isEqualTo(28);
assertThat(subImage.getWidth()).isEqualTo(28);
}
}
Binary file added core/jobs/src/test/resources/screens/48x48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%--

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.

--%>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AET Demo Page</title>
</head>
<body style="display:none">
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%--

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.

--%>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AET Demo Page</title>
</head>
<body style="display:none">
<h1 class="someclass" style="display:none"></h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%--

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.

--%>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AET Demo Page</title>
</head>
<body style="height:1px">
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%--

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.

--%>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AET Demo Page</title>
</head>
<body style="height:10px">
<div style="height:100px"></div>
</body>
</html>
57 changes: 57 additions & 0 deletions integration-tests/test-suite/partials/layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,62 @@
</urls>
</test>

<test name="S-comparator-Layout-Auto-Height-1px-Page">
<collect>
<open/>
<resolution width="767"/>
<screen/>
</collect>
<compare>
<screen comparator="layout"/>
</compare>
<urls>
<url href="comparators/layout/1px_height_page.jsp"/>
</urls>
</test>

<test name="S-comparator-Layout-Auto-Height-0px-Page">
Copy link
Contributor

@plutasnyy plutasnyy Aug 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not 100% sure but with a change in integration tests you should also update https://github.com/Cognifide/aet/blob/master/integration-tests/sanity-functional/src/test/resources/features/filtering.feature or/and https://github.com/Cognifide/aet/blob/master/integration-tests/sanity-functional/src/test/java/com/cognifide/aet/sanity/functional/HomePageTilesTest.java

I remember that documentation contains some tutorial on how to run these tests but unfortunately I cannot find it :P

<collect>
<open/>
<resolution width="767"/>
<screen/>
</collect>
<compare>
<screen comparator="layout"/>
</compare>
<urls>
<url href="comparators/layout/0px_height_page.jsp"/>
</urls>
</test>

<test name="S-comparator-Layout-Auto-Height-0px-Page-With-Partial">
<collect>
<open/>
<resolution width="767"/>
<screen css="someclass"/>
</collect>
<compare>
<screen comparator="layout"/>
</compare>
<urls>
<url href="comparators/layout/0px_height_page_partial.jsp"/>
</urls>
</test>

<test name="S-comparator-Layout-Auto-Height-Partial-Bigger-Than-Page">
<collect>
<open/>
<resolution width="767"/>
<screen css="body > div"/>
</collect>
<compare>
<screen comparator="layout" />
</compare>
<urls>
<url href="comparators/layout/partial_bigger_than_page.jsp"/>
</urls>
</test>


<!-- Layout-Comparator END -->
</suite>