Skip to content

Commit 78a4c61

Browse files
committed
[fullscreen_screenshot_web] - implemented the taking of Fullscreen screenshots
1 parent 47b092a commit 78a4c61

File tree

6 files changed

+110
-82
lines changed

6 files changed

+110
-82
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-rc2</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>

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

Lines changed: 41 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,22 @@ 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+
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportRetina(100, 0, 0, (SystemHelper.isRetinaDisplay()) ? 2 : 1)).takeScreenshot(driver);
44+
try {
45+
ImageIO.write(screenshot.getImage(), "PNG", file);
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
} else {
50+
byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
51+
52+
try (OutputStream stream = new FileOutputStream(file)) {
53+
stream.write(bytes);
54+
} catch (IOException e) {
55+
throw new RuntimeException(e);
56+
}
4157
}
4258
}
4359

@@ -112,6 +128,21 @@ public String getZoom() {
112128
}
113129
}
114130

131+
public void setZoom(int percentage) {
132+
if (!isAppiumContext()) {
133+
if (percentage <= 0) {
134+
throw new IllegalArgumentException(String.format("illegal zoom percentage %s - should be greater than zero", percentage));
135+
}
136+
JavascriptExecutor jse = (JavascriptExecutor) driver;
137+
if (isFirefox()) {
138+
jse.executeScript("document.body.style.MozTransform = 'scale(" + (percentage / 100f) + ")';");
139+
} else {
140+
jse.executeScript("document.body.style.zoom = '" + percentage + "%'");
141+
}
142+
}
143+
144+
}
145+
115146
private String getZoomScript() {
116147
if (isFirefox()) {
117148
return "document.body.style.MozTransform";
@@ -153,39 +184,23 @@ private long retrievePageWidth() {
153184
}
154185
}
155186

156-
157187
public Dimension retrievePageSize() {
158188
return new Dimension((int) retrievePageWidth(), (int) retrievePageHeight());
159189
}
160190

161-
public void setResolution(Dimension resolution) {
162-
driver.manage().window().setSize(resolution);
163-
}
164-
165191
public Dimension getResolution() {
166192
if (isAppiumContext() && getApp() == null) {
167193
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]);
194+
int width = parseInt(resolution.split("x")[0]);
195+
int height = parseInt(resolution.split("x")[1]);
170196

171197
return new Dimension(width, height);
172198
} else {
173199
return driver.manage().window().getSize();
174200
}
175201
}
176202

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-
203+
public void setResolution(Dimension resolution) {
204+
driver.manage().window().setSize(resolution);
190205
}
191206
}

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)