From 173391f7180928de8121825e386cfd7b637c980a Mon Sep 17 00:00:00 2001 From: Delta456 Date: Wed, 24 Sep 2025 17:22:34 +0530 Subject: [PATCH 1/7] [java][BiDi] implement `browsingContext.downloadEnd` --- .../browsingcontext/DownloadCanceled.java | 79 +++++++++++++++ .../browsingcontext/DownloadCompleted.java | 95 +++++++++++++++++++ .../bidi/browsingcontext/DownloadEnded.java | 93 ++++++++++++++++++ .../bidi/module/BrowsingContextInspector.java | 27 ++++-- .../BrowsingContextInspectorTest.java | 16 ++-- 5 files changed, 296 insertions(+), 14 deletions(-) create mode 100644 java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCanceled.java create mode 100644 java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCompleted.java create mode 100644 java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCanceled.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCanceled.java new file mode 100644 index 0000000000000..3aeaf9210280d --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCanceled.java @@ -0,0 +1,79 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.bidi.browsingcontext; + +import org.openqa.selenium.json.JsonInput; + +public class DownloadCanceled extends NavigationInfo { + + private final String status; + + DownloadCanceled( + String browsingContextId, String navigationId, long timestamp, String url, String status) { + super(browsingContextId, navigationId, timestamp, url); + this.status = status != null ? status : "canceled"; + } + + public static DownloadCanceled fromJson(JsonInput input) { + String browsingContextId = null; + String navigationId = null; + long timestamp = 0; + String url = null; + String status = "canceled"; + + input.beginObject(); + while (input.hasNext()) { + switch (input.nextName()) { + case "context": + browsingContextId = input.read(String.class); + break; + + case "navigation": + navigationId = input.read(String.class); + break; + + case "timestamp": + timestamp = input.read(Long.class); + break; + + case "url": + url = input.read(String.class); + break; + + case "status": + status = input.read(String.class); + if (!"canceled".equals(status)) { + throw new IllegalArgumentException("Expected status 'canceled', but got: " + status); + } + break; + + default: + input.skipValue(); + break; + } + } + + input.endObject(); + + return new DownloadCanceled(browsingContextId, navigationId, timestamp, url, status); + } + + public String getStatus() { + return status; + } +} diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCompleted.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCompleted.java new file mode 100644 index 0000000000000..5e4e6bbb78f2d --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCompleted.java @@ -0,0 +1,95 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.bidi.browsingcontext; + +import org.openqa.selenium.json.JsonInput; + +public class DownloadCompleted extends NavigationInfo { + + private final String status; + private final String filepath; + + DownloadCompleted( + String browsingContextId, + String navigationId, + long timestamp, + String url, + String status, + String filepath) { + super(browsingContextId, navigationId, timestamp, url); + this.status = status != null ? status : "complete"; + this.filepath = filepath; + } + + public static DownloadCompleted fromJson(JsonInput input) { + String browsingContextId = null; + String navigationId = null; + long timestamp = 0; + String url = null; + String status = "complete"; + String filepath = null; + + input.beginObject(); + while (input.hasNext()) { + switch (input.nextName()) { + case "context": + browsingContextId = input.read(String.class); + break; + + case "navigation": + navigationId = input.read(String.class); + break; + + case "timestamp": + timestamp = input.read(Long.class); + break; + + case "url": + url = input.read(String.class); + break; + + case "status": + status = input.read(String.class); + if (!"complete".equals(status)) { + throw new IllegalArgumentException("Expected status 'complete', but got: " + status); + } + break; + + case "filepath": + filepath = input.read(String.class); + break; + + default: + input.skipValue(); + break; + } + } + + input.endObject(); + + return new DownloadCompleted(browsingContextId, navigationId, timestamp, url, status, filepath); + } + + public String getStatus() { + return status; + } + + public String getFilepath() { + return filepath; + } +} diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java new file mode 100644 index 0000000000000..bff50ba60f635 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java @@ -0,0 +1,93 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.bidi.browsingcontext; + +import org.openqa.selenium.json.JsonInput; + +public class DownloadEnded { + + private final NavigationInfo downloadParams; + + public DownloadEnded(NavigationInfo downloadParams) { + this.downloadParams = downloadParams; + } + + public static DownloadEnded fromJson(JsonInput input) { + String browsingContextId = null; + String navigationId = null; + long timestamp = 0; + String url = null; + String status = null; + String filepath = null; + + input.beginObject(); + while (input.hasNext()) { + switch (input.nextName()) { + case "context": + browsingContextId = input.read(String.class); + break; + case "navigation": + navigationId = input.read(String.class); + break; + case "timestamp": + timestamp = input.read(Long.class); + break; + case "url": + url = input.read(String.class); + break; + case "status": + status = input.read(String.class); + break; + case "filepath": + filepath = input.read(String.class); + break; + default: + input.skipValue(); + break; + } + } + input.endObject(); + + // Create the appropriate object based on status + if ("canceled".equals(status)) { + DownloadCanceled canceled = + new DownloadCanceled(browsingContextId, navigationId, timestamp, url, status); + return new DownloadEnded(canceled); + } else if ("complete".equals(status)) { + DownloadCompleted completed = + new DownloadCompleted(browsingContextId, navigationId, timestamp, url, status, filepath); + return new DownloadEnded(completed); + } else { + throw new IllegalArgumentException( + "status must be either 'canceled' or 'complete', but got: " + status); + } + } + + public NavigationInfo getDownloadParams() { + return downloadParams; + } + + public boolean isCanceled() { + return downloadParams instanceof DownloadCanceled; + } + + public boolean isCompleted() { + return downloadParams instanceof DownloadCompleted; + } + +} diff --git a/java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java b/java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java index 4b7637e2af22f..4206dbae61465 100644 --- a/java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java +++ b/java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java @@ -28,12 +28,7 @@ import org.openqa.selenium.bidi.BiDi; import org.openqa.selenium.bidi.Event; import org.openqa.selenium.bidi.HasBiDi; -import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo; -import org.openqa.selenium.bidi.browsingcontext.DownloadInfo; -import org.openqa.selenium.bidi.browsingcontext.HistoryUpdated; -import org.openqa.selenium.bidi.browsingcontext.NavigationInfo; -import org.openqa.selenium.bidi.browsingcontext.UserPromptClosed; -import org.openqa.selenium.bidi.browsingcontext.UserPromptOpened; +import org.openqa.selenium.bidi.browsingcontext.*; import org.openqa.selenium.internal.Require; import org.openqa.selenium.json.Json; import org.openqa.selenium.json.JsonInput; @@ -70,6 +65,14 @@ public class BrowsingContextInspector implements AutoCloseable { } }; + private final Function, DownloadEnded> downloadWillEndMapper = + params -> { + try (StringReader reader = new StringReader(JSON.toJson(params)); + JsonInput input = JSON.newInput(reader)) { + return input.read(DownloadEnded.class); + } + }; + private final Event browsingContextCreated = new Event<>("browsingContext.contextCreated", browsingContextInfoMapper); @@ -91,6 +94,9 @@ public class BrowsingContextInspector implements AutoCloseable { private final Event downloadWillBeginEvent = new Event<>("browsingContext.downloadWillBegin", downloadWillBeginMapper); + private final Event downloadWillEndEvent = + new Event<>("browsingContext.downloadEnd", downloadWillEndMapper); + private final Event userPromptOpened = new Event<>( "browsingContext.userPromptOpened", @@ -171,6 +177,14 @@ public void onDownloadWillBegin(Consumer consumer) { } } + public void onDownloadEnd(Consumer consumer) { + if (browsingContextIds.isEmpty()) { + this.bidi.addListener(downloadWillEndEvent, consumer); + } else { + this.bidi.addListener(browsingContextIds, downloadWillEndEvent, consumer); + } + } + public void onNavigationAborted(Consumer consumer) { addNavigationEventListener("browsingContext.navigationAborted", consumer); } @@ -227,6 +241,7 @@ public void close() { this.bidi.clearListener(userPromptClosed); this.bidi.clearListener(historyUpdated); this.bidi.clearListener(downloadWillBeginEvent); + this.bidi.clearListener(downloadWillEndEvent); navigationEventSet.forEach(this.bidi::clearListener); } diff --git a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java index 93ddcfcb83d10..f6e7e6b3096d3 100644 --- a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java +++ b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java @@ -236,22 +236,22 @@ void canListenToNavigationCommittedEvent() @Test @NeedsFreshDriver @NotYetImplemented(FIREFOX) - void canListenToDownloadWillBeginEvent() - throws ExecutionException, InterruptedException, TimeoutException { + void canListenToDownloadEnd() throws ExecutionException, InterruptedException, TimeoutException { try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) { - CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); - inspector.onDownloadWillBegin(future::complete); + inspector.onDownloadEnd(future::complete); BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); context.navigate(appServer.whereIs("/downloads/download.html"), ReadinessState.COMPLETE); driver.findElement(By.id("file-1")).click(); - DownloadInfo downloadInfo = future.get(5, TimeUnit.SECONDS); - assertThat(downloadInfo.getBrowsingContextId()).isEqualTo(context.getId()); - assertThat(downloadInfo.getUrl()).contains("/downloads/file_1.txt"); - assertThat(downloadInfo.getSuggestedFilename()).isEqualTo("file_1.txt"); + DownloadEnded downloadEnded = future.get(5, TimeUnit.SECONDS); + assertThat(downloadEnded.getDownloadParams().getBrowsingContextId()) + .isEqualTo(context.getId()); + assertThat(downloadEnded.isCompleted()).isTrue(); + assertThat(downloadEnded.getDownloadParams().getUrl()).contains("/downloads/file_1.txt"); } } From 8b155ab287787d775f4003675a4be862b736013e Mon Sep 17 00:00:00 2001 From: Delta456 Date: Wed, 24 Sep 2025 17:54:08 +0530 Subject: [PATCH 2/7] fmt --- .../org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java | 1 - 1 file changed, 1 deletion(-) diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java index bff50ba60f635..f2c60ca1b378f 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java @@ -89,5 +89,4 @@ public boolean isCanceled() { public boolean isCompleted() { return downloadParams instanceof DownloadCompleted; } - } From 08b5b6a7cb38a115cef76e78145fd24e0025ae06 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Wed, 24 Sep 2025 17:57:12 +0530 Subject: [PATCH 3/7] Update java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java Co-authored-by: qodo-merge-pro[bot] <151058649+qodo-merge-pro[bot]@users.noreply.github.com> --- .../bidi/browsingcontext/DownloadEnded.java | 58 ++++--------------- 1 file changed, 12 insertions(+), 46 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java index f2c60ca1b378f..dd46c342637f7 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java @@ -28,54 +28,20 @@ public DownloadEnded(NavigationInfo downloadParams) { } public static DownloadEnded fromJson(JsonInput input) { - String browsingContextId = null; - String navigationId = null; - long timestamp = 0; - String url = null; - String status = null; - String filepath = null; - - input.beginObject(); - while (input.hasNext()) { - switch (input.nextName()) { - case "context": - browsingContextId = input.read(String.class); - break; - case "navigation": - navigationId = input.read(String.class); - break; - case "timestamp": - timestamp = input.read(Long.class); - break; - case "url": - url = input.read(String.class); - break; - case "status": - status = input.read(String.class); - break; - case "filepath": - filepath = input.read(String.class); - break; - default: - input.skipValue(); - break; + Map jsonMap = input.read(Map.class); + String status = (String) jsonMap.get("status"); + + try (StringReader reader = new StringReader(new Json().toJson(jsonMap)); + JsonInput jsonInput = new Json().newInput(reader)) { + if ("canceled".equals(status)) { + return new DownloadEnded(DownloadCanceled.fromJson(jsonInput)); + } else if ("complete".equals(status)) { + return new DownloadEnded(DownloadCompleted.fromJson(jsonInput)); + } else { + throw new IllegalArgumentException( + "status must be either 'canceled' or 'complete', but got: " + status); } } - input.endObject(); - - // Create the appropriate object based on status - if ("canceled".equals(status)) { - DownloadCanceled canceled = - new DownloadCanceled(browsingContextId, navigationId, timestamp, url, status); - return new DownloadEnded(canceled); - } else if ("complete".equals(status)) { - DownloadCompleted completed = - new DownloadCompleted(browsingContextId, navigationId, timestamp, url, status, filepath); - return new DownloadEnded(completed); - } else { - throw new IllegalArgumentException( - "status must be either 'canceled' or 'complete', but got: " + status); - } } public NavigationInfo getDownloadParams() { From f8b4a45cc7dc34ec1fa3d36689892a8dcf64857a Mon Sep 17 00:00:00 2001 From: Delta456 Date: Wed, 24 Sep 2025 17:58:40 +0530 Subject: [PATCH 4/7] add imports --- .../openqa/selenium/bidi/browsingcontext/DownloadEnded.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java index dd46c342637f7..bd6312216e633 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java @@ -17,6 +17,9 @@ package org.openqa.selenium.bidi.browsingcontext; +import java.io.StringReader; +import java.util.Map; +import org.openqa.selenium.json.Json; import org.openqa.selenium.json.JsonInput; public class DownloadEnded { From 97887a7c17ad1b0269c0c79b2dc3856aaf3bd907 Mon Sep 17 00:00:00 2001 From: Delta456 Date: Wed, 24 Sep 2025 18:09:56 +0530 Subject: [PATCH 5/7] rename --- .../bidi/module/BrowsingContextInspector.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java b/java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java index 4206dbae61465..ac0b9fa0b4904 100644 --- a/java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java +++ b/java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java @@ -65,7 +65,7 @@ public class BrowsingContextInspector implements AutoCloseable { } }; - private final Function, DownloadEnded> downloadWillEndMapper = + private final Function, DownloadEnded> downloadEndMapper = params -> { try (StringReader reader = new StringReader(JSON.toJson(params)); JsonInput input = JSON.newInput(reader)) { @@ -94,8 +94,8 @@ public class BrowsingContextInspector implements AutoCloseable { private final Event downloadWillBeginEvent = new Event<>("browsingContext.downloadWillBegin", downloadWillBeginMapper); - private final Event downloadWillEndEvent = - new Event<>("browsingContext.downloadEnd", downloadWillEndMapper); + private final Event downloadEndEvent = + new Event<>("browsingContext.downloadEnd", downloadEndMapper); private final Event userPromptOpened = new Event<>( @@ -179,9 +179,9 @@ public void onDownloadWillBegin(Consumer consumer) { public void onDownloadEnd(Consumer consumer) { if (browsingContextIds.isEmpty()) { - this.bidi.addListener(downloadWillEndEvent, consumer); + this.bidi.addListener(downloadEndEvent, consumer); } else { - this.bidi.addListener(browsingContextIds, downloadWillEndEvent, consumer); + this.bidi.addListener(browsingContextIds, downloadEndEvent, consumer); } } @@ -241,7 +241,7 @@ public void close() { this.bidi.clearListener(userPromptClosed); this.bidi.clearListener(historyUpdated); this.bidi.clearListener(downloadWillBeginEvent); - this.bidi.clearListener(downloadWillEndEvent); + this.bidi.clearListener(downloadEndEvent); navigationEventSet.forEach(this.bidi::clearListener); } From b8af810aff8b4f4135e2d94ec3c66e797c607b8e Mon Sep 17 00:00:00 2001 From: Delta456 Date: Wed, 24 Sep 2025 18:13:55 +0530 Subject: [PATCH 6/7] revert old test (was accidental) --- .../BrowsingContextInspectorTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java index f6e7e6b3096d3..e5882c0375a3b 100644 --- a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java +++ b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextInspectorTest.java @@ -233,6 +233,28 @@ void canListenToNavigationCommittedEvent() } } + @Test + @NeedsFreshDriver + @NotYetImplemented(FIREFOX) + void canListenToDownloadWillBeginEvent() + throws ExecutionException, InterruptedException, TimeoutException { + try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) { + CompletableFuture future = new CompletableFuture<>(); + + inspector.onDownloadWillBegin(future::complete); + + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); + context.navigate(appServer.whereIs("/downloads/download.html"), ReadinessState.COMPLETE); + + driver.findElement(By.id("file-1")).click(); + + DownloadInfo downloadInfo = future.get(5, TimeUnit.SECONDS); + assertThat(downloadInfo.getBrowsingContextId()).isEqualTo(context.getId()); + assertThat(downloadInfo.getUrl()).contains("/downloads/file_1.txt"); + assertThat(downloadInfo.getSuggestedFilename()).isEqualTo("file_1.txt"); + } + } + @Test @NeedsFreshDriver @NotYetImplemented(FIREFOX) From cca96253715cb7f791373f14e53e0267d556431b Mon Sep 17 00:00:00 2001 From: Delta456 Date: Thu, 25 Sep 2025 13:05:20 +0530 Subject: [PATCH 7/7] use consts and util.objects --- .../bidi/browsingcontext/DownloadCanceled.java | 13 +++++++++---- .../bidi/browsingcontext/DownloadCompleted.java | 13 +++++++++---- .../bidi/browsingcontext/DownloadEnded.java | 9 ++++++--- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCanceled.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCanceled.java index 3aeaf9210280d..f98059cf57b00 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCanceled.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCanceled.java @@ -17,16 +17,20 @@ package org.openqa.selenium.bidi.browsingcontext; +import static java.util.Objects.requireNonNullElse; + import org.openqa.selenium.json.JsonInput; public class DownloadCanceled extends NavigationInfo { private final String status; + private static final String CANCELED = "canceled"; + DownloadCanceled( String browsingContextId, String navigationId, long timestamp, String url, String status) { super(browsingContextId, navigationId, timestamp, url); - this.status = status != null ? status : "canceled"; + this.status = requireNonNullElse(status, CANCELED); } public static DownloadCanceled fromJson(JsonInput input) { @@ -34,7 +38,7 @@ public static DownloadCanceled fromJson(JsonInput input) { String navigationId = null; long timestamp = 0; String url = null; - String status = "canceled"; + String status = CANCELED; input.beginObject(); while (input.hasNext()) { @@ -57,8 +61,9 @@ public static DownloadCanceled fromJson(JsonInput input) { case "status": status = input.read(String.class); - if (!"canceled".equals(status)) { - throw new IllegalArgumentException("Expected status 'canceled', but got: " + status); + if (!CANCELED.equals(status)) { + throw new IllegalArgumentException( + "Expected status '" + CANCELED + "' , but got: " + status); } break; diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCompleted.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCompleted.java index 5e4e6bbb78f2d..270a33871e3ce 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCompleted.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadCompleted.java @@ -17,6 +17,8 @@ package org.openqa.selenium.bidi.browsingcontext; +import static java.util.Objects.requireNonNullElse; + import org.openqa.selenium.json.JsonInput; public class DownloadCompleted extends NavigationInfo { @@ -24,6 +26,8 @@ public class DownloadCompleted extends NavigationInfo { private final String status; private final String filepath; + private static final String COMPLETE = "complete"; + DownloadCompleted( String browsingContextId, String navigationId, @@ -32,7 +36,7 @@ public class DownloadCompleted extends NavigationInfo { String status, String filepath) { super(browsingContextId, navigationId, timestamp, url); - this.status = status != null ? status : "complete"; + this.status = requireNonNullElse(status, COMPLETE); this.filepath = filepath; } @@ -41,7 +45,7 @@ public static DownloadCompleted fromJson(JsonInput input) { String navigationId = null; long timestamp = 0; String url = null; - String status = "complete"; + String status = COMPLETE; String filepath = null; input.beginObject(); @@ -65,8 +69,9 @@ public static DownloadCompleted fromJson(JsonInput input) { case "status": status = input.read(String.class); - if (!"complete".equals(status)) { - throw new IllegalArgumentException("Expected status 'complete', but got: " + status); + if (!COMPLETE.equals(status)) { + throw new IllegalArgumentException( + "Expected status '" + COMPLETE + "' , but got: " + status); } break; diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java index bd6312216e633..8d097db435d64 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/DownloadEnded.java @@ -24,6 +24,9 @@ public class DownloadEnded { + private static final String CANCELED = "canceled"; + private static final String COMPLETE = "complete"; + private final NavigationInfo downloadParams; public DownloadEnded(NavigationInfo downloadParams) { @@ -36,13 +39,13 @@ public static DownloadEnded fromJson(JsonInput input) { try (StringReader reader = new StringReader(new Json().toJson(jsonMap)); JsonInput jsonInput = new Json().newInput(reader)) { - if ("canceled".equals(status)) { + if (CANCELED.equals(status)) { return new DownloadEnded(DownloadCanceled.fromJson(jsonInput)); - } else if ("complete".equals(status)) { + } else if (COMPLETE.equals(status)) { return new DownloadEnded(DownloadCompleted.fromJson(jsonInput)); } else { throw new IllegalArgumentException( - "status must be either 'canceled' or 'complete', but got: " + status); + "status must be either '" + CANCELED + "' or '" + COMPLETE + "', but got: " + status); } } }