|
| 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.remote; |
| 19 | + |
| 20 | +import static java.util.stream.Collectors.toList; |
| 21 | + |
| 22 | +import java.lang.reflect.Array; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.function.Function; |
| 29 | +import org.openqa.selenium.WrapsElement; |
| 30 | + |
| 31 | +/** |
| 32 | + * Converts {@link RemoteWebElement} objects, which may be {@link WrapsElement wrapped}, into their |
| 33 | + * JSON representation as defined by the WebDriver wire protocol. This class will recursively |
| 34 | + * convert Lists and Maps to catch nested references. |
| 35 | + * |
| 36 | + * @see <a |
| 37 | + * href="https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#webelement-json-object"> |
| 38 | + * WebDriver JSON Wire Protocol</a> |
| 39 | + */ |
| 40 | +public class WebElementToJsonConverter implements Function<Object, Object> { |
| 41 | + @Override |
| 42 | + public Object apply(Object arg) { |
| 43 | + if (arg == null || arg instanceof String || arg instanceof Boolean || arg instanceof Number) { |
| 44 | + return arg; |
| 45 | + } |
| 46 | + |
| 47 | + while (arg instanceof WrapsElement) { |
| 48 | + arg = ((WrapsElement) arg).getWrappedElement(); |
| 49 | + } |
| 50 | + |
| 51 | + if (arg instanceof RemoteWebElement) { |
| 52 | + return Map.of(Dialect.W3C.getEncodedElementKey(), ((RemoteWebElement) arg).getId()); |
| 53 | + } |
| 54 | + |
| 55 | + if (arg instanceof ShadowRoot) { |
| 56 | + return Map.of(Dialect.W3C.getShadowRootElementKey(), ((ShadowRoot) arg).getId()); |
| 57 | + } |
| 58 | + |
| 59 | + if (arg.getClass().isArray()) { |
| 60 | + arg = arrayToList(arg); |
| 61 | + } |
| 62 | + |
| 63 | + if (arg instanceof Collection<?>) { |
| 64 | + Collection<?> args = (Collection<?>) arg; |
| 65 | + return args.stream().map(this).collect(toList()); |
| 66 | + } |
| 67 | + |
| 68 | + if (arg instanceof Map<?, ?>) { |
| 69 | + Map<?, ?> args = (Map<?, ?>) arg; |
| 70 | + Map<String, Object> converted = new HashMap<>(args.size()); |
| 71 | + for (Map.Entry<?, ?> entry : args.entrySet()) { |
| 72 | + Object key = entry.getKey(); |
| 73 | + if (!(key instanceof String)) { |
| 74 | + throw new IllegalArgumentException( |
| 75 | + "All keys in Map script arguments must be strings: " + key.getClass().getName()); |
| 76 | + } |
| 77 | + converted.put((String) key, apply(entry.getValue())); |
| 78 | + } |
| 79 | + return converted; |
| 80 | + } |
| 81 | + |
| 82 | + throw new IllegalArgumentException( |
| 83 | + "Argument is of an illegal type: " + arg.getClass().getName()); |
| 84 | + } |
| 85 | + |
| 86 | + private static List<Object> arrayToList(Object array) { |
| 87 | + List<Object> list = new ArrayList<>(); |
| 88 | + for (int i = 0; i < Array.getLength(array); i++) { |
| 89 | + list.add(Array.get(array, i)); |
| 90 | + } |
| 91 | + return list; |
| 92 | + } |
| 93 | +} |
0 commit comments