1818package org .openqa .selenium .bidi .emulation ;
1919
2020import static java .lang .Math .abs ;
21+ import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
2122import static org .openqa .selenium .testing .drivers .Browser .FIREFOX ;
2223
2324import java .util .List ;
@@ -90,18 +91,15 @@ void canSetGeolocationOverrideWithCoordinatesInContext() {
9091 Object result = getBrowserGeolocation (driver , null , origin );
9192 Map <String , Object > r = ((Map <String , Object >) result );
9293
93- assert ! r .containsKey ("error" ) : "Geolocation failed with error: " + r . get ( "error" );
94+ assertThat ( r .containsKey ("error" )). isFalse ( );
9495
9596 double latitude = ((Number ) r .get ("latitude" )).doubleValue ();
9697 double longitude = ((Number ) r .get ("longitude" )).doubleValue ();
9798 double accuracy = ((Number ) r .get ("accuracy" )).doubleValue ();
9899
99- assert abs (latitude - coords .getLatitude ()) < 0.0001
100- : "Latitude mismatch: expected " + coords .getLatitude () + ", got " + latitude ;
101- assert abs (longitude - coords .getLongitude ()) < 0.0001
102- : "Longitude mismatch: expected " + coords .getLongitude () + ", got " + longitude ;
103- assert abs (accuracy - coords .getAccuracy ()) < 0.0001
104- : "Accuracy mismatch: expected " + coords .getAccuracy () + ", got " + accuracy ;
100+ assertThat (abs (latitude - coords .getLatitude ())).isLessThan (0.0001 );
101+ assertThat (abs (longitude - coords .getLongitude ())).isLessThan (0.0001 );
102+ assertThat (abs (accuracy - coords .getAccuracy ())).isLessThan (0.0001 );
105103 }
106104
107105 @ Test
@@ -134,15 +132,15 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
134132 Map <String , Object > r =
135133 (Map <String , Object >) getBrowserGeolocation (driver , userContext1 , origin1 );
136134
137- assert ! r .containsKey ("error" ) : "Context1 geolocation failed with error: " + r . get ( "error" );
135+ assertThat ( r .containsKey ("error" )). isFalse ( );
138136
139137 double latitude1 = ((Number ) r .get ("latitude" )).doubleValue ();
140138 double longitude1 = ((Number ) r .get ("longitude" )).doubleValue ();
141139 double accuracy1 = ((Number ) r .get ("accuracy" )).doubleValue ();
142140
143- assert abs (latitude1 - coords .getLatitude ()) < 0.0001 : "Context1 latitude mismatch" ;
144- assert abs (longitude1 - coords .getLongitude ()) < 0.0001 : "Context1 longitude mismatch" ;
145- assert abs (accuracy1 - coords .getAccuracy ()) < 0.0001 : "Context1 accuracy mismatch" ;
141+ assertThat ( abs (latitude1 - coords .getLatitude ())). isLessThan ( 0.0001 ) ;
142+ assertThat ( abs (longitude1 - coords .getLongitude ())). isLessThan ( 0.0001 ) ;
143+ assertThat ( abs (accuracy1 - coords .getAccuracy ())). isLessThan ( 0.0001 ) ;
146144
147145 driver .switchTo ().window (context2 .getId ());
148146 String url2 = appServer .whereIsSecure ("blank.html" );
@@ -154,15 +152,15 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
154152 Map <String , Object > r2 =
155153 (Map <String , Object >) getBrowserGeolocation (driver , userContext2 , origin2 );
156154
157- assert ! r2 .containsKey ("error" ) : "Context2 geolocation failed with error: " + r2 . get ( "error" );
155+ assertThat ( r2 .containsKey ("error" )). isFalse ( );
158156
159157 double latitude2 = ((Number ) r2 .get ("latitude" )).doubleValue ();
160158 double longitude2 = ((Number ) r2 .get ("longitude" )).doubleValue ();
161159 double accuracy2 = ((Number ) r2 .get ("accuracy" )).doubleValue ();
162160
163- assert abs (latitude2 - coords .getLatitude ()) < 0.0001 : "Context2 latitude mismatch" ;
164- assert abs (longitude2 - coords .getLongitude ()) < 0.0001 : "Context2 longitude mismatch" ;
165- assert abs (accuracy2 - coords .getAccuracy ()) < 0.0001 : "Context2 accuracy mismatch" ;
161+ assertThat ( abs (latitude2 - coords .getLatitude ())). isLessThan ( 0.0001 ) ;
162+ assertThat ( abs (longitude2 - coords .getLongitude ())). isLessThan ( 0.0001 ) ;
163+ assertThat ( abs (accuracy2 - coords .getAccuracy ())). isLessThan ( 0.0001 ) ;
166164
167165 context1 .close ();
168166 context2 .close ();
@@ -192,8 +190,48 @@ void canSetGeolocationOverrideWithError() {
192190 Object result = getBrowserGeolocation (driver , null , origin );
193191 Map <String , Object > r = ((Map <String , Object >) result );
194192
195- assert r .containsKey ("error" ) : "Expected geolocation to fail with error, but got: " + r ;
193+ assertThat ( r .containsKey ("error" )). isTrue () ;
196194
197195 context .close ();
198196 }
197+
198+ @ Test
199+ @ NeedsFreshDriver
200+ void canResetGeolocationOverrideWithNullCoordinates () {
201+ BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
202+ String contextId = context .getId ();
203+
204+ String url = appServer .whereIsSecure ("blank.html" );
205+ context .navigate (url , ReadinessState .COMPLETE );
206+ driver .switchTo ().window (context .getId ());
207+
208+ String origin =
209+ (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
210+
211+ Emulation emul = new Emulation (driver );
212+
213+ GeolocationCoordinates coords = new GeolocationCoordinates (37.7749 , -122.4194 );
214+ emul .setGeolocationOverride (
215+ new SetGeolocationOverrideParameters (coords ).contexts (List .of (contextId )));
216+
217+ Object firstResult = getBrowserGeolocation (driver , null , origin );
218+ Map <String , Object > r1 = ((Map <String , Object >) firstResult );
219+
220+ assertThat (r1 .containsKey ("error" )).isFalse ();
221+ double latitude1 = ((Number ) r1 .get ("latitude" )).doubleValue ();
222+ double longitude1 = ((Number ) r1 .get ("longitude" )).doubleValue ();
223+
224+ assertThat (abs (latitude1 - coords .getLatitude ())).isLessThan (0.0001 );
225+ assertThat (abs (longitude1 - coords .getLongitude ())).isLessThan (0.0001 );
226+
227+ emul .setGeolocationOverride (
228+ new SetGeolocationOverrideParameters ((GeolocationCoordinates ) null )
229+ .contexts (List .of (contextId )));
230+
231+ Object secondResult = getBrowserGeolocation (driver , null , origin );
232+ Map <String , Object > r2 = ((Map <String , Object >) secondResult );
233+
234+ // Error because there's no real geolocation available
235+ assertThat (r2 .containsKey ("error" )).isTrue ();
236+ }
199237}
0 commit comments