| 37 | import org.openqa.selenium.remote.http.HttpClient; |
| 38 | |
| 39 | @SuppressWarnings({"rawtypes", "RedundantSuppression"}) |
| 40 | @AutoService(AugmenterProvider.class) |
| 41 | @Beta |
| 42 | @ApiStatus.Internal |
| 43 | public class BiDiProvider implements AugmenterProvider<HasBiDi> { |
| 44 | private static final Logger LOG = Logger.getLogger(BiDiProvider.class.getName()); |
| 45 | |
| 46 | @Override |
| 47 | public Predicate<Capabilities> isApplicable() { |
| 48 | return caps -> getBiDiUrl(caps).isPresent(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public Class<HasBiDi> getDescribedInterface() { |
| 53 | return HasBiDi.class; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public HasBiDi getImplementation(Capabilities caps, ExecuteMethod executeMethod) { |
| 58 | final Lazy<BiDi> biDi = lazy(() -> establishBiDiConnection(caps, executeMethod)); |
| 59 | |
| 60 | LOG.log( |
| 61 | INFO, |
| 62 | "WebDriver augmented with BiDi interface; connection will not be verified until first" |
| 63 | + " use."); |
| 64 | |
| 65 | return new HasBiDi() { |
| 66 | @Override |
| 67 | public Optional<BiDi> maybeGetBiDi() { |
| 68 | return biDi.getIfInitialized(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public BiDi getBiDi() { |
| 73 | return biDi.get(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Handle getHandle() { |
| 78 | return biDi.get().asHandle(); |
| 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | private BiDi establishBiDiConnection(Capabilities caps, ExecuteMethod executeMethod) { |
| 84 | URI wsUri = getBiDiUrl(caps).orElseThrow(() -> new BiDiException("BiDi not supported")); |
| 85 | ClientConfig clientConfig = |
| 86 | ((RemoteExecuteMethod) executeMethod).getWrappedDriver().getClientConfig(); |
| 87 | |
| 88 | HttpClient.Factory clientFactory = HttpClient.Factory.createDefault(); |
| 89 | ClientConfig wsConfig = clientConfig.baseUri(wsUri); |
| 90 | HttpClient wsClient = clientFactory.createClient(wsConfig); |
| 91 | Connection connection = new Connection(wsClient, wsUri.toString()); |
| 92 | |
| 93 | return new BiDi(connection, wsConfig.wsTimeout()); |
| 94 | } |
| 95 | |
| 96 | private Optional<URI> getBiDiUrl(Capabilities caps) { |