Enhance the interfaces implemented by this instance of WebDriver if that instance is a org.openqa.selenium.remote.RemoteWebDriver. The WebDriver that is returned may well be a dynamic proxy. You cannot rely on the concrete implementing class to remain constant. @param driver The driver to e
(WebDriver driver)
| 142 | * @return A class implementing the described interfaces. |
| 143 | */ |
| 144 | public WebDriver augment(WebDriver driver) { |
| 145 | Require.nonNull("WebDriver", driver); |
| 146 | Require.precondition( |
| 147 | driver instanceof HasCapabilities, "Driver must have capabilities", driver); |
| 148 | |
| 149 | if (driver instanceof Decorated<?>) { |
| 150 | LOG.warning( |
| 151 | "Warning: In future versions, passing a decorated driver will no longer be allowed.\n" |
| 152 | + " Instead, augment the driver first and then use it to created a decorated" |
| 153 | + " driver.\n" |
| 154 | + " Explanation: Decorated drivers are not aware of the augmentations applied to" |
| 155 | + " them. It can lead to expected behavior.\n" |
| 156 | + " For example, augmenting HasDevTools interface to a decorated driver. \n" |
| 157 | + " The decorated driver is not aware that after augmentation it is an instance of" |
| 158 | + " HasDevTools. So it does not invoke the close() method of the underlying" |
| 159 | + " websocket, potentially causing a memory leak. "); |
| 160 | } |
| 161 | |
| 162 | Capabilities caps = ImmutableCapabilities.copyOf(((HasCapabilities) driver).getCapabilities()); |
| 163 | |
| 164 | // Collect the interfaces to apply |
| 165 | List<Augmentation<?>> matchingAugmenters = |
| 166 | augmentations.stream() |
| 167 | // Only consider the augmenters that match interfaces we don't already implement |
| 168 | .filter( |
| 169 | augmentation -> !augmentation.interfaceClass.isAssignableFrom(driver.getClass())) |
| 170 | // And which match an augmentation we have |
| 171 | .filter(augmentation -> augmentation.whenMatches.test(caps)) |
| 172 | .collect(Collectors.toList()); |
| 173 | |
| 174 | return this.augment(driver, matchingAugmenters); |
| 175 | } |
| 176 | |
| 177 | private WebDriver augment(WebDriver driver, List<Augmentation<?>> matchingAugmenters) { |
| 178 | Capabilities caps = ImmutableCapabilities.copyOf(((HasCapabilities) driver).getCapabilities()); |