Guest User

Untitled

a guest
Feb 28th, 2012
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. /*
  2. Copyright 2007-2009 WebDriver committers
  3. Copyright 2007-2009 Google Inc.
  4.  
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. 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, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16.  */
  17.  
  18. package org.openqa.selenium.support.pagefactory;
  19.  
  20. import org.openqa.selenium.WebElement;
  21. import org.openqa.selenium.internal.Locatable;
  22. import org.openqa.selenium.internal.WrapsElement;
  23. import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;
  24.  
  25. import java.lang.reflect.Field;
  26. import java.lang.reflect.InvocationHandler;
  27. import java.lang.reflect.Proxy;
  28.  
  29. public class CustomFieldDecorator implements FieldDecorator {
  30.  
  31.   protected ElementLocatorFactory factory;
  32.  
  33.   public CustomFieldDecorator(ElementLocatorFactory factory) {
  34.     this.factory = factory;
  35.   }
  36.  
  37.   public Object decorate(ClassLoader loader, Field field) {
  38.     if (!WebElement.class.isAssignableFrom(field.getType())) {
  39.       return null;
  40.     }
  41.  
  42.     ElementLocator locator = factory.createLocator(field);
  43.     if (locator == null) {
  44.       return null;
  45.     }
  46.  
  47.     return proxyForLocator(loader, locator, field);
  48.   }
  49.  
  50.   protected Object proxyForLocator(ClassLoader loader,
  51.       ElementLocator locator, Field field) {
  52.     InvocationHandler handler = new LocatingElementHandler(locator);
  53.  
  54.     WebElement proxy;
  55.     proxy = (WebElement) Proxy.newProxyInstance(
  56.         loader, new Class[] {WebElement.class, WrapsElement.class, Locatable.class}, handler);
  57.     if(field.getType() == Button.class){
  58.         return new Button(proxy);
  59.     }
  60.     return proxy;
  61.   }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment