Skip to content

Commit 53ea4a4

Browse files
authored
Merge pull request #91 from ITArray/fullscreen_screenshot_web
Fullscreen screenshot web
2 parents 47b092a + 03fd2e5 commit 53ea4a4

File tree

10 files changed

+201
-118
lines changed

10 files changed

+201
-118
lines changed

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.itarray</groupId>
88
<artifactId>automotion</artifactId>
9-
<version>2.1.0-rc1</version>
9+
<version>2.1.0</version>
1010
<name>Automotion</name>
1111
<description>Library for smart visual automation testing</description>
1212
<url>https://automotion.itarray.net</url>
@@ -163,17 +163,17 @@
163163
<dependency>
164164
<groupId>org.seleniumhq.selenium</groupId>
165165
<artifactId>selenium-java</artifactId>
166-
<version>3.9.1</version>
166+
<version>3.10.0</version>
167167
</dependency>
168168
<dependency>
169169
<groupId>org.seleniumhq.selenium</groupId>
170170
<artifactId>selenium-server</artifactId>
171-
<version>3.9.1</version>
171+
<version>3.10.0</version>
172172
</dependency>
173173
<dependency>
174174
<groupId>org.seleniumhq.selenium</groupId>
175175
<artifactId>selenium-remote-driver</artifactId>
176-
<version>3.9.1</version>
176+
<version>3.10.0</version>
177177
</dependency>
178178
<dependency>
179179
<groupId>io.appium</groupId>
@@ -215,6 +215,12 @@
215215
<artifactId>javax.mail-api</artifactId>
216216
<version>1.6.1</version>
217217
</dependency>
218+
<dependency>
219+
<groupId>ru.yandex.qatools.ashot</groupId>
220+
<artifactId>ashot</artifactId>
221+
<version>1.5.4</version>
222+
</dependency>
223+
218224
<dependency>
219225
<groupId>com.optimaize.languagedetector</groupId>
220226
<artifactId>language-detector</artifactId>

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
[![Build Status](https://travis-ci.org/ITArray/automotion-java.svg?branch=master)](https://travis-ci.org/ITArray/automotion-java)
1+
[![Build Status](https://travis-ci.org/ITArray/automotion-java.svg?branch=master)](https://travis-ci.org/ITArray/automotion-java) [![Maven Central](https://img.shields.io/maven-central/v/net.itarray/automotion.svg?label=Maven%20Central)](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22net.itarray%22%20AND%20a%3A%22automotion%22)
22

33
# Automotion #
44
![alt tag](https://www.itarray.net/wp-content/uploads/2016/12/Automotion-2.jpg)
55

66
##### Official web site: https://automotion.itarray.net/
77
##### Example: https://github.com/ITArray/automotion-example
8+
##### Example of composed report based on automotion-maven-plugin: https://automotion-report-demo.itarray.net/
89
### Steps to connect ###
910
- Repo:
1011
* add dependecy:

src/main/java/net/itarray/automotion/internal/DriverFacade.java

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import io.appium.java_client.android.AndroidDriver;
55
import io.appium.java_client.ios.IOSDriver;
66
import net.itarray.automotion.internal.geometry.Vector;
7+
import net.itarray.automotion.tools.general.SystemHelper;
78
import org.openqa.selenium.*;
89
import org.openqa.selenium.chrome.ChromeDriver;
910
import org.openqa.selenium.firefox.FirefoxDriver;
1011
import org.openqa.selenium.phantomjs.PhantomJSDriver;
1112
import org.openqa.selenium.remote.RemoteWebDriver;
13+
import ru.yandex.qatools.ashot.AShot;
14+
import ru.yandex.qatools.ashot.Screenshot;
15+
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
1216

1317
import javax.imageio.ImageIO;
1418
import java.awt.image.BufferedImage;
@@ -17,7 +21,9 @@
1721
import java.io.IOException;
1822
import java.io.OutputStream;
1923

20-
import static net.itarray.automotion.tools.environment.EnvironmentFactory.*;
24+
import static java.lang.Integer.parseInt;
25+
import static net.itarray.automotion.tools.environment.EnvironmentFactory.getApp;
26+
import static net.itarray.automotion.tools.environment.EnvironmentFactory.isFirefox;
2127

2228
public class DriverFacade {
2329
private final WebDriver driver;
@@ -32,12 +38,32 @@ public File takeScreenshot() {
3238

3339
public void takeScreenshot(File file) {
3440
file.getParentFile().mkdirs();
35-
byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
3641

37-
try (OutputStream stream = new FileOutputStream(file); ){
38-
stream.write(bytes);
39-
} catch (IOException e) {
40-
throw new RuntimeException(e);
42+
if (!isPhantomJSDriver() && !isAppiumContext() && parseInt(getZoom().replace("%", "")) <= 100) {
43+
long windowYOffset = (long) executeScript("return window.pageYOffset");
44+
long windowXOffset = (long) executeScript("return window.pageXOffset");
45+
46+
Screenshot screenshot = new AShot()
47+
.shootingStrategy(ShootingStrategies.viewportRetina(100,
48+
Integer.parseInt(System.getProperty("headerCutPx") != null ? System.getProperty("headerCutPx") : "0"),
49+
Integer.parseInt(System.getProperty("footerCutPx") != null ? System.getProperty("footerCutPx") : "0"),
50+
(SystemHelper.isRetinaDisplay()) ? 2 : 1)).takeScreenshot(driver);
51+
52+
try {
53+
ImageIO.write(screenshot.getImage(), "PNG", file);
54+
} catch (IOException e) {
55+
e.printStackTrace();
56+
}
57+
58+
executeScript("window.scrollTo(" + windowXOffset + ", " + windowYOffset + ")");
59+
} else {
60+
byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
61+
62+
try (OutputStream stream = new FileOutputStream(file)) {
63+
stream.write(bytes);
64+
} catch (IOException e) {
65+
throw new RuntimeException(e);
66+
}
4167
}
4268
}
4369

@@ -112,6 +138,21 @@ public String getZoom() {
112138
}
113139
}
114140

141+
public void setZoom(int percentage) {
142+
if (!isAppiumContext()) {
143+
if (percentage <= 0) {
144+
throw new IllegalArgumentException(String.format("illegal zoom percentage %s - should be greater than zero", percentage));
145+
}
146+
JavascriptExecutor jse = (JavascriptExecutor) driver;
147+
if (isFirefox()) {
148+
jse.executeScript("document.body.style.MozTransform = 'scale(" + (percentage / 100f) + ")';");
149+
} else {
150+
jse.executeScript("document.body.style.zoom = '" + percentage + "%'");
151+
}
152+
}
153+
154+
}
155+
115156
private String getZoomScript() {
116157
if (isFirefox()) {
117158
return "document.body.style.MozTransform";
@@ -153,39 +194,23 @@ private long retrievePageWidth() {
153194
}
154195
}
155196

156-
157197
public Dimension retrievePageSize() {
158198
return new Dimension((int) retrievePageWidth(), (int) retrievePageHeight());
159199
}
160200

161-
public void setResolution(Dimension resolution) {
162-
driver.manage().window().setSize(resolution);
163-
}
164-
165201
public Dimension getResolution() {
166202
if (isAppiumContext() && getApp() == null) {
167203
String resolution = ((RemoteWebDriver) driver).getCapabilities().getCapability("deviceScreenSize").toString();
168-
int width = Integer.parseInt(resolution.split("x")[0]);
169-
int height = Integer.parseInt(resolution.split("x")[1]);
204+
int width = parseInt(resolution.split("x")[0]);
205+
int height = parseInt(resolution.split("x")[1]);
170206

171207
return new Dimension(width, height);
172208
} else {
173209
return driver.manage().window().getSize();
174210
}
175211
}
176212

177-
public void setZoom(int percentage) {
178-
if (!isAppiumContext()) {
179-
if (percentage <= 0) {
180-
throw new IllegalArgumentException(String.format("illegal zoom percentage %s - should be greater than zero", percentage));
181-
}
182-
JavascriptExecutor jse = (JavascriptExecutor) driver;
183-
if (isFirefox()) {
184-
jse.executeScript("document.body.style.MozTransform = 'scale(" + (percentage / 100f) + ")';");
185-
} else {
186-
jse.executeScript("document.body.style.zoom = '" + percentage + "%'");
187-
}
188-
}
189-
213+
public void setResolution(Dimension resolution) {
214+
driver.manage().window().setSize(resolution);
190215
}
191216
}

src/main/java/net/itarray/automotion/internal/ResponsiveUIValidatorBase.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,17 @@
1212
import org.openqa.selenium.Dimension;
1313

1414
import java.awt.*;
15-
import java.io.BufferedWriter;
16-
import java.io.File;
17-
import java.io.FileOutputStream;
18-
import java.io.IOException;
19-
import java.io.OutputStreamWriter;
20-
import java.io.Writer;
15+
import java.io.*;
2116
import java.nio.charset.StandardCharsets;
2217

2318
import static net.itarray.automotion.validation.Constants.*;
2419

2520
public abstract class ResponsiveUIValidatorBase {
2621

27-
private final Errors errors;
2822
protected final UIElement page;
29-
private final long startTime;
3023
protected final UISnapshot snapshot;
24+
private final Errors errors;
25+
private final long startTime;
3126
private final DriverFacade driver;
3227
private final double zoomFactor;
3328
private DrawableScreenshot drawableScreenshot;
@@ -47,12 +42,12 @@ protected ResponsiveUIValidatorBase(UISnapshot snapshot) {
4742

4843

4944
public DrawableScreenshot getDrawableScreenshot() {
50-
if (drawableScreenshot == null) {
45+
if (drawableScreenshot == null && this.errors.hasMessages()) {
5146
File screenshotName = snapshot.takeScreenshot();
5247
Vector extend = driver.getExtend(screenshotName);
5348
this.drawableScreenshot = new DrawableScreenshot(extend, getTransform(), getDrawingConfiguration(), getNameOfToBeValidated(), screenshotName);
5449
}
55-
if (isWithReport() && !rootElementDrawn) {
50+
if (isWithReport() && !rootElementDrawn && this.errors.hasMessages()) {
5651
rootElementDrawn = true;
5752
drawRootElement();
5853
}
@@ -104,7 +99,7 @@ public ResponsiveUIValidatorBase dontDrawMap() {
10499
public boolean validate() {
105100

106101
//if (errors.hasMessages()) {
107-
compileValidationReport();
102+
compileValidationReport();
108103
//}
109104

110105
return !errors.hasMessages();
@@ -127,7 +122,9 @@ public boolean isPixels() {
127122
}
128123

129124
@Override
130-
public Scalar getTolerance() { return tolerance; }
125+
public Scalar getTolerance() {
126+
return tolerance;
127+
}
131128

132129
@Override
133130
public void add(String message) {
@@ -136,29 +133,41 @@ public void add(String message) {
136133

137134
@Override
138135
public void draw(UIElement element) {
139-
if (isWithReport()) {
140-
getDrawableScreenshot().draw(element);
136+
DrawableScreenshot drawableScreenshot = getDrawableScreenshot();
137+
if (drawableScreenshot != null) {
138+
if (isWithReport()) {
139+
getDrawableScreenshot().draw(element);
140+
}
141141
}
142142
}
143143

144144
@Override
145145
public void drawRoot(UIElement element) {
146-
if (isWithReport()) {
147-
getDrawableScreenshot().drawRoot(element);
146+
DrawableScreenshot drawableScreenshot = getDrawableScreenshot();
147+
if (drawableScreenshot != null) {
148+
if (isWithReport()) {
149+
getDrawableScreenshot().drawRoot(element);
150+
}
148151
}
149152
}
150153

151154
@Override
152155
public void drawHorizontalLine(Vector onLine) {
153-
if (isWithReport()) {
154-
getDrawableScreenshot().drawHorizontalLine(onLine.getY());
156+
DrawableScreenshot drawableScreenshot = getDrawableScreenshot();
157+
if (drawableScreenshot != null) {
158+
if (isWithReport()) {
159+
getDrawableScreenshot().drawHorizontalLine(onLine.getY());
160+
}
155161
}
156162
}
157163

158164
@Override
159165
public void drawVerticalLine(Vector onLine) {
160-
if (isWithReport()) {
161-
getDrawableScreenshot().drawVerticalLine(onLine.getX());
166+
DrawableScreenshot drawableScreenshot = getDrawableScreenshot();
167+
if (drawableScreenshot != null) {
168+
if (isWithReport()) {
169+
getDrawableScreenshot().drawVerticalLine(onLine.getX());
170+
}
162171
}
163172
}
164173

@@ -173,8 +182,12 @@ public int errorCount() {
173182
protected abstract String getNameOfToBeValidated();
174183

175184
private void compileValidationReport() {
176-
if (isWithReport()) {
177-
getDrawableScreenshot().saveDrawing();
185+
DrawableScreenshot drawableScreenshot = getDrawableScreenshot();
186+
187+
if (drawableScreenshot != null) {
188+
if (isWithReport()) {
189+
getDrawableScreenshot().saveDrawing();
190+
}
178191
}
179192

180193
if (isWithReport()) {
@@ -224,8 +237,8 @@ private void writeResults(DrawableScreenshot drawableScreenshot) {
224237
jsonResults.put(ROOT_ELEMENT, rootDetails);
225238
jsonResults.put(TIME_EXECUTION, String.valueOf(System.currentTimeMillis() - startTime) + " milliseconds");
226239
jsonResults.put(ELEMENT_NAME, getNameOfToBeValidated());
227-
jsonResults.put(SCREENSHOT, drawableScreenshot.getScreenshotName().getName());
228-
jsonResults.put(DRAWINGS, drawableScreenshot.getDrawingsOutput().getName());
240+
jsonResults.put(SCREENSHOT, drawableScreenshot != null ? drawableScreenshot.getScreenshotName().getName() : "");
241+
jsonResults.put(DRAWINGS, drawableScreenshot != null ? drawableScreenshot.getDrawingsOutput().getName() : "");
229242

230243
long ms = System.currentTimeMillis();
231244
String uuid = Helper.getGeneratedStringWithLength(7);

src/main/java/net/itarray/automotion/tools/driver/WebDriverFactory.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,34 @@ public WebDriverFactory() {
4242

4343
private static void setChromeDriver() {
4444
Platform platform = Platform.getCurrent();
45-
String chromeBinary = "src/main/resources/drivers/chromedriver"
46-
+ (platform.toString().toUpperCase().contains("WIN") ? ".exe" : "");
47-
System.setProperty("webdriver.chrome.driver", chromeBinary);
45+
if (System.getProperty("webdriver.chrome.driver") == null || System.getProperty("webdriver.chrome.driver").isEmpty()) {
46+
String chromeBinary = "src/main/resources/drivers/chromedriver"
47+
+ (platform.toString().toUpperCase().contains("WIN") ? ".exe" : "");
48+
System.setProperty("webdriver.chrome.driver", chromeBinary);
49+
}
4850
}
4951

5052
private static void setGeckoDriver() {
5153
Platform platform = Platform.getCurrent();
52-
String geckoBinary = "src/main/resources/drivers/geckodriver"
53-
+ (platform.toString().toUpperCase().contains("WIN") ? ".exe" : "");
54-
System.setProperty("webdriver.gecko.driver", geckoBinary);
54+
if (System.getProperty("webdriver.gecko.driver") == null || System.getProperty("webdriver.gecko.driver").isEmpty()) {
55+
String geckoBinary = "src/main/resources/drivers/geckodriver"
56+
+ (platform.toString().toUpperCase().contains("WIN") ? ".exe" : "");
57+
System.setProperty("webdriver.gecko.driver", geckoBinary);
58+
}
5559
}
5660

5761
private static void setIEDriver() {
58-
String ieBinary = "src/main/resources/drivers/IEDriverServer.exe";
59-
System.setProperty("webdriver.ie.driver", ieBinary);
62+
if (System.getProperty("webdriver.ie.driver") == null || System.getProperty("webdriver.ie.driver").isEmpty()) {
63+
String ieBinary = "src/main/resources/drivers/IEDriverServer.exe";
64+
System.setProperty("webdriver.ie.driver", ieBinary);
65+
}
6066
}
6167

6268
private static void setEdgeDriver() {
63-
String edgeBinary = "src/main/resources/drivers/MicrosoftWebDriver.exe";
64-
System.setProperty("webdriver.edge.driver", edgeBinary);
69+
if (System.getProperty("webdriver.edge.driver") == null || System.getProperty("webdriver.edge.driver").isEmpty()) {
70+
String edgeBinary = "src/main/resources/drivers/MicrosoftWebDriver.exe";
71+
System.setProperty("webdriver.edge.driver", edgeBinary);
72+
}
6573
}
6674

6775
public WebDriver getDriver() {
@@ -114,6 +122,9 @@ private WebDriver getLocalWebDriver() {
114122
options.addArguments("--start-maximized");
115123
options.addArguments("--kiosk");
116124
options.addArguments("--disable-notifications");
125+
options.setExperimentalOption("useAutomationExtension", false);
126+
options.addArguments("disable-infobars");
127+
117128
webDriver = new ChromeDriver(options);
118129
} else if (isSafari()) {
119130
webDriver = new SafariDriver();

0 commit comments

Comments
 (0)