44import io .appium .java_client .android .AndroidDriver ;
55import io .appium .java_client .ios .IOSDriver ;
66import net .itarray .automotion .internal .geometry .Vector ;
7+ import net .itarray .automotion .tools .general .SystemHelper ;
78import org .openqa .selenium .*;
89import org .openqa .selenium .chrome .ChromeDriver ;
910import org .openqa .selenium .firefox .FirefoxDriver ;
1011import org .openqa .selenium .phantomjs .PhantomJSDriver ;
1112import 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
1317import javax .imageio .ImageIO ;
1418import java .awt .image .BufferedImage ;
1721import java .io .IOException ;
1822import 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
2228public 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}
0 commit comments