3535import org .openqa .selenium .testing .Ignore ;
3636import org .openqa .selenium .testing .JupiterTestBase ;
3737import org .openqa .selenium .testing .NeedsFreshDriver ;
38+ import org .openqa .selenium .testing .NeedsSecureServer ;
3839
40+ @ NeedsSecureServer
3941class EmulationTest extends JupiterTestBase {
4042 private static final String GET_GEOLOCATION_PERMISSION =
4143 "() => {return navigator.permissions.query({ name: 'geolocation' })"
@@ -44,14 +46,79 @@ class EmulationTest extends JupiterTestBase {
4446 Object getBrowserGeolocation (WebDriver driver , String userContext , String origin ) {
4547 JavascriptExecutor executor = (JavascriptExecutor ) driver ;
4648
49+ System .out .println (
50+ "DEBUG: getBrowserGeolocation called with origin: "
51+ + origin
52+ + ", userContext: "
53+ + userContext );
54+
4755 Permission permission = new Permission (driver );
48- permission .setPermission (
49- Map .of ("name" , "geolocation" ), PermissionState .GRANTED , origin , userContext );
5056
57+ // Add delay to ensure page is fully loaded
58+ try {
59+ Thread .sleep (500 );
60+ } catch (InterruptedException e ) {
61+ Thread .currentThread ().interrupt ();
62+ }
63+
64+ // Check if we're in a secure context
65+ Object isSecureContext = executor .executeScript ("return window.isSecureContext;" );
66+ System .out .println ("DEBUG: isSecureContext: " + isSecureContext );
67+
68+ // Check current URL
69+ String currentUrl = driver .getCurrentUrl ();
70+ System .out .println ("DEBUG: Current URL: " + currentUrl );
71+
72+ // Try to set permission with retry logic for CI environments
73+ int maxRetries = 5 ;
74+ int retryCount = 0 ;
75+ boolean permissionSet = false ;
76+
77+ while (!permissionSet && retryCount < maxRetries ) {
78+ try {
79+ System .out .println ("DEBUG: Attempt " + (retryCount + 1 ) + " to set geolocation permission" );
80+ permission .setPermission (
81+ Map .of ("name" , "geolocation" ), PermissionState .GRANTED , origin , userContext );
82+ permissionSet = true ;
83+ System .out .println ("DEBUG: Permission set successfully" );
84+ } catch (Exception e ) {
85+ retryCount ++;
86+ System .out .println (
87+ "DEBUG: Permission setting failed on attempt " + retryCount + ": " + e .getMessage ());
88+ if (retryCount >= maxRetries ) {
89+ // If permission setting fails after retries, continue anyway
90+ // as some CI environments may have different permission handling
91+ System .out .println (
92+ "Warning: Could not set geolocation permission after "
93+ + maxRetries
94+ + " attempts: "
95+ + e .getMessage ());
96+ break ;
97+ }
98+ try {
99+ Thread .sleep (200 ); // Longer delay before retry
100+ } catch (InterruptedException ie ) {
101+ Thread .currentThread ().interrupt ();
102+ break ;
103+ }
104+ }
105+ }
106+
107+ // Check permission state
108+ try {
109+ Object permissionState = executor .executeScript (GET_GEOLOCATION_PERMISSION );
110+ System .out .println ("DEBUG: Geolocation permission state: " + permissionState );
111+ } catch (Exception e ) {
112+ System .out .println ("DEBUG: Could not check permission state: " + e .getMessage ());
113+ }
114+
115+ System .out .println ("DEBUG: Executing geolocation script" );
51116 return executor .executeAsyncScript (
52117 "const callback = arguments[arguments.length - 1];\n "
118+ + " console.log('Starting geolocation request');\n "
53119 + " navigator.geolocation.getCurrentPosition(\n "
54120 + " position => {\n "
121+ + " console.log('Geolocation success:', position);\n "
55122 + " const coords = position.coords;\n "
56123 + " callback({\n "
57124 + " latitude: coords.latitude,\n "
@@ -65,126 +132,218 @@ Object getBrowserGeolocation(WebDriver driver, String userContext, String origin
65132 + " });\n "
66133 + " },\n "
67134 + " error => {\n "
135+ + " console.log('Geolocation error:', error);\n "
68136 + " callback({ error: error.message });\n "
69- + " }\n "
137+ + " },\n "
138+ + " { enableHighAccuracy: false, timeout: 10000, maximumAge: 0 }\n "
70139 + " );" );
71140 }
72141
73142 @ Test
74143 @ NeedsFreshDriver
75144 void getGeolocationOverrideWithCoordinatesInContext () {
145+ System .out .println ("DEBUG: Starting getGeolocationOverrideWithCoordinatesInContext test" );
146+
76147 BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
77148 String contextId = context .getId ();
149+ System .out .println ("DEBUG: Created context with ID: " + contextId );
150+
151+ // Use secure URL for geolocation (now guaranteed to be available)
152+ String url = appServer .whereIsSecure ("blank.html" );
153+ System .out .println ("DEBUG: Using secure URL: " + url );
78154
79- String url = appServer .whereIs ("blank.html" );
80155 context .navigate (url , ReadinessState .COMPLETE );
81156 driver .switchTo ().window (context .getId ());
82157
158+ // Wait for page to be fully loaded
159+ try {
160+ Thread .sleep (1000 );
161+ } catch (InterruptedException e ) {
162+ Thread .currentThread ().interrupt ();
163+ }
164+
83165 String origin =
84166 (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
167+ System .out .println ("DEBUG: Origin: " + origin );
85168
86169 Emulation emul = new Emulation (driver );
87170 GeolocationCoordinates coords =
88171 new GeolocationCoordinates (37.7749 , -122.4194 , 10.0 , null , null , null );
172+ System .out .println (
173+ "DEBUG: Setting geolocation override with coordinates: "
174+ + coords .getLatitude ()
175+ + ", "
176+ + coords .getLongitude ());
177+
89178 emul .setGeolocationOverride (
90179 new SetGeolocationOverrideParameters (coords , null , List .of (contextId ), null ));
180+ System .out .println ("DEBUG: Geolocation override set" );
91181
92182 Object result = getBrowserGeolocation (driver , null , origin );
93183 Map <String , Object > r = ((Map <String , Object >) result );
94184
95- System .out .println (r );
185+ System .out .println ("DEBUG: Geolocation result: " + r );
96186
97- assert !r .containsKey ("error" );
187+ assert !r .containsKey ("error" ) : "Geolocation failed with error: " + r . get ( "error" ) ;
98188
99189 double latitude = ((Number ) r .get ("latitude" )).doubleValue ();
100190 double longitude = ((Number ) r .get ("longitude" )).doubleValue ();
101191 double accuracy = ((Number ) r .get ("accuracy" )).doubleValue ();
102192
103- assert abs (latitude - coords .getLatitude ()) < 0.0001 ;
104- assert abs (longitude - coords .getLongitude ()) < 0.0001 ;
105- assert abs (accuracy - coords .getAccuracy ()) < 0.0001 ;
193+ System .out .println (
194+ "DEBUG: Checking coordinates - expected: "
195+ + coords .getLatitude ()
196+ + ", "
197+ + coords .getLongitude ()
198+ + ", actual: "
199+ + latitude
200+ + ", "
201+ + longitude );
202+
203+ assert abs (latitude - coords .getLatitude ()) < 0.0001
204+ : "Latitude mismatch: expected " + coords .getLatitude () + ", got " + latitude ;
205+ assert abs (longitude - coords .getLongitude ()) < 0.0001
206+ : "Longitude mismatch: expected " + coords .getLongitude () + ", got " + longitude ;
207+ assert abs (accuracy - coords .getAccuracy ()) < 0.0001
208+ : "Accuracy mismatch: expected " + coords .getAccuracy () + ", got " + accuracy ;
209+
210+ System .out .println ("DEBUG: Test completed successfully" );
106211 }
107212
108213 @ Test
109214 void canSetGeolocationOverrideWithMultipleUserContexts () {
215+ System .out .println ("DEBUG: Starting canSetGeolocationOverrideWithMultipleUserContexts test" );
216+
110217 Browser browser = new Browser (driver );
111218 String userContext1 = browser .createUserContext ();
112219 String userContext2 = browser .createUserContext ();
220+ System .out .println ("DEBUG: Created user contexts: " + userContext1 + ", " + userContext2 );
113221
114222 BrowsingContext context1 =
115223 new BrowsingContext (
116224 driver , new CreateContextParameters (WindowType .TAB ).userContext (userContext1 ));
117225 BrowsingContext context2 =
118226 new BrowsingContext (
119227 driver , new CreateContextParameters (WindowType .TAB ).userContext (userContext2 ));
228+ System .out .println (
229+ "DEBUG: Created browsing contexts: " + context1 .getId () + ", " + context2 .getId ());
120230
121231 GeolocationCoordinates coords =
122- new GeolocationCoordinates (45.5 , -122.4194 , 10.0 , null , null , null , null );
232+ new GeolocationCoordinates (45.5 , -122.4194 , 10.0 , null , null , null );
123233
124234 Emulation emulation = new Emulation (driver );
235+ System .out .println ("DEBUG: Setting geolocation override for multiple user contexts" );
125236 emulation .setGeolocationOverride (
126237 new SetGeolocationOverrideParameters (
127238 coords , null , null , List .of (userContext1 , userContext2 )));
128239
129240 // Test first user context
241+ System .out .println ("DEBUG: Testing first user context" );
130242 driver .switchTo ().window (context1 .getId ());
131- context1 .navigate (appServer .whereIs ("blank.html" ), ReadinessState .COMPLETE );
243+
244+ String url1 = appServer .whereIsSecure ("blank.html" );
245+ System .out .println ("DEBUG: Using secure URL for context1: " + url1 );
246+
247+ context1 .navigate (url1 , ReadinessState .COMPLETE );
248+
249+ // Wait for page to be fully loaded
250+ try {
251+ Thread .sleep (1000 );
252+ } catch (InterruptedException e ) {
253+ Thread .currentThread ().interrupt ();
254+ }
255+
132256 String origin1 =
133257 (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
258+ System .out .println ("DEBUG: Origin1: " + origin1 );
134259
135260 Map <String , Object > r =
136261 (Map <String , Object >) getBrowserGeolocation (driver , userContext1 , origin1 );
137262
138- assert !r .containsKey ("error" );
263+ System .out .println ("DEBUG: Context1 result: " + r );
264+ assert !r .containsKey ("error" ) : "Context1 geolocation failed with error: " + r .get ("error" );
139265
140266 double latitude1 = ((Number ) r .get ("latitude" )).doubleValue ();
141267 double longitude1 = ((Number ) r .get ("longitude" )).doubleValue ();
142268 double accuracy1 = ((Number ) r .get ("accuracy" )).doubleValue ();
143269
144- assert abs (latitude1 - coords .getLatitude ()) < 0.0001 ;
145- assert abs (longitude1 - coords .getLongitude ()) < 0.0001 ;
146- assert abs (accuracy1 - coords .getAccuracy ()) < 0.0001 ;
270+ assert abs (latitude1 - coords .getLatitude ()) < 0.0001 : "Context1 latitude mismatch" ;
271+ assert abs (longitude1 - coords .getLongitude ()) < 0.0001 : "Context1 longitude mismatch" ;
272+ assert abs (accuracy1 - coords .getAccuracy ()) < 0.0001 : "Context1 accuracy mismatch" ;
147273
148274 // Test second user context
275+ System .out .println ("DEBUG: Testing second user context" );
149276 driver .switchTo ().window (context2 .getId ());
150- context2 .navigate (appServer .whereIs ("blank.html" ), ReadinessState .COMPLETE );
277+
278+ String url2 = appServer .whereIsSecure ("blank.html" );
279+ System .out .println ("DEBUG: Using secure URL for context2: " + url2 );
280+
281+ context2 .navigate (url2 , ReadinessState .COMPLETE );
282+
283+ // Wait for page to be fully loaded
284+ try {
285+ Thread .sleep (1000 );
286+ } catch (InterruptedException e ) {
287+ Thread .currentThread ().interrupt ();
288+ }
289+
151290 String origin2 =
152291 (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
292+ System .out .println ("DEBUG: Origin2: " + origin2 );
293+
153294 Map <String , Object > r2 =
154295 (Map <String , Object >) getBrowserGeolocation (driver , userContext2 , origin2 );
155296
156- assert !r2 .containsKey ("error" );
297+ System .out .println ("DEBUG: Context2 result: " + r2 );
298+ assert !r2 .containsKey ("error" ) : "Context2 geolocation failed with error: " + r2 .get ("error" );
157299
158300 double latitude2 = ((Number ) r2 .get ("latitude" )).doubleValue ();
159301 double longitude2 = ((Number ) r2 .get ("longitude" )).doubleValue ();
160302 double accuracy2 = ((Number ) r2 .get ("accuracy" )).doubleValue ();
161303
162- assert abs (latitude2 - coords .getLatitude ()) < 0.0001 ;
163- assert abs (longitude2 - coords .getLongitude ()) < 0.0001 ;
164- assert abs (accuracy2 - coords .getAccuracy ()) < 0.0001 ;
304+ assert abs (latitude2 - coords .getLatitude ()) < 0.0001 : "Context2 latitude mismatch" ;
305+ assert abs (longitude2 - coords .getLongitude ()) < 0.0001 : "Context2 longitude mismatch" ;
306+ assert abs (accuracy2 - coords .getAccuracy ()) < 0.0001 : "Context2 accuracy mismatch" ;
165307
308+ System .out .println ("DEBUG: Cleaning up contexts" );
166309 context1 .close ();
167310 context2 .close ();
168311 browser .removeUserContext (userContext1 );
169312 browser .removeUserContext (userContext2 );
313+
314+ System .out .println ("DEBUG: Multiple user contexts test completed successfully" );
170315 }
171316
172317 @ Test
173318 @ Ignore (FIREFOX )
174319 void canSetGeolocationOverrideWithError () {
320+ System .out .println ("DEBUG: Starting canSetGeolocationOverrideWithError test" );
321+
175322 BrowsingContext context = new BrowsingContext (driver , WindowType .TAB );
176323 String contextId = context .getId ();
324+ System .out .println ("DEBUG: Created context with ID: " + contextId );
325+
326+ String url = appServer .whereIsSecure ("blank.html" );
327+ System .out .println ("DEBUG: Using secure URL: " + url );
177328
178- String url = appServer .whereIs ("blank.html" );
179329 context .navigate (url , ReadinessState .COMPLETE );
180330
181331 // Switch to the new context
182332 driver .switchTo ().window (contextId );
183333
334+ // Wait for page to be fully loaded
335+ try {
336+ Thread .sleep (1000 );
337+ } catch (InterruptedException e ) {
338+ Thread .currentThread ().interrupt ();
339+ }
340+
184341 String origin =
185342 (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
343+ System .out .println ("DEBUG: Origin: " + origin );
186344
187345 GeolocationPositionError error = new GeolocationPositionError ();
346+ System .out .println ("DEBUG: Setting geolocation override with error" );
188347
189348 Emulation emul = new Emulation (driver );
190349 emul .setGeolocationOverride (
@@ -193,8 +352,10 @@ void canSetGeolocationOverrideWithError() {
193352 Object result = getBrowserGeolocation (driver , null , origin );
194353 Map <String , Object > r = ((Map <String , Object >) result );
195354
196- assert r .containsKey ("error" );
355+ System .out .println ("DEBUG: Error test result: " + r );
356+ assert r .containsKey ("error" ) : "Expected geolocation to fail with error, but got: " + r ;
197357
198358 context .close ();
359+ System .out .println ("DEBUG: Error test completed successfully" );
199360 }
200361}
0 commit comments