3939
4040@ NeedsSecureServer
4141class EmulationTest extends JupiterTestBase {
42- private static final String GET_GEOLOCATION_PERMISSION =
43- "() => {return navigator.permissions.query({ name: 'geolocation' })"
44- + ".then(val => val.state, err => err.message)}" ;
45-
4642 Object getBrowserGeolocation (WebDriver driver , String userContext , String origin ) {
4743 JavascriptExecutor executor = (JavascriptExecutor ) driver ;
48-
49- System .out .println (
50- "DEBUG: getBrowserGeolocation called with origin: "
51- + origin
52- + ", userContext: "
53- + userContext );
54-
5544 Permission permission = new Permission (driver );
5645
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" );
46+ permission .setPermission (
47+ Map .of ("name" , "geolocation" ), PermissionState .GRANTED , origin , userContext );
48+
11649 return executor .executeAsyncScript (
11750 "const callback = arguments[arguments.length - 1];\n "
118- + " console.log('Starting geolocation request');\n "
11951 + " navigator.geolocation.getCurrentPosition(\n "
12052 + " position => {\n "
121- + " console.log('Geolocation success:', position);\n "
12253 + " const coords = position.coords;\n "
12354 + " callback({\n "
12455 + " latitude: coords.latitude,\n "
@@ -141,126 +72,71 @@ Object getBrowserGeolocation(WebDriver driver, String userContext, String origin
14172
14273 @ Test
14374 @ NeedsFreshDriver
144- void getGeolocationOverrideWithCoordinatesInContext () {
145- System .out .println ("DEBUG: Starting getGeolocationOverrideWithCoordinatesInContext test" );
146-
75+ void canSetGeolocationOverrideWithCoordinatesInContext () {
14776 BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
14877 String contextId = context .getId ();
149- System .out .println ("DEBUG: Created context with ID: " + contextId );
15078
151- // Use secure URL for geolocation (now guaranteed to be available)
15279 String url = appServer .whereIsSecure ("blank.html" );
153- System .out .println ("DEBUG: Using secure URL: " + url );
154-
15580 context .navigate (url , ReadinessState .COMPLETE );
15681 driver .switchTo ().window (context .getId ());
15782
158- // Wait for page to be fully loaded
159- try {
160- Thread .sleep (1000 );
161- } catch (InterruptedException e ) {
162- Thread .currentThread ().interrupt ();
163- }
164-
16583 String origin =
16684 (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
167- System .out .println ("DEBUG: Origin: " + origin );
16885
16986 Emulation emul = new Emulation (driver );
17087 GeolocationCoordinates coords =
17188 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-
17889 emul .setGeolocationOverride (
17990 new SetGeolocationOverrideParameters (coords , null , List .of (contextId ), null ));
180- System .out .println ("DEBUG: Geolocation override set" );
18191
18292 Object result = getBrowserGeolocation (driver , null , origin );
18393 Map <String , Object > r = ((Map <String , Object >) result );
18494
185- System .out .println ("DEBUG: Geolocation result: " + r );
186-
18795 assert !r .containsKey ("error" ) : "Geolocation failed with error: " + r .get ("error" );
18896
18997 double latitude = ((Number ) r .get ("latitude" )).doubleValue ();
19098 double longitude = ((Number ) r .get ("longitude" )).doubleValue ();
19199 double accuracy = ((Number ) r .get ("accuracy" )).doubleValue ();
192100
193- System .out .println (
194- "DEBUG: Checking coordinates - expected: "
195- + coords .getLatitude ()
196- + ", "
197- + coords .getLongitude ()
198- + ", actual: "
199- + latitude
200- + ", "
201- + longitude );
202-
203101 assert abs (latitude - coords .getLatitude ()) < 0.0001
204102 : "Latitude mismatch: expected " + coords .getLatitude () + ", got " + latitude ;
205103 assert abs (longitude - coords .getLongitude ()) < 0.0001
206104 : "Longitude mismatch: expected " + coords .getLongitude () + ", got " + longitude ;
207105 assert abs (accuracy - coords .getAccuracy ()) < 0.0001
208106 : "Accuracy mismatch: expected " + coords .getAccuracy () + ", got " + accuracy ;
209-
210- System .out .println ("DEBUG: Test completed successfully" );
211107 }
212108
213109 @ Test
214110 void canSetGeolocationOverrideWithMultipleUserContexts () {
215- System .out .println ("DEBUG: Starting canSetGeolocationOverrideWithMultipleUserContexts test" );
216-
217111 Browser browser = new Browser (driver );
218112 String userContext1 = browser .createUserContext ();
219113 String userContext2 = browser .createUserContext ();
220- System .out .println ("DEBUG: Created user contexts: " + userContext1 + ", " + userContext2 );
221114
222115 BrowsingContext context1 =
223116 new BrowsingContext (
224117 driver , new CreateContextParameters (WindowType .TAB ).userContext (userContext1 ));
225118 BrowsingContext context2 =
226119 new BrowsingContext (
227120 driver , new CreateContextParameters (WindowType .TAB ).userContext (userContext2 ));
228- System .out .println (
229- "DEBUG: Created browsing contexts: " + context1 .getId () + ", " + context2 .getId ());
230121
231122 GeolocationCoordinates coords =
232123 new GeolocationCoordinates (45.5 , -122.4194 , 10.0 , null , null , null );
233124
234125 Emulation emulation = new Emulation (driver );
235- System .out .println ("DEBUG: Setting geolocation override for multiple user contexts" );
236126 emulation .setGeolocationOverride (
237127 new SetGeolocationOverrideParameters (
238128 coords , null , null , List .of (userContext1 , userContext2 )));
239129
240- // Test first user context
241- System .out .println ("DEBUG: Testing first user context" );
242130 driver .switchTo ().window (context1 .getId ());
243-
244131 String url1 = appServer .whereIsSecure ("blank.html" );
245- System .out .println ("DEBUG: Using secure URL for context1: " + url1 );
246-
247132 context1 .navigate (url1 , ReadinessState .COMPLETE );
248133
249- // Wait for page to be fully loaded
250- try {
251- Thread .sleep (1000 );
252- } catch (InterruptedException e ) {
253- Thread .currentThread ().interrupt ();
254- }
255-
256134 String origin1 =
257135 (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
258- System .out .println ("DEBUG: Origin1: " + origin1 );
259136
260137 Map <String , Object > r =
261138 (Map <String , Object >) getBrowserGeolocation (driver , userContext1 , origin1 );
262139
263- System .out .println ("DEBUG: Context1 result: " + r );
264140 assert !r .containsKey ("error" ) : "Context1 geolocation failed with error: " + r .get ("error" );
265141
266142 double latitude1 = ((Number ) r .get ("latitude" )).doubleValue ();
@@ -271,30 +147,16 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
271147 assert abs (longitude1 - coords .getLongitude ()) < 0.0001 : "Context1 longitude mismatch" ;
272148 assert abs (accuracy1 - coords .getAccuracy ()) < 0.0001 : "Context1 accuracy mismatch" ;
273149
274- // Test second user context
275- System .out .println ("DEBUG: Testing second user context" );
276150 driver .switchTo ().window (context2 .getId ());
277-
278151 String url2 = appServer .whereIsSecure ("blank.html" );
279- System .out .println ("DEBUG: Using secure URL for context2: " + url2 );
280-
281152 context2 .navigate (url2 , ReadinessState .COMPLETE );
282153
283- // Wait for page to be fully loaded
284- try {
285- Thread .sleep (1000 );
286- } catch (InterruptedException e ) {
287- Thread .currentThread ().interrupt ();
288- }
289-
290154 String origin2 =
291155 (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
292- System .out .println ("DEBUG: Origin2: " + origin2 );
293156
294157 Map <String , Object > r2 =
295158 (Map <String , Object >) getBrowserGeolocation (driver , userContext2 , origin2 );
296159
297- System .out .println ("DEBUG: Context2 result: " + r2 );
298160 assert !r2 .containsKey ("error" ) : "Context2 geolocation failed with error: " + r2 .get ("error" );
299161
300162 double latitude2 = ((Number ) r2 .get ("latitude" )).doubleValue ();
@@ -305,57 +167,36 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
305167 assert abs (longitude2 - coords .getLongitude ()) < 0.0001 : "Context2 longitude mismatch" ;
306168 assert abs (accuracy2 - coords .getAccuracy ()) < 0.0001 : "Context2 accuracy mismatch" ;
307169
308- System .out .println ("DEBUG: Cleaning up contexts" );
309170 context1 .close ();
310171 context2 .close ();
311172 browser .removeUserContext (userContext1 );
312173 browser .removeUserContext (userContext2 );
313-
314- System .out .println ("DEBUG: Multiple user contexts test completed successfully" );
315174 }
316175
317176 @ Test
318177 @ Ignore (FIREFOX )
319178 void canSetGeolocationOverrideWithError () {
320- System .out .println ("DEBUG: Starting canSetGeolocationOverrideWithError test" );
321179
322180 BrowsingContext context = new BrowsingContext (driver , WindowType .TAB );
323181 String contextId = context .getId ();
324- System .out .println ("DEBUG: Created context with ID: " + contextId );
325182
326183 String url = appServer .whereIsSecure ("blank.html" );
327- System .out .println ("DEBUG: Using secure URL: " + url );
328-
329184 context .navigate (url , ReadinessState .COMPLETE );
330-
331- // Switch to the new context
332185 driver .switchTo ().window (contextId );
333186
334- // Wait for page to be fully loaded
335- try {
336- Thread .sleep (1000 );
337- } catch (InterruptedException e ) {
338- Thread .currentThread ().interrupt ();
339- }
340-
341187 String origin =
342188 (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
343- System .out .println ("DEBUG: Origin: " + origin );
344189
345190 GeolocationPositionError error = new GeolocationPositionError ();
346- System .out .println ("DEBUG: Setting geolocation override with error" );
347-
348191 Emulation emul = new Emulation (driver );
349192 emul .setGeolocationOverride (
350193 new SetGeolocationOverrideParameters (null , error , List .of (contextId ), null ));
351194
352195 Object result = getBrowserGeolocation (driver , null , origin );
353196 Map <String , Object > r = ((Map <String , Object >) result );
354197
355- System .out .println ("DEBUG: Error test result: " + r );
356198 assert r .containsKey ("error" ) : "Expected geolocation to fail with error, but got: " + r ;
357199
358200 context .close ();
359- System .out .println ("DEBUG: Error test completed successfully" );
360201 }
361202}
0 commit comments