11package net .itarray .automotion .internal ;
22
33import io .appium .java_client .AppiumDriver ;
4+ import io .appium .java_client .android .AndroidDriver ;
45import org .openqa .selenium .*;
56import org .openqa .selenium .remote .RemoteWebDriver ;
67
@@ -33,6 +34,31 @@ public void takeScreenshot(File file) {
3334 }
3435 }
3536
37+ public WebDriver getDriver () {
38+ return driver ;
39+ }
40+
41+ public boolean isAppiumAndroidContext () {
42+ if ((driver instanceof AndroidDriver )) {
43+ return true ;
44+ }
45+ return false ;
46+ }
47+
48+ public boolean isAppiumIOSContext () {
49+ if ((driver instanceof AndroidDriver )) {
50+ return true ;
51+ }
52+ return false ;
53+ }
54+
55+ public boolean isAppiumContext () {
56+ if ((driver instanceof AppiumDriver )) {
57+ return true ;
58+ }
59+ return false ;
60+ }
61+
3662 public boolean isAppiumWebContext () {
3763 if (!(driver instanceof AppiumDriver )) {
3864 return false ;
@@ -52,11 +78,15 @@ public Object executeScript(String script) {
5278 }
5379
5480 public String getZoom () {
55- String zoom = (String ) executeScript (getZoomScript ());
56- if (zoom == null || zoom .equals ("" )) {
57- zoom = "100%" ;
81+ if (!isAppiumContext ()) {
82+ String zoom = (String ) executeScript (getZoomScript ());
83+ if (zoom == null || zoom .equals ("" )) {
84+ zoom = "100%" ;
85+ }
86+ return zoom ;
87+ } else {
88+ return "100%" ;
5889 }
59- return zoom ;
6090 }
6191
6292 private String getZoomScript () {
@@ -68,34 +98,34 @@ private String getZoomScript() {
6898 }
6999
70100 private long retrievePageHeight () {
71- if (!isMobile ()) {
101+ if (!isAppiumAndroidContext ()) {
72102 if (getZoom ().equals ("100%" )) {
73- return (long ) executeScript ("if (self.innerHeight ) {return self.innerHeight ;} if (document.documentElement && document.documentElement.clientHeight) {return document.documentElement.clientHeight;}if (document.body) {return document.body.clientHeight;}" );
103+ return (long ) executeScript ("if (self.outerHeight ) {return self.outerHeight ;} if (document.documentElement && document.documentElement.clientHeight) {return document.documentElement.clientHeight;}if (document.body) {return document.body.clientHeight;}" );
74104 } else {
75105 return (long ) executeScript ("return document.getElementsByTagName('body')[0].offsetHeight" );
76106 }
77107 } else {
78- if (isAppiumNativeMobileContext () || isIOS ()) {
108+ if (isAppiumNativeMobileContext () || isAppiumIOSContext ()) {
79109 return driver .manage ().window ().getSize ().getHeight ();
80110 } else {
81- return (long ) executeScript ("if (self.innerHeight ) {return self.innerHeight ;} if (document.documentElement && document.documentElement.clientHeight) {return document.documentElement.clientHeight;}if (document.body) {return document.body.clientHeight;}" );
111+ return (long ) executeScript ("if (self.outerHeight ) {return self.outerHeight ;} if (document.documentElement && document.documentElement.clientHeight) {return document.documentElement.clientHeight;}if (document.body) {return document.body.clientHeight;}" );
82112 }
83113 }
84114 }
85115
86116 private long retrievePageWidth () {
87- if (!isMobile ()) {
117+ if (!isAppiumAndroidContext ()) {
88118 if (getZoom ().equals ("100%" )) {
89- String script = "if (self.innerWidth ) {return self.innerWidth ;} if (document.documentElement && document.documentElement.clientWidth) {return document.documentElement.clientWidth;}if (document.body) {return document.body.clientWidth;}" ;
119+ String script = "if (self.outerWidth ) {return self.outerWidth ;} if (document.documentElement && document.documentElement.clientWidth) {return document.documentElement.clientWidth;}if (document.body) {return document.body.clientWidth;}" ;
90120 return (long ) executeScript (script );
91121 } else {
92122 return (long ) executeScript ("return document.getElementsByTagName('body')[0].offsetWidth" );
93123 }
94124 } else {
95- if (isAppiumNativeMobileContext () || isIOS ()) {
125+ if (isAppiumNativeMobileContext () || isAppiumIOSContext ()) {
96126 return driver .manage ().window ().getSize ().getWidth ();
97127 } else {
98- return (long ) executeScript ("if (self.innerWidth ) {return self.innerWidth ;} if (document.documentElement && document.documentElement.clientWidth) {return document.documentElement.clientWidth;}if (document.body) {return document.body.clientWidth;}" );
128+ return (long ) executeScript ("if (self.outerWidth ) {return self.outerWidth ;} if (document.documentElement && document.documentElement.clientWidth) {return document.documentElement.clientWidth;}if (document.body) {return document.body.clientWidth;}" );
99129 }
100130 }
101131 }
@@ -110,7 +140,7 @@ public void setResolution(Dimension resolution) {
110140 }
111141
112142 public Dimension getResolution () {
113- if (isMobile () && getApp () == null ) {
143+ if (! isAppiumContext () && getApp () == null ) {
114144 String resolution = ((RemoteWebDriver ) driver ).getCapabilities ().getCapability ("deviceScreenSize" ).toString ();
115145 int width = Integer .parseInt (resolution .split ("x" )[0 ]);
116146 int height = Integer .parseInt (resolution .split ("x" )[1 ]);
@@ -122,14 +152,16 @@ public Dimension getResolution() {
122152 }
123153
124154 public void setZoom (int percentage ) {
125- if (percentage <= 0 ) {
126- throw new IllegalArgumentException (String .format ("illegal zoom percentage %s - should be greater than zero" , percentage ));
127- }
128- JavascriptExecutor jse = (JavascriptExecutor ) driver ;
129- if (isFirefox ()) {
130- jse .executeScript ("document.body.style.MozTransform = 'scale(" + (percentage / 100f ) + ")';" );
131- } else {
132- jse .executeScript ("document.body.style.zoom = '" + percentage + "%'" );
155+ if (!isAppiumContext ()) {
156+ if (percentage <= 0 ) {
157+ throw new IllegalArgumentException (String .format ("illegal zoom percentage %s - should be greater than zero" , percentage ));
158+ }
159+ JavascriptExecutor jse = (JavascriptExecutor ) driver ;
160+ if (isFirefox ()) {
161+ jse .executeScript ("document.body.style.MozTransform = 'scale(" + (percentage / 100f ) + ")';" );
162+ } else {
163+ jse .executeScript ("document.body.style.zoom = '" + percentage + "%'" );
164+ }
133165 }
134166
135167 }
0 commit comments