Skip to content

Commit 3adb987

Browse files
committed
try again
1 parent 4c45cf3 commit 3adb987

File tree

1 file changed

+38
-99
lines changed

1 file changed

+38
-99
lines changed

java/test/org/openqa/selenium/bidi/emulation/EmulationTest.java

Lines changed: 38 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.Optional;
2526
import org.junit.jupiter.api.Test;
2627
import org.openqa.selenium.JavascriptExecutor;
2728
import org.openqa.selenium.WebDriver;
@@ -31,26 +32,53 @@
3132
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
3233
import org.openqa.selenium.bidi.module.Browser;
3334
import org.openqa.selenium.bidi.module.Permission;
35+
import org.openqa.selenium.bidi.module.Script;
3436
import org.openqa.selenium.bidi.permissions.PermissionState;
37+
import org.openqa.selenium.bidi.script.EvaluateResult;
38+
import org.openqa.selenium.bidi.script.EvaluateResultSuccess;
3539
import org.openqa.selenium.testing.Ignore;
3640
import org.openqa.selenium.testing.JupiterTestBase;
3741
import org.openqa.selenium.testing.NeedsFreshDriver;
3842

3943
class EmulationTest extends JupiterTestBase {
44+
private static final String GET_GEOLOCATION_PERMISSION =
45+
"() => {return navigator.permissions.query({ name: 'geolocation' })"
46+
+ ".then(val => val.state, err => err.message)}";
47+
4048
Object getBrowserGeolocation(WebDriver driver, String userContext) {
4149
JavascriptExecutor executor = (JavascriptExecutor) driver;
4250

43-
// BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
44-
// String url = appServer.whereIs("blank.html");
45-
// context.navigate(url, ReadinessState.COMPLETE);
46-
// driver.switchTo().window(context.getId());
51+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
52+
String url = appServer.whereIs("blank.html");
53+
context.navigate(url, ReadinessState.COMPLETE);
54+
driver.switchTo().window(context.getId());
4755

4856
Permission permission = new Permission(driver);
49-
var origin = executor.executeScript("return window.location.origin;");
50-
String originValue = (String) origin;
51-
52-
permission.setPermission(
53-
Map.of("name", "geolocation"), PermissionState.GRANTED, originValue);
57+
Script script = new Script(driver);
58+
EvaluateResult origin =
59+
script.callFunctionInBrowsingContext(
60+
context.getId(),
61+
"() => {return window.location.origin;}",
62+
true,
63+
Optional.empty(),
64+
Optional.empty(),
65+
Optional.empty());
66+
67+
String originValue = (String) ((EvaluateResultSuccess) origin).getResult().getValue().get();
68+
69+
permission.setPermission(Map.of("name", "geolocation"), PermissionState.GRANTED, originValue);
70+
71+
EvaluateResult result =
72+
script.callFunctionInBrowsingContext(
73+
context.getId(),
74+
GET_GEOLOCATION_PERMISSION,
75+
true,
76+
Optional.empty(),
77+
Optional.empty(),
78+
Optional.empty());
79+
80+
String resultValue = (String) ((EvaluateResultSuccess) result).getResult().getValue().get();
81+
System.out.println(resultValue);
5482

5583
return executor.executeAsyncScript(
5684
"const callback = arguments[arguments.length - 1];\n"
@@ -93,39 +121,7 @@ void getGeolocationOverrideWithCoordinatesInContext() {
93121
Object result = getBrowserGeolocation(driver, null);
94122
Map<String, Object> r = ((Map<String, Object>) result);
95123

96-
assert !r.containsKey("error");
97-
98-
double latitude = ((Number) r.get("latitude")).doubleValue();
99-
double longitude = ((Number) r.get("longitude")).doubleValue();
100-
double accuracy = ((Number) r.get("accuracy")).doubleValue();
101-
102-
assert abs(latitude - coords.getLatitude()) < 0.0001;
103-
assert abs(longitude - coords.getLongitude()) < 0.0001;
104-
assert abs(accuracy - coords.getAccuracy()) < 0.0001;
105-
}
106-
107-
@Test
108-
void canSetGeolocationOverrideWithCoordinatesInUserContext() {
109-
Browser browser = new Browser(driver);
110-
String userContext = browser.createUserContext();
111-
112-
CreateContextParameters parameters =
113-
new CreateContextParameters(WindowType.TAB).userContext(userContext);
114-
115-
BrowsingContext context = new BrowsingContext(driver, parameters);
116-
117-
String url = appServer.whereIs("blank.html");
118-
context.navigate(url, ReadinessState.COMPLETE);
119-
driver.switchTo().window(context.getId());
120-
121-
Emulation emul = new Emulation(driver);
122-
GeolocationCoordinates coords =
123-
new GeolocationCoordinates(37.7749, -122.4194, 10.0, null, null, null);
124-
emul.setGeolocationOverride(
125-
new SetGeolocationOverrideParameters(coords, null, null, List.of(userContext)));
126-
127-
Object result = getBrowserGeolocation(driver, userContext);
128-
Map<String, Object> r = ((Map<String, Object>) result);
124+
System.out.println(r);
129125

130126
assert !r.containsKey("error");
131127

@@ -136,63 +132,6 @@ void canSetGeolocationOverrideWithCoordinatesInUserContext() {
136132
assert abs(latitude - coords.getLatitude()) < 0.0001;
137133
assert abs(longitude - coords.getLongitude()) < 0.0001;
138134
assert abs(accuracy - coords.getAccuracy()) < 0.0001;
139-
140-
context.close();
141-
browser.removeUserContext(userContext);
142-
}
143-
144-
@Test
145-
void canSetGeolocationOverrideWithMultipleContexts() {
146-
// Create two browsing contexts
147-
BrowsingContext context1 = new BrowsingContext(driver, WindowType.TAB);
148-
BrowsingContext context2 = new BrowsingContext(driver, WindowType.TAB);
149-
150-
GeolocationCoordinates coords =
151-
new GeolocationCoordinates(45.5, -122.4194, 10.0, null, null, null, null);
152-
153-
Emulation emulation = new Emulation(driver);
154-
SetGeolocationOverrideParameters parameters =
155-
new SetGeolocationOverrideParameters(
156-
coords, null, List.of(context1.getId(), context2.getId()), null);
157-
158-
emulation.setGeolocationOverride(parameters);
159-
160-
// Test first context
161-
String url = appServer.whereIs("blank.html");
162-
context1.navigate(url, ReadinessState.COMPLETE);
163-
driver.switchTo().window(context1.getId());
164-
165-
Map<String, Object> r1 = (Map<String, Object>) getBrowserGeolocation(driver, null);
166-
167-
assert !r1.containsKey("error");
168-
169-
double latitude = ((Number) r1.get("latitude")).doubleValue();
170-
double longitude = ((Number) r1.get("longitude")).doubleValue();
171-
double accuracy = ((Number) r1.get("accuracy")).doubleValue();
172-
173-
assert abs(latitude - coords.getLatitude()) < 0.0001;
174-
assert abs(longitude - coords.getLongitude()) < 0.0001;
175-
assert abs(accuracy - coords.getAccuracy()) < 0.0001;
176-
177-
// Test second context
178-
url = appServer.whereIs("blank.html");
179-
context2.navigate(url, ReadinessState.COMPLETE);
180-
driver.switchTo().window(context2.getId());
181-
182-
Map<String, Object> r = (Map<String, Object>) getBrowserGeolocation(driver, null);
183-
184-
assert !r.containsKey("error");
185-
186-
double latitude1 = ((Number) r.get("latitude")).doubleValue();
187-
double longitude1 = ((Number) r.get("longitude")).doubleValue();
188-
double accuracy1 = ((Number) r.get("accuracy")).doubleValue();
189-
190-
assert abs(latitude1 - coords.getLatitude()) < 0.0001;
191-
assert abs(longitude1 - coords.getLongitude()) < 0.0001;
192-
assert abs(accuracy1 - coords.getAccuracy()) < 0.0001;
193-
194-
context1.close();
195-
context2.close();
196135
}
197136

198137
@Test

0 commit comments

Comments
 (0)