Skip to content

Commit

Permalink
[bidi][java] Add locate nodes command (#13445)
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani authored Jan 16, 2024
1 parent f09f064 commit c9bc81a
Show file tree
Hide file tree
Showing 5 changed files with 551 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.openqa.selenium.bidi.browsingcontext;

import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -28,6 +29,7 @@
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.script.RemoteValue;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
Expand All @@ -36,6 +38,8 @@

public class BrowsingContext {

private static final Json JSON = new Json();

private final String id;
private final BiDi bidi;
private static final String CONTEXT = "context";
Expand Down Expand Up @@ -338,6 +342,22 @@ public void forward() {
this.traverseHistory(1);
}

public List<RemoteValue> locateNodes(LocateNodeParameters parameters) {
Map<String, Object> params = new HashMap<>(parameters.toMap());
params.put("context", id);
return this.bidi.send(
new Command<>(
"browsingContext.locateNodes",
params,
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
try (StringReader reader = new StringReader(JSON.toJson(result.get("nodes")));
JsonInput input = JSON.newInput(reader)) {
return input.read(new TypeToken<List<RemoteValue>>() {}.getType());
}
}));
}

public void close() {
// This might need more clean up actions once the behavior is defined.
// Specially when last tab or window is closed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.openqa.selenium.bidi.script.RemoteReference;
import org.openqa.selenium.bidi.script.ResultOwnership;
import org.openqa.selenium.bidi.script.SerializationOptions;

public class LocateNodeParameters {

private final Locator locator;
private final Optional<Long> maxNodeCount;
private final Optional<ResultOwnership> ownership;
private final Optional<String> sandbox;
private final Optional<SerializationOptions> serializationOptions;
private final Optional<List<RemoteReference>> startNodes;

private LocateNodeParameters(Builder builder) {
this.locator = builder.locator;
this.maxNodeCount = Optional.ofNullable(builder.maxNodeCount);
this.ownership = Optional.ofNullable(builder.ownership);
this.sandbox = Optional.ofNullable(builder.sandbox);
this.serializationOptions = Optional.ofNullable(builder.serializationOptions);
this.startNodes = Optional.ofNullable(builder.startNodes);
}

public Map<String, Object> toMap() {
final Map<String, Object> map = new HashMap<>();

map.put("locator", locator.toMap());
maxNodeCount.ifPresent(value -> map.put("maxNodeCount", value));
ownership.ifPresent(value -> map.put("ownership", value.toString()));
sandbox.ifPresent(value -> map.put("sandbox", value));
serializationOptions.ifPresent(value -> map.put("serializationOptions", value.toJson()));
startNodes.ifPresent(
value -> {
List<Map<String, Object>> startNodesJson = new ArrayList<>();
value.forEach(remoteNode -> startNodesJson.add(remoteNode.toJson()));
map.put("startNodes", startNodesJson);
});

return map;
}

public static class Builder {

private final Locator locator;
private Long maxNodeCount = null;
private ResultOwnership ownership;
private String sandbox;
private SerializationOptions serializationOptions;
private List<RemoteReference> startNodes;

public Builder(Locator locator) {
this.locator = locator;
}

public Builder setMaxNodeCount(long maxNodeCount) {
this.maxNodeCount = maxNodeCount;
return this;
}

public Builder setOwnership(ResultOwnership ownership) {
this.ownership = ownership;
return this;
}

public Builder setSandbox(String sandbox) {
this.sandbox = sandbox;
return this;
}

public Builder setSerializationOptions(SerializationOptions options) {
this.serializationOptions = options;
return this;
}

public Builder setStartNodes(List<RemoteReference> startNodes) {
this.startNodes = startNodes;
return this;
}

public LocateNodeParameters build() {
return new LocateNodeParameters(this);
}
}
}
101 changes: 101 additions & 0 deletions java/src/org/openqa/selenium/bidi/browsingcontext/Locator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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 java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class Locator {
private enum Type {
CSS("css"),
INNER("innerText"),
XPATH("xpath");

private final String value;

Type(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}

private final Type type;

private final String value;

private Optional<Boolean> ignoreCase = Optional.empty();

private Optional<String> matchType = Optional.empty();

private Optional<Long> maxDepth = Optional.empty();

private Locator(Type type, String value) {
this.type = type;
this.value = value;
}

public Locator(
Type type,
String value,
Optional<Boolean> ignoreCase,
Optional<String> matchType,
Optional<Long> maxDepth) {
this.type = type;
this.value = value;
this.ignoreCase = ignoreCase;
this.matchType = matchType;
this.maxDepth = maxDepth;
}

public static Locator css(String value) {
return new Locator(Type.CSS, value);
}

public static Locator innerText(
String value,
Optional<Boolean> ignoreCase,
Optional<String> matchType,
Optional<Long> maxDepth) {
return new Locator(Type.INNER, value, ignoreCase, matchType, maxDepth);
}

public static Locator innerText(String value) {
return new Locator(Type.INNER, value);
}

public static Locator xpath(String value) {
return new Locator(Type.XPATH, value);
}

public Map<String, Object> toMap() {
final Map<String, Object> map = new HashMap<>();
map.put("type", type.toString());
map.put("value", value);

ignoreCase.ifPresent(val -> map.put("ignoreCase", val));
matchType.ifPresent(val -> map.put("matchType", val));
maxDepth.ifPresent(val -> map.put("maxDepth", val));

return map;
}
}
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/bidi/script/RemoteValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ private static Object deserializeValue(Object value, Type type) {

switch (type) {
case ARRAY:
case NODE_LIST:
case SET:
try (StringReader reader = new StringReader(JSON.toJson(value));
JsonInput input = JSON.newInput(reader)) {
Expand Down
Loading

0 comments on commit c9bc81a

Please sign in to comment.