Skip to content

Commit fbb3fb9

Browse files
authored
Merge branch 'trunk' into py-compound-class-names-invalidselector
2 parents 8cf970a + a603bed commit fbb3fb9

File tree

4 files changed

+148
-3
lines changed

4 files changed

+148
-3
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.browsingcontext;
19+
20+
import org.openqa.selenium.json.JsonInput;
21+
22+
public class DownloadInfo extends NavigationInfo {
23+
24+
private final String suggestedFilename;
25+
26+
private DownloadInfo(
27+
String browsingContextId,
28+
String navigationId,
29+
long timestamp,
30+
String url,
31+
String suggestedFilename) {
32+
super(browsingContextId, navigationId, timestamp, url);
33+
this.suggestedFilename = suggestedFilename;
34+
}
35+
36+
public static DownloadInfo fromJson(JsonInput input) {
37+
String browsingContextId = null;
38+
String navigationId = null;
39+
long timestamp = 0;
40+
String url = null;
41+
String suggestedFilename = null;
42+
43+
input.beginObject();
44+
while (input.hasNext()) {
45+
switch (input.nextName()) {
46+
case "context":
47+
browsingContextId = input.read(String.class);
48+
break;
49+
50+
case "navigation":
51+
navigationId = input.read(String.class);
52+
break;
53+
54+
case "timestamp":
55+
timestamp = input.read(Long.class);
56+
break;
57+
58+
case "url":
59+
url = input.read(String.class);
60+
break;
61+
62+
case "suggestedFilename":
63+
suggestedFilename = input.read(String.class);
64+
break;
65+
66+
default:
67+
input.skipValue();
68+
break;
69+
}
70+
}
71+
72+
input.endObject();
73+
74+
return new DownloadInfo(browsingContextId, navigationId, timestamp, url, suggestedFilename);
75+
}
76+
77+
public String getSuggestedFilename() {
78+
return suggestedFilename;
79+
}
80+
}

java/src/org/openqa/selenium/bidi/browsingcontext/NavigationInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class NavigationInfo {
3333

3434
private final String url;
3535

36-
private NavigationInfo(
36+
protected NavigationInfo(
3737
String browsingContextId, String navigationId, long timestamp, String url) {
3838
this.browsingContextId = browsingContextId;
3939
this.navigationId = navigationId;

java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.openqa.selenium.bidi.Event;
3030
import org.openqa.selenium.bidi.HasBiDi;
3131
import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo;
32+
import org.openqa.selenium.bidi.browsingcontext.DownloadInfo;
3233
import org.openqa.selenium.bidi.browsingcontext.HistoryUpdated;
3334
import org.openqa.selenium.bidi.browsingcontext.NavigationInfo;
3435
import org.openqa.selenium.bidi.browsingcontext.UserPromptClosed;
@@ -61,6 +62,14 @@ public class BrowsingContextInspector implements AutoCloseable {
6162
}
6263
};
6364

65+
private final Function<Map<String, Object>, DownloadInfo> downloadWillBeginMapper =
66+
params -> {
67+
try (StringReader reader = new StringReader(JSON.toJson(params));
68+
JsonInput input = JSON.newInput(reader)) {
69+
return input.read(DownloadInfo.class);
70+
}
71+
};
72+
6473
private final Event<BrowsingContextInfo> browsingContextCreated =
6574
new Event<>("browsingContext.contextCreated", browsingContextInfoMapper);
6675

@@ -79,6 +88,9 @@ public class BrowsingContextInspector implements AutoCloseable {
7988

8089
private final Set<Event<NavigationInfo>> navigationEventSet = new HashSet<>();
8190

91+
private final Event<DownloadInfo> downloadWillBeginEvent =
92+
new Event<>("browsingContext.downloadWillBegin", downloadWillBeginMapper);
93+
8294
private final Event<UserPromptOpened> userPromptOpened =
8395
new Event<>(
8496
"browsingContext.userPromptOpened",
@@ -151,8 +163,12 @@ public void onBrowsingContextLoaded(Consumer<NavigationInfo> consumer) {
151163
addNavigationEventListener("browsingContext.load", consumer);
152164
}
153165

154-
public void onDownloadWillBegin(Consumer<NavigationInfo> consumer) {
155-
addNavigationEventListener("browsingContext.downloadWillBegin", consumer);
166+
public void onDownloadWillBegin(Consumer<DownloadInfo> consumer) {
167+
if (browsingContextIds.isEmpty()) {
168+
this.bidi.addListener(downloadWillBeginEvent, consumer);
169+
} else {
170+
this.bidi.addListener(browsingContextIds, downloadWillBeginEvent, consumer);
171+
}
156172
}
157173

158174
public void onNavigationAborted(Consumer<NavigationInfo> consumer) {
@@ -210,6 +226,7 @@ public void close() {
210226
this.bidi.clearListener(userPromptOpened);
211227
this.bidi.clearListener(userPromptClosed);
212228
this.bidi.clearListener(historyUpdated);
229+
this.bidi.clearListener(downloadWillBeginEvent);
213230

214231
navigationEventSet.forEach(this.bidi::clearListener);
215232
}

java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.openqa.selenium.bidi.module.BrowsingContextInspector;
3232
import org.openqa.selenium.testing.JupiterTestBase;
3333
import org.openqa.selenium.testing.NeedsFreshDriver;
34+
import org.openqa.selenium.testing.NotYetImplemented;
3435

3536
class BrowsingContextInspectorTest extends JupiterTestBase {
3637

@@ -231,4 +232,51 @@ void canListenToNavigationCommittedEvent()
231232
assertThat(navigationInfo.getUrl()).contains("/bidi/logEntryAdded.html");
232233
}
233234
}
235+
236+
@Test
237+
@NeedsFreshDriver
238+
@NotYetImplemented(FIREFOX)
239+
void canListenToDownloadWillBeginEvent()
240+
throws ExecutionException, InterruptedException, TimeoutException {
241+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
242+
CompletableFuture<DownloadInfo> future = new CompletableFuture<>();
243+
244+
inspector.onDownloadWillBegin(future::complete);
245+
246+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
247+
context.navigate(appServer.whereIs("/downloads/download.html"), ReadinessState.COMPLETE);
248+
249+
driver.findElement(By.id("file-1")).click();
250+
251+
DownloadInfo downloadInfo = future.get(5, TimeUnit.SECONDS);
252+
assertThat(downloadInfo.getBrowsingContextId()).isEqualTo(context.getId());
253+
assertThat(downloadInfo.getUrl()).contains("/downloads/file_1.txt");
254+
assertThat(downloadInfo.getSuggestedFilename()).isEqualTo("file_1.txt");
255+
}
256+
}
257+
258+
@Test
259+
@NeedsFreshDriver
260+
@NotYetImplemented(FIREFOX)
261+
void canListenToNavigationFailedEvent()
262+
throws ExecutionException, InterruptedException, TimeoutException {
263+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
264+
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
265+
266+
inspector.onNavigationFailed(future::complete);
267+
268+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
269+
try {
270+
context.navigate(
271+
"http://invalid-domain-that-does-not-exist.test/", ReadinessState.COMPLETE);
272+
} catch (Exception e) {
273+
// Expect an exception due to navigation failure
274+
}
275+
276+
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
277+
assertThat(navigationInfo.getBrowsingContextId()).isEqualTo(context.getId());
278+
assertThat(navigationInfo.getUrl())
279+
.isEqualTo("http://invalid-domain-that-does-not-exist.test/");
280+
}
281+
}
234282
}

0 commit comments

Comments
 (0)