Skip to content

Commit

Permalink
[bidi][java] Add execute script high-level API
Browse files Browse the repository at this point in the history
Related to SeleniumHQ#13992
  • Loading branch information
pujagani committed Aug 1, 2024
1 parent 1ad8846 commit 057a7a9
Show file tree
Hide file tree
Showing 4 changed files with 452 additions and 0 deletions.
84 changes: 84 additions & 0 deletions java/src/org/openqa/selenium/bidi/script/LocalValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@

package org.openqa.selenium.bidi.script;

import java.math.BigInteger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.openqa.selenium.json.Json;

public abstract class LocalValue {

private static Json JSON = new Json();

enum SpecialNumberType {
NAN("NaN"),
MINUS_ZERO("-0"),
Expand Down Expand Up @@ -123,4 +131,80 @@ public static LocalValue remoteReference(String handle, String sharedId) {
public static LocalValue remoteReference(RemoteReference.Type type, String id) {
return new RemoteReference(type, id);
}

public static LocalValue getArgument(Object arg) {
LocalValue localValue = null;

if (arg instanceof String) {
switch ((String) arg) {
case "undefined":
localValue = undefinedValue();
break;
case "null":
localValue = nullValue();
break;
case "-Infinity":
localValue = numberValue(SpecialNumberType.MINUS_INFINITY);
break;
case "Infinity":
localValue = numberValue(SpecialNumberType.INFINITY);
break;
case "NaN":
localValue = numberValue(SpecialNumberType.NAN);
break;
case "-0":
localValue = numberValue(SpecialNumberType.MINUS_ZERO);
break;
default:
localValue = stringValue((String) arg);
break;
}
} else if (arg instanceof Number) {
if (arg instanceof Integer || arg instanceof Long) {
localValue = numberValue(((Number) arg).longValue());
} else if (arg instanceof Double || arg instanceof Float) {
localValue = numberValue(((Number) arg).doubleValue());
} else if (arg instanceof BigInteger) {
localValue = bigIntValue(arg.toString());
}
} else if (arg instanceof Boolean) {
localValue = booleanValue((Boolean) arg);
} else if (arg instanceof Instant) {
localValue = dateValue(((Instant) arg).toString());
} else if (arg instanceof Map) {
Map<Object, LocalValue> map = new HashMap<>();
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) arg).entrySet()) {
Object key =
(entry.getKey() instanceof String) ? entry.getKey() : getArgument(entry.getKey());
map.put(key, getArgument(entry.getValue()));
}
localValue = mapValue(map);
} else if (arg instanceof List) {
List<LocalValue> values = new ArrayList<>();
((List<Object>) arg).forEach(value -> values.add(getArgument(value)));
localValue = arrayValue(values);
} else if (arg instanceof Set) {
Set<LocalValue> values = new HashSet<>();
((Set<Object>) arg).forEach(value -> values.add(getArgument(value)));
localValue = setValue(values);
} else if (arg instanceof RegExpValue) {
localValue = (RegExpValue) arg;
} else {
String json = JSON.toJson(arg);
Map<Object, Object> objectMap = JSON.toType(json, Map.class);

Map<Object, LocalValue> map = new HashMap<>();

for (Map.Entry<Object, Object> entry : objectMap.entrySet()) {
Object key =
(entry.getKey() instanceof String) ? entry.getKey() : getArgument(entry.getKey());
map.put(key, getArgument(entry.getValue()));
}
localValue = objectValue(map);

return localValue;
}

return localValue;
}
}
35 changes: 35 additions & 0 deletions java/src/org/openqa/selenium/remote/RemoteScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import org.openqa.selenium.Beta;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
import org.openqa.selenium.bidi.module.LogInspector;
import org.openqa.selenium.bidi.script.ChannelValue;
import org.openqa.selenium.bidi.script.EvaluateResult;
import org.openqa.selenium.bidi.script.EvaluateResultExceptionValue;
import org.openqa.selenium.bidi.script.EvaluateResultSuccess;
import org.openqa.selenium.bidi.script.LocalValue;
import org.openqa.selenium.bidi.script.RemoteValue;
import org.openqa.selenium.json.Json;

@Beta
Expand Down Expand Up @@ -131,4 +140,30 @@ public String pin(String script) {
public void unpin(String id) {
this.script.removePreloadScript(id);
}

@Override
public RemoteValue execute(String script, Object... args) {
String browsingContextId = this.driver.getWindowHandle();

List<LocalValue> arguments = new ArrayList<>();

Arrays.stream(args).forEach(arg -> arguments.add(LocalValue.getArgument(arg)));

EvaluateResult result =
this.script.callFunctionInBrowsingContext(
browsingContextId,
script,
true,
Optional.of(arguments),
Optional.empty(),
Optional.empty());

if (result.getResultType().equals(EvaluateResult.Type.SUCCESS)) {
return ((EvaluateResultSuccess) result).getResult();
} else {
throw new WebDriverException(
"Error while executing script: "
+ ((EvaluateResultExceptionValue) result).getExceptionDetails().getText());
}
}
}
3 changes: 3 additions & 0 deletions java/src/org/openqa/selenium/remote/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openqa.selenium.Beta;
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
import org.openqa.selenium.bidi.script.RemoteValue;

@Beta
public interface Script {
Expand All @@ -40,4 +41,6 @@ public interface Script {
String pin(String script);

void unpin(String id);

RemoteValue execute(String script, Object... args);
}
Loading

0 comments on commit 057a7a9

Please sign in to comment.