| 39 | |
| 40 | // This will make porting from W3C WebDriver classic to BiDi seamless for Actions |
| 41 | @SuppressWarnings("unchecked") |
| 42 | public void perform(String browsingContext, Collection<Sequence> actions) { |
| 43 | |
| 44 | // This step is needed to map the origin if it's an element to the key expected by BiDi |
| 45 | List<Map<String, Object>> encodedActions = |
| 46 | actions.stream().map(Sequence::encode).collect(Collectors.toList()); |
| 47 | |
| 48 | encodedActions.forEach( |
| 49 | encodedAction -> { |
| 50 | String type = (String) encodedAction.get("type"); |
| 51 | // Element as origin is only possible for input pointer or wheel |
| 52 | if (type.equals("pointer") || type.equals("wheel")) { |
| 53 | List<Map<String, Object>> actionList = |
| 54 | (List<Map<String, Object>>) encodedAction.get("actions"); |
| 55 | |
| 56 | actionList.stream() |
| 57 | .filter( |
| 58 | action -> |
| 59 | // For pointer only pointMove action can have element as origin |
| 60 | action.get("type").equals("pointerMove") |
| 61 | || action.get("type").equals("scroll")) |
| 62 | .filter(action -> action.get("origin") instanceof WebElement) |
| 63 | .forEach( |
| 64 | action -> { |
| 65 | Object element = action.get("origin"); |
| 66 | try { |
| 67 | // Using reflection because adding RemoteWebElement as a dependency creates |
| 68 | // a circular dependency in Bazel |
| 69 | String id = (String) element.getClass().getMethod("getId").invoke(element); |
| 70 | // sharedId is required by BiDi, the reason for this step |
| 71 | action.put( |
| 72 | "origin", Map.of("type", "element", "element", Map.of("sharedId", id))); |
| 73 | } catch (NoSuchMethodException |
| 74 | | InvocationTargetException |
| 75 | | IllegalAccessException e) { |
| 76 | throw new RuntimeException(e); |
| 77 | } |
| 78 | }); |
| 79 | } |
| 80 | }); |
| 81 | bidi.send( |
| 82 | new Command<>( |
| 83 | "input.performActions", |
| 84 | Map.of( |
| 85 | "context", browsingContext, |
| 86 | "actions", encodedActions))); |
| 87 | } |
| 88 | |
| 89 | public void release(String browsingContext) { |
| 90 | bidi.send(new Command<>("input.releaseActions", Map.of("context", browsingContext))); |