|
| 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 | +} |
0 commit comments