| 33 | import org.openqa.selenium.internal.Require; |
| 34 | |
| 35 | @Beta |
| 36 | public class Browser { |
| 37 | private final BiDi bidi; |
| 38 | |
| 39 | private static final Function<@Nullable Object, String> userContextInfoMapper = |
| 40 | ConverterFunctions.map("userContext", String.class); |
| 41 | |
| 42 | private static final Function<@Nullable Object, List<String>> userContextsInfoMapper = |
| 43 | result -> { |
| 44 | List<Map<String, String>> userContexts = asList(result, "userContexts"); |
| 45 | return userContexts.stream() |
| 46 | .map(map -> map.get("userContext")) |
| 47 | .collect(Collectors.toList()); |
| 48 | }; |
| 49 | |
| 50 | private static final Function<@Nullable Object, List<ClientWindowInfo>> clientWindowsInfoMapper = |
| 51 | result -> { |
| 52 | List<Map<String, Object>> clientWindows = asList(result, "clientWindows"); |
| 53 | return clientWindows.stream().map(ClientWindowInfo::fromJson).collect(Collectors.toList()); |
| 54 | }; |
| 55 | |
| 56 | @SuppressWarnings("unchecked") |
| 57 | private static <T> List<T> asList(@Nullable Object result, String key) { |
| 58 | return (List<T>) |
| 59 | Require.nonNull( |
| 60 | "Field " + key, ((Map<?, ?>) Require.nonNull("Command result", result)).get(key)); |
| 61 | } |
| 62 | |
| 63 | public Browser(WebDriver driver) { |
| 64 | this.bidi = ((HasBiDi) driver).getBiDi(); |
| 65 | } |
| 66 | |
| 67 | public String createUserContext() { |
| 68 | return bidi.send(new Command<>("browser.createUserContext", Map.of(), userContextInfoMapper)); |
| 69 | } |
| 70 | |
| 71 | public List<String> getUserContexts() { |
| 72 | return bidi.send(new Command<>("browser.getUserContexts", Map.of(), userContextsInfoMapper)); |
| 73 | } |
| 74 | |
| 75 | public void removeUserContext(String userContext) { |
| 76 | bidi.send(new Command<>("browser.removeUserContext", Map.of("userContext", userContext))); |
| 77 | } |
| 78 | |
| 79 | public List<ClientWindowInfo> getClientWindows() { |
| 80 | return bidi.send(new Command<>("browser.getClientWindows", Map.of(), clientWindowsInfoMapper)); |
| 81 | } |
| 82 | |
| 83 | public void setDownloadBehavior(SetDownloadBehaviorParameters parameters) { |
| 84 | bidi.send(new Command<>("browser.setDownloadBehavior", parameters.toMap())); |
| 85 | } |
| 86 | } |