...40import org.openqa.selenium.WebElement;41import org.openqa.selenium.interactions.internal.Coordinates;42import org.openqa.selenium.remote.FileDetector;43import org.openqa.selenium.remote.RemoteWebDriver;44import org.openqa.selenium.remote.RemoteWebElement;45import com.neotys.selenium.proxies.helpers.ProxySendHelper;46import com.neotys.selenium.proxies.helpers.SeleniumProxyConfig;47import com.neotys.selenium.proxies.helpers.WrapperUtils;48/** We tried to avoid using direct wrapper methods like this, but we found a case where an "instanceof" check was done49 * for a @RemoteWebElement in @org.openqa.selenium.remote.internal.WebElementToJsonConverter#apply(). */50public class RemoteWebElementWrapper extends RemoteWebElement {51 final WebDriver webDriver;52 final SeleniumProxyConfig proxyConfig;53 final ProxySendHelper proxySendHelper;54 final WrapperUtils wrapperUtils;55 final RemoteWebElement original;56 private static final List<String> METHODS_SEND_ON_EXCEPTION_ONLY = WebElementProxy.METHODS_SEND_ON_EXCEPTION_ONLY;57 private static final List<String> METHODS_ALWAYS_SEND = WebElementProxy.METHODS_ALWAYS_SEND;58 public RemoteWebElementWrapper(final WebDriver webDriver, final RemoteWebElement original, final SeleniumProxyConfig proxyConfig){59 this.webDriver = webDriver;60 this.proxyConfig = proxyConfig;61 this.proxySendHelper = new ProxySendHelper(proxyConfig);62 this.wrapperUtils = new WrapperUtils(proxyConfig);63 this.original = original;64 }65 66 @Override67 public void setFileDetector(FileDetector detector) {68 original.setFileDetector(detector);69 }70 71 @Override72 public void setId(String id) {73 original.setId(id);74 }75 76 @Override77 public void setParent(RemoteWebDriver parent) {78 super.setParent(parent);79 original.setParent(parent);80 }81 82 @Override83 public void click() {84 if (!SeleniumProxyConfig.isEnabled()) {85 original.click();86 return;87 }88 try {89 final Method method = RemoteWebElement.class.getDeclaredMethod("click", (Class<?>[])null);90 proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),91 webDriver, original, method, (Object[])null);92 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {93 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);94 }95 }96 @Override97 public void submit() {98 if (!SeleniumProxyConfig.isEnabled()) {99 original.submit();100 return;101 }102 try {103 final Method method = RemoteWebElement.class.getDeclaredMethod("submit", (Class<?>[])null);104 proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),105 webDriver, original, method, (Object[])null);106 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {107 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);108 }109 }110 @Override111 public String getTagName() {112 if (!SeleniumProxyConfig.isEnabled()) {113 return original.getTagName();114 }115 try {116 final Method method = RemoteWebElement.class.getDeclaredMethod("getTagName", (Class<?>[])null);117 return (String) proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),118 webDriver, original, method, (Object[])null);119 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {120 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);121 }122 }123 @Override124 public String getAttribute(final String name) {125 if (!SeleniumProxyConfig.isEnabled()) {126 return original.getAttribute(name);127 }128 try {129 final Method method = RemoteWebElement.class.getDeclaredMethod("getAttribute", (new Class<?>[]{String.class}));130 return (String) proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),131 webDriver, original, method, new Object[]{name});132 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {133 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);134 } 135 }136 @Override137 public List<WebElement> findElements(final By by) {138 if (!SeleniumProxyConfig.isEnabled()) {139 return original.findElements(by);140 }141 try {142 final Method method = RemoteWebElement.class.getDeclaredMethod("findElements", (new Class<?>[]{By.class}));143 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),144 webDriver, original, method, new Object[]{by});145 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {146 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);147 } 148 }149 @Override150 public WebElement findElement(final By by) {151 if (!SeleniumProxyConfig.isEnabled()) {152 return original.findElement(by);153 }154 try {155 final Method method = RemoteWebElement.class.getDeclaredMethod("findElement", (new Class<?>[]{By.class}));156 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),157 webDriver, original, method, new Object[]{by});158 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {159 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);160 } 161 }162 @Override163 public WebElement findElementById(final String using) {164 if (!SeleniumProxyConfig.isEnabled()) {165 return original.findElementById(using);166 }167 try {168 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementById", (new Class<?>[]{String.class}));169 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),170 webDriver, original, method, new Object[]{using});171 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {172 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);173 } 174 }175 @Override176 public List<WebElement> findElementsById(final String using) {177 if (!SeleniumProxyConfig.isEnabled()) {178 return original.findElementsById(using);179 }180 try {181 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementsById", (new Class<?>[]{String.class}));182 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),183 webDriver, original, method, new Object[]{using});184 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {185 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);186 } 187 }188 @Override189 public WebElement findElementByLinkText(final String using) {190 if (!SeleniumProxyConfig.isEnabled()) {191 return original.findElementByLinkText(using);192 }193 try {194 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementByLinkText", (new Class<?>[]{String.class}));195 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),196 webDriver, original, method, new Object[]{using});197 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {198 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);199 } 200 }201 @Override202 public List<WebElement> findElementsByLinkText(final String using) {203 if (!SeleniumProxyConfig.isEnabled()) {204 return original.findElementsByLinkText(using);205 }206 try {207 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementsByLinkText", (new Class<?>[]{String.class}));208 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),209 webDriver, original, method, new Object[]{using});210 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {211 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);212 } 213 }214 @Override215 public WebElement findElementByName(final String using) {216 if (!SeleniumProxyConfig.isEnabled()) {217 return original.findElementByName(using);218 }219 try {220 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementByName", (new Class<?>[]{String.class}));221 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),222 webDriver, original, method, new Object[]{using});223 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {224 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);225 } 226 }227 @Override228 public List<WebElement> findElementsByName(final String using) {229 if (!SeleniumProxyConfig.isEnabled()) {230 return original.findElementsByName(using);231 }232 try {233 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementsByName", (new Class<?>[]{String.class}));234 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),235 webDriver, original, method, new Object[]{using});236 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {237 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);238 } 239 }240 @Override241 public WebElement findElementByClassName(final String using) {242 if (!SeleniumProxyConfig.isEnabled()) {243 return original.findElementByClassName(using);244 }245 try {246 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementByClassName", (new Class<?>[]{String.class}));247 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),248 webDriver, original, method, new Object[]{using});249 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {250 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);251 } 252 }253 @Override254 public List<WebElement> findElementsByClassName(final String using) {255 if (!SeleniumProxyConfig.isEnabled()) {256 return original.findElementsByClassName(using);257 }258 try {259 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementsByClassName", (new Class<?>[]{String.class}));260 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),261 webDriver, original, method, new Object[]{using});262 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {263 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);264 } 265 }266 @Override267 public WebElement findElementByCssSelector(final String using) {268 if (!SeleniumProxyConfig.isEnabled()) {269 return original.findElementByCssSelector(using);270 }271 try {272 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementByCssSelector", (new Class<?>[]{String.class}));273 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),274 webDriver, original, method, new Object[]{using});275 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {276 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);277 } 278 }279 @Override280 public List<WebElement> findElementsByCssSelector(final String using) {281 if (!SeleniumProxyConfig.isEnabled()) {282 return original.findElementsByCssSelector(using);283 }284 try {285 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementsByCssSelector", (new Class<?>[]{String.class}));286 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),287 webDriver, original, method, new Object[]{using});288 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {289 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);290 } 291 }292 @Override293 public WebElement findElementByXPath(final String using) {294 if (!SeleniumProxyConfig.isEnabled()) {295 return original.findElementByXPath(using);296 }297 try {298 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementByXPath", (new Class<?>[]{String.class}));299 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),300 webDriver, original, method, new Object[]{using});301 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {302 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);303 } 304 }305 @Override306 public List<WebElement> findElementsByXPath(final String using) {307 if (!SeleniumProxyConfig.isEnabled()) {308 return original.findElementsByXPath(using);309 }310 try {311 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementsByXPath", (new Class<?>[]{String.class}));312 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),313 webDriver, original, method, new Object[]{using});314 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {315 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);316 } 317 }318 @Override319 public WebElement findElementByPartialLinkText(final String using) {320 if (!SeleniumProxyConfig.isEnabled()) {321 return original.findElementByPartialLinkText(using);322 }323 try {324 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementByPartialLinkText", (new Class<?>[]{String.class}));325 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),326 webDriver, original, method, new Object[]{using});327 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {328 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);329 } 330 }331 @Override332 public List<WebElement> findElementsByPartialLinkText(final String using) {333 if (!SeleniumProxyConfig.isEnabled()) {334 return original.findElementsByPartialLinkText(using);335 }336 try {337 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementsByPartialLinkText", (new Class<?>[]{String.class}));338 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),339 webDriver, original, method, new Object[]{using});340 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {341 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);342 } 343 }344 @Override345 public WebElement findElementByTagName(final String using) {346 if (!SeleniumProxyConfig.isEnabled()) {347 return original.findElementByTagName(using);348 }349 try {350 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementByTagName", (new Class<?>[]{String.class}));351 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),352 webDriver, original, method, new Object[]{using});353 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {354 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);355 } 356 }357 @Override358 public List<WebElement> findElementsByTagName(final String using) {359 if (!SeleniumProxyConfig.isEnabled()) {360 return original.findElementsByTagName(using);361 }362 try {363 final Method method = RemoteWebElement.class.getDeclaredMethod("findElementsByTagName", (new Class<?>[]{String.class}));364 return proxySendHelper.sendAndReturn(METHODS_ALWAYS_SEND, METHODS_SEND_ON_EXCEPTION_ONLY, Collections.<String>emptyList(),365 webDriver, original, method, new Object[]{using});366 } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {367 throw new NeotysWrappingException("Issue with NeoLoad proxy.", e);368 } 369 }370 @Override371 public WebDriver getWrappedDriver() {372 if (!SeleniumProxyConfig.isEnabled()) {373 return original.getWrappedDriver();374 }375 return (WebDriver) wrapperUtils.wrapIfNecessary(webDriver, super.getWrappedDriver());376 }377 @Override378 protected Object clone() throws CloneNotSupportedException {379 if (!SeleniumProxyConfig.isEnabled()) {380 return super.clone();381 }382 return wrapperUtils.wrapIfNecessary(webDriver, super.clone());383 }384 // automatically generated wrapper methods --------------------------------------------------------385 /**386 * @return387 * @see org.openqa.selenium.remote.RemoteWebElement#getId()388 */389 @Override390 public String getId() {391 return original.getId();392 }393 /**394 * @param keysToSend395 * @see org.openqa.selenium.remote.RemoteWebElement#sendKeys(java.lang.CharSequence[])396 */397 @Override398 public void sendKeys(CharSequence... keysToSend) {399 original.sendKeys(keysToSend);400 }401 /**402 * 403 * @see org.openqa.selenium.remote.RemoteWebElement#clear()404 */405 @Override406 public void clear() {407 original.clear();408 }409 /**410 * @return411 * @see org.openqa.selenium.remote.RemoteWebElement#isSelected()412 */413 @Override414 public boolean isSelected() {415 return original.isSelected();416 }417 /**418 * @return419 * @see org.openqa.selenium.remote.RemoteWebElement#isEnabled()420 */421 @Override422 public boolean isEnabled() {423 return original.isEnabled();424 }425 /**426 * @return427 * @see org.openqa.selenium.remote.RemoteWebElement#getText()428 */429 @Override430 public String getText() {431 return original.getText();432 }433 /**434 * @param propertyName435 * @return436 * @see org.openqa.selenium.remote.RemoteWebElement#getCssValue(java.lang.String)437 */438 @Override439 public String getCssValue(String propertyName) {440 return original.getCssValue(propertyName);441 }442 /**443 * @param obj444 * @return445 * @see org.openqa.selenium.remote.RemoteWebElement#equals(java.lang.Object)446 */447 @Override448 public boolean equals(Object obj) {449 return original.equals(obj);450 }451 /**452 * @return453 * @see org.openqa.selenium.remote.RemoteWebElement#hashCode()454 */455 @Override456 public int hashCode() {457 return original.hashCode();458 }459 /**460 * @return461 * @see org.openqa.selenium.remote.RemoteWebElement#isDisplayed()462 */463 @Override464 public boolean isDisplayed() {465 return original.isDisplayed();466 }467 /**468 * @return469 * @see org.openqa.selenium.remote.RemoteWebElement#getLocation()470 */471 @Override472 public Point getLocation() {473 return original.getLocation();474 }475 /**476 * @return477 * @see org.openqa.selenium.remote.RemoteWebElement#getSize()478 */479 @Override480 public Dimension getSize() {481 return original.getSize();482 }483 /**484 * @return485 * @see org.openqa.selenium.remote.RemoteWebElement#getRect()486 */487 @Override488 public Rectangle getRect() {489 return original.getRect();490 }491 /**492 * @return493 * @see org.openqa.selenium.remote.RemoteWebElement#getCoordinates()494 */495 @Override496 public Coordinates getCoordinates() {497 return original.getCoordinates();498 }499 /**500 * @param outputType501 * @return502 * @throws WebDriverException503 * @see org.openqa.selenium.remote.RemoteWebElement#getScreenshotAs(org.openqa.selenium.OutputType)504 */505 @Override506 public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException {507 return original.getScreenshotAs(outputType);508 }509 /**510 * @return511 * @see org.openqa.selenium.remote.RemoteWebElement#toString()512 */513 @Override514 public String toString() {515 return original.toString();516 }517}...