From 309b3e802f1ee5052194548d31f2e6fcae78a841 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Wed, 3 Apr 2024 11:41:15 +0530 Subject: [PATCH] [bidi][java] Deprecate using builder for Locate Node parameters. (#13767) --- .../browsingcontext/LocateNodeParameters.java | 45 ++++++++++++++++--- .../bidi/browsingcontext/LocateNodesTest.java | 34 +++++--------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/LocateNodeParameters.java b/java/src/org/openqa/selenium/bidi/browsingcontext/LocateNodeParameters.java index cfdcdc2b3f03e..cbaf450a710bd 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/LocateNodeParameters.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/LocateNodeParameters.java @@ -29,11 +29,11 @@ public class LocateNodeParameters { private final Locator locator; - private final Optional maxNodeCount; - private final Optional ownership; - private final Optional sandbox; - private final Optional serializationOptions; - private final Optional> startNodes; + private Optional maxNodeCount = Optional.empty(); + private Optional ownership = Optional.empty(); + private Optional sandbox = Optional.empty(); + private Optional serializationOptions = Optional.empty(); + private Optional> startNodes = Optional.empty(); private LocateNodeParameters(Builder builder) { this.locator = builder.locator; @@ -44,6 +44,35 @@ private LocateNodeParameters(Builder builder) { this.startNodes = Optional.ofNullable(builder.startNodes); } + public LocateNodeParameters(Locator locator) { + this.locator = locator; + } + + public LocateNodeParameters setMaxNodeCount(long maxNodeCount) { + this.maxNodeCount = Optional.of(maxNodeCount); + return this; + } + + public LocateNodeParameters setOwnership(ResultOwnership ownership) { + this.ownership = Optional.of(ownership); + return this; + } + + public LocateNodeParameters setSandbox(String sandbox) { + this.sandbox = Optional.of(sandbox); + return this; + } + + public LocateNodeParameters setSerializationOptions(SerializationOptions options) { + this.serializationOptions = Optional.of(options); + return this; + } + + public LocateNodeParameters setStartNodes(List startNodes) { + this.startNodes = Optional.of(startNodes); + return this; + } + public Map toMap() { final Map map = new HashMap<>(); @@ -62,6 +91,12 @@ public Map toMap() { return map; } + /** + * @deprecated Use the chaining of LocateNodeParameters methods to add optional parameters. This + * is in favor of keeping the usage pattern consistent for BiDi parameters. Use the {@link + * LocateNodeParameters#LocateNodeParameters(Locator locator)} constructor and chain methods. + */ + @Deprecated(since = "4.20", forRemoval = true) public static class Builder { private final Locator locator; diff --git a/java/test/org/openqa/selenium/bidi/browsingcontext/LocateNodesTest.java b/java/test/org/openqa/selenium/bidi/browsingcontext/LocateNodesTest.java index 2ee0d3ed0d08b..8a11d5ecc03f0 100644 --- a/java/test/org/openqa/selenium/bidi/browsingcontext/LocateNodesTest.java +++ b/java/test/org/openqa/selenium/bidi/browsingcontext/LocateNodesTest.java @@ -65,7 +65,7 @@ void canLocateNodes() { driver.get(pages.xhtmlTestPage); - LocateNodeParameters parameters = new LocateNodeParameters.Builder(Locator.css("div")).build(); + LocateNodeParameters parameters = new LocateNodeParameters(Locator.css("div")); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isEqualTo(13); @@ -113,9 +113,7 @@ void canLocateNodesWithCSSLocator() { driver.get(pages.xhtmlTestPage); LocateNodeParameters parameters = - new LocateNodeParameters.Builder(Locator.css("div.extraDiv, div.content")) - .setMaxNodeCount(1) - .build(); + new LocateNodeParameters(Locator.css("div.extraDiv, div.content")).setMaxNodeCount(1); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isGreaterThanOrEqualTo(1); @@ -141,9 +139,7 @@ void canLocateNodesWithXPathLocator() { driver.get(pages.xhtmlTestPage); LocateNodeParameters parameters = - new LocateNodeParameters.Builder(Locator.xpath("/html/body/div[2]")) - .setMaxNodeCount(1) - .build(); + new LocateNodeParameters(Locator.xpath("/html/body/div[2]")).setMaxNodeCount(1); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isGreaterThanOrEqualTo(1); @@ -170,9 +166,7 @@ void canLocateNodesWithInnerText() { driver.get(pages.xhtmlTestPage); LocateNodeParameters parameters = - new LocateNodeParameters.Builder(Locator.innerText("Spaced out")) - .setMaxNodeCount(1) - .build(); + new LocateNodeParameters(Locator.innerText("Spaced out")).setMaxNodeCount(1); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isGreaterThanOrEqualTo(1); @@ -194,7 +188,7 @@ void canLocateNodesWithMaxNodeCount() { driver.get(pages.xhtmlTestPage); LocateNodeParameters parameters = - new LocateNodeParameters.Builder(Locator.css("div")).setMaxNodeCount(4).build(); + new LocateNodeParameters(Locator.css("div")).setMaxNodeCount(4); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isEqualTo(4); @@ -212,9 +206,7 @@ void canLocateNodesWithNoneOwnershipParameter() { driver.get(pages.xhtmlTestPage); LocateNodeParameters parameters = - new LocateNodeParameters.Builder(Locator.css("div")) - .setOwnership(ResultOwnership.NONE) - .build(); + new LocateNodeParameters(Locator.css("div")).setOwnership(ResultOwnership.NONE); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isEqualTo(13); @@ -233,9 +225,7 @@ void canLocateNodesWithRootOwnershipParameter() { driver.get(pages.xhtmlTestPage); LocateNodeParameters parameters = - new LocateNodeParameters.Builder(Locator.css("div")) - .setOwnership(ResultOwnership.ROOT) - .build(); + new LocateNodeParameters(Locator.css("div")).setOwnership(ResultOwnership.ROOT); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isEqualTo(13); @@ -276,10 +266,9 @@ void canLocateNodesGivenStartNodes() { new RemoteReference(RemoteReference.Type.SHARED_ID, value.getSharedId().get()))); LocateNodeParameters parameters = - new LocateNodeParameters.Builder(Locator.css("input")) + new LocateNodeParameters(Locator.css("input")) .setStartNodes(startNodes) - .setMaxNodeCount(50) - .build(); + .setMaxNodeCount(50); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isEqualTo(35); @@ -298,10 +287,7 @@ void canLocateNodesInAGivenSandbox() { browsingContext.navigate(pages.xhtmlTestPage, ReadinessState.COMPLETE); LocateNodeParameters parameters = - new LocateNodeParameters.Builder(Locator.css("div")) - .setSandbox(sandbox) - .setMaxNodeCount(1) - .build(); + new LocateNodeParameters(Locator.css("div")).setSandbox(sandbox).setMaxNodeCount(1); List elements = browsingContext.locateNodes(parameters); assertThat(elements.size()).isEqualTo(1);