Skip to content

Commit 060288e

Browse files
authored
[bidi][java] Add support for Input module (Actions) (#13259)
1 parent 273c3d4 commit 060288e

File tree

8 files changed

+1919
-0
lines changed

8 files changed

+1919
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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;
19+
20+
import java.lang.reflect.InvocationTargetException;
21+
import java.util.Collection;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.stream.Collectors;
25+
import org.openqa.selenium.WebDriver;
26+
import org.openqa.selenium.WebElement;
27+
import org.openqa.selenium.interactions.Sequence;
28+
29+
public class Input {
30+
private final BiDi bidi;
31+
32+
public Input(WebDriver driver) {
33+
this.bidi = ((HasBiDi) driver).getBiDi();
34+
}
35+
36+
// This will make porting from W3C WebDriver classic to BiDi seamless for Actions
37+
public void perform(String browsingContext, Collection<Sequence> actions) {
38+
39+
// This step is needed to map the origin if it's an element to the key expected by BiDi
40+
List<Map<String, Object>> encodedActions =
41+
actions.stream().map(Sequence::encode).collect(Collectors.toList());
42+
43+
encodedActions.forEach(
44+
encodedAction -> {
45+
String type = (String) encodedAction.get("type");
46+
// Element as origin is only possible for input pointer or wheel
47+
if (type.equals("pointer") || type.equals("wheel")) {
48+
List<Map<String, Object>> actionList =
49+
(List<Map<String, Object>>) encodedAction.get("actions");
50+
51+
actionList.stream()
52+
.filter(
53+
action ->
54+
// For pointer only pointMove action can have element as origin
55+
action.get("type").equals("pointerMove")
56+
|| action.get("type").equals("scroll"))
57+
.filter(action -> action.get("origin") instanceof WebElement)
58+
.forEach(
59+
action -> {
60+
Object element = action.get("origin");
61+
try {
62+
// Using reflection because adding RemoteWebElement as a dependency creates
63+
// a circular dependency in Bazel
64+
String id = (String) element.getClass().getMethod("getId").invoke(element);
65+
// sharedId is required by BiDi, the reason for this step
66+
action.put(
67+
"origin", Map.of("type", "element", "element", Map.of("sharedId", id)));
68+
} catch (NoSuchMethodException
69+
| InvocationTargetException
70+
| IllegalAccessException e) {
71+
throw new RuntimeException(e);
72+
}
73+
});
74+
}
75+
});
76+
bidi.send(
77+
new Command<>(
78+
"input.performActions",
79+
Map.of(
80+
"context", browsingContext,
81+
"actions", encodedActions)));
82+
}
83+
}

java/src/org/openqa/selenium/interactions/Actions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.time.Duration;
2424
import java.util.Arrays;
25+
import java.util.Collection;
2526
import java.util.HashMap;
2627
import java.util.HashSet;
2728
import java.util.LinkedHashMap;
@@ -584,6 +585,10 @@ private Sequence getSequence(InputSource source) {
584585
return sequence;
585586
}
586587

588+
public Collection<Sequence> getSequences() {
589+
return sequences.values();
590+
}
591+
587592
private static class BuiltAction implements Action {
588593
private final WebDriver driver;
589594
private final Map<InputSource, Sequence> sequences;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
load("@rules_jvm_external//:defs.bzl", "artifact")
2+
load("//java:defs.bzl", "JUNIT5_DEPS", "java_selenium_test_suite")
3+
4+
java_selenium_test_suite(
5+
name = "large-tests",
6+
size = "large",
7+
srcs = glob(["*Test.java"]),
8+
browsers = [
9+
"firefox",
10+
],
11+
data = [
12+
"//third_party/chrome_ext:backspace.crx",
13+
],
14+
tags = [
15+
"selenium-remote",
16+
],
17+
deps = [
18+
"//java/src/org/openqa/selenium/chrome",
19+
"//java/src/org/openqa/selenium/firefox",
20+
"//java/src/org/openqa/selenium/json",
21+
"//java/src/org/openqa/selenium/remote",
22+
"//java/src/org/openqa/selenium/support",
23+
"//java/test/org/openqa/selenium:helpers",
24+
"//java/test/org/openqa/selenium/build",
25+
"//java/test/org/openqa/selenium/environment",
26+
"//java/test/org/openqa/selenium/testing:annotations",
27+
"//java/test/org/openqa/selenium/testing:test-base",
28+
"//java/test/org/openqa/selenium/testing/drivers",
29+
artifact("org.junit.jupiter:junit-jupiter-api"),
30+
artifact("org.assertj:assertj-core"),
31+
artifact("org.mockito:mockito-core"),
32+
] + JUNIT5_DEPS,
33+
)

0 commit comments

Comments
 (0)