2222
2323import java .util .List ;
2424import java .util .Map ;
25- import java .util .Optional ;
2625import org .junit .jupiter .api .Test ;
2726import org .openqa .selenium .JavascriptExecutor ;
2827import org .openqa .selenium .WebDriver ;
3231import org .openqa .selenium .bidi .browsingcontext .ReadinessState ;
3332import org .openqa .selenium .bidi .module .Browser ;
3433import org .openqa .selenium .bidi .module .Permission ;
35- import org .openqa .selenium .bidi .module .Script ;
3634import org .openqa .selenium .bidi .permissions .PermissionState ;
37- import org .openqa .selenium .bidi .script .EvaluateResult ;
38- import org .openqa .selenium .bidi .script .EvaluateResultSuccess ;
3935import org .openqa .selenium .testing .Ignore ;
4036import org .openqa .selenium .testing .JupiterTestBase ;
4137import org .openqa .selenium .testing .NeedsFreshDriver ;
@@ -45,13 +41,12 @@ class EmulationTest extends JupiterTestBase {
4541 "() => {return navigator.permissions.query({ name: 'geolocation' })"
4642 + ".then(val => val.state, err => err.message)}" ;
4743
48- Object getBrowserGeolocation (WebDriver driver , String userContext ) {
44+ Object getBrowserGeolocation (WebDriver driver , String userContext , String origin ) {
4945 JavascriptExecutor executor = (JavascriptExecutor ) driver ;
5046
51- String originValue = (String ) executor .executeScript ("return window.location.origin;" );
52-
5347 Permission permission = new Permission (driver );
54- permission .setPermission (Map .of ("name" , "geolocation" ), PermissionState .GRANTED , originValue , userContext );
48+ permission .setPermission (
49+ Map .of ("name" , "geolocation" ), PermissionState .GRANTED , origin , userContext );
5550
5651 return executor .executeAsyncScript (
5752 "const callback = arguments[arguments.length - 1];\n "
@@ -85,13 +80,16 @@ void getGeolocationOverrideWithCoordinatesInContext() {
8580 context .navigate (url , ReadinessState .COMPLETE );
8681 driver .switchTo ().window (context .getId ());
8782
83+ String origin =
84+ (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
85+
8886 Emulation emul = new Emulation (driver );
8987 GeolocationCoordinates coords =
9088 new GeolocationCoordinates (37.7749 , -122.4194 , 10.0 , null , null , null );
9189 emul .setGeolocationOverride (
9290 new SetGeolocationOverrideParameters (coords , null , List .of (contextId ), null ));
9391
94- Object result = getBrowserGeolocation (driver , null );
92+ Object result = getBrowserGeolocation (driver , null , origin );
9593 Map <String , Object > r = ((Map <String , Object >) result );
9694
9795 System .out .println (r );
@@ -131,8 +129,11 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
131129 // Test first user context
132130 driver .switchTo ().window (context1 .getId ());
133131 context1 .navigate (appServer .whereIs ("blank.html" ), ReadinessState .COMPLETE );
132+ String origin1 =
133+ (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
134134
135- Map <String , Object > r = (Map <String , Object >) getBrowserGeolocation (driver , userContext1 );
135+ Map <String , Object > r =
136+ (Map <String , Object >) getBrowserGeolocation (driver , userContext1 , origin1 );
136137
137138 assert !r .containsKey ("error" );
138139
@@ -147,7 +148,10 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
147148 // Test second user context
148149 driver .switchTo ().window (context2 .getId ());
149150 context2 .navigate (appServer .whereIs ("blank.html" ), ReadinessState .COMPLETE );
150- Map <String , Object > r2 = (Map <String , Object >) getBrowserGeolocation (driver , userContext2 );
151+ String origin2 =
152+ (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
153+ Map <String , Object > r2 =
154+ (Map <String , Object >) getBrowserGeolocation (driver , userContext2 , origin2 );
151155
152156 assert !r2 .containsKey ("error" );
153157
@@ -177,13 +181,16 @@ void canSetGeolocationOverrideWithError() {
177181 // Switch to the new context
178182 driver .switchTo ().window (contextId );
179183
184+ String origin =
185+ (String ) ((JavascriptExecutor ) driver ).executeScript ("return window.location.origin;" );
186+
180187 GeolocationPositionError error = new GeolocationPositionError ();
181188
182189 Emulation emul = new Emulation (driver );
183190 emul .setGeolocationOverride (
184191 new SetGeolocationOverrideParameters (null , error , List .of (contextId ), null ));
185192
186- Object result = getBrowserGeolocation (driver , null );
193+ Object result = getBrowserGeolocation (driver , null , origin );
187194 Map <String , Object > r = ((Map <String , Object >) result );
188195
189196 assert r .containsKey ("error" );
0 commit comments