How to use decorate method of org.openqa.selenium.support.pagefactory.DefaultFieldDecorator class

Best Selenium code snippet using org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.decorate

Source:CustomFieldDecorator.java Github

copy

Full Screen

...15 /**16 * Метод вызывается фабрикой для каждого поля в классе17 */18 @Override19 public Object decorate(ClassLoader loader, Field field) {20 Class<IElement> decoratableClass = decoratableClass(field);21 // если класс поля декорируемый22 if (decoratableClass != null) {23 ElementLocator locator = factory.createLocator(field);24 if (locator == null) {25 return null;26 }27 if (List.class.isAssignableFrom(field.getType())) {28 return createList(loader, locator, decoratableClass);29 }30 return createElement(loader, locator, decoratableClass);31 }32 return super.decorate(loader, field);33 }34 /**35 * Возвращает декорируемый класс поля,36 * либо null если класс не подходит для декоратора37 */38 @SuppressWarnings("unchecked")39 private Class<IElement> decoratableClass(Field field) {40 Class<?> clazz = field.getType();41 if (List.class.isAssignableFrom(clazz)) {42 // для списка обязательно должна быть задана аннотация43 if (field.getAnnotation(FindBy.class) == null &&44 field.getAnnotation(FindBys.class) == null) {45 return null;46 }47 // Список должен быть параметризирован48 Type genericType = field.getGenericType();49 if (!(genericType instanceof ParameterizedType)) {50 return null;51 }52 // получаем класс для элементов списка53 clazz = (Class<?>) ((ParameterizedType) genericType).54 getActualTypeArguments()[0];55 }56 if (IElement.class.isAssignableFrom(clazz)) {57 return (Class<IElement>) clazz;58 }59 else {60 return null;61 }62 }63 /**64 * Создание элемента.65 * Находит WebElement и передает его в кастомный класс66 */67 protected IElement createElement(ClassLoader loader,68 ElementLocator locator,69 Class<IElement> clazz) {70 WebElement proxy = proxyForLocator(loader, locator);71 return WrapperFactory.createInstance(clazz, proxy);72 }73 /**74 * Создание списка75 */76 @SuppressWarnings("unchecked")77 protected List<IElement> createList(ClassLoader loader,78 ElementLocator locator,79 Class<IElement> clazz) {80 InvocationHandler handler =81 new LocatingCustomElementListHandler(locator, clazz);82 List<IElement> elements =83 (List<IElement>) Proxy.newProxyInstance(84 loader, new Class[] {List.class}, handler);85 return elements;86 }87}88/*89import org.openqa.selenium.SearchContext;90import org.openqa.selenium.WebElement;91import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;92import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;93import org.openqa.selenium.support.pagefactory.ElementLocator;94import java.lang.reflect.Field;95public class CustomFieldDecorator extends DefaultFieldDecorator {96 public CustomFieldDecorator(SearchContext searchContext) {97 super(new DefaultElementLocatorFactory(searchContext));98 }99 @Override100 public Object decorate(ClassLoader loader, Field field) {101 Class<?> decoratableClass = decoratableClass(field);102 // если класс поля декорируемый103 if (decoratableClass != null) {104 ElementLocator locator = factory.createLocator(field);105 if (locator == null) {106 return null;107 }108 // элемент109 return createElement(loader, locator, decoratableClass);110 }111 // return null;112 return super.decorate(loader, field);113 }114 private Class<?> decoratableClass(Field field) {115 Class<?> clazz = field.getType();116 // у элемента должен быть конструктор, принимающий WebElement117 try {118 clazz.getConstructor(WebElement.class);119 } catch (Exception e) {120 return null;121 }122 return clazz;123 }124 protected <T> T createElement(ClassLoader loader,125 ElementLocator locator, Class<T> clazz) {126 WebElement proxy = proxyForLocator(loader, locator);...

Full Screen

Full Screen

Source:STAFElementFieldDecorator.java Github

copy

Full Screen

...48 defaultFieldDecorator = new DefaultFieldDecorator(new DefaultElementLocatorFactory(searchContext));49 }50 /**51 * This method is called by the Selenium PageFactory on all fields to decide how52 * to decorate the field.53 *54 * @param loader The class loader that was used for the page object55 * @param field The field which should be decorated. Should be an FindBy56 * annotated (custom) webelement.57 * @return Value to decorate the field with.58 **/59 @Override60 public Object decorate(final ClassLoader loader, final Field field) {61 if (STAFElementFieldDecorator.logger.isTraceEnabled()) {62 STAFElementFieldDecorator.logger.trace(String.format("decorate filed: '%s'.", field));63 }64 // If it is a custom annotated webelement, then ensure proper initialisation via65 // the adding of the callback method66 if (STAFElement.class.isAssignableFrom(field.getType()) && field.isAnnotationPresent(FindBy.class)) {67 return getEnhancedObject(field.getType(), getElementHandler(field), field.getAnnotation(FindBy.class));68 }69 // If it is a normal webelement, then use the default FieldDecorator70 // implementation71 return defaultFieldDecorator.decorate(loader, field);72 }73 /**74 * Creates the class with the callback method. The callback method will be75 * called when a method is called on the given field object (e.g. a click()76 * method call on a button).77 *78 * @return The class which contains the callback method.79 **/80 private STAFElementLocator getElementHandler(final Field field) {81 return new STAFElementLocator(getLocator(field));82 }83 /**84 * Returns the element handler for the field which melds the field and the85 * locator (aka FindBy) together for further usage. An ElementLocator locator...

Full Screen

Full Screen

Source:fieldDecorator.java Github

copy

Full Screen

...43 }444546 //@Override47 public Object decorate( ClassLoader loader, Field field ) {48 if ( UIElement.class.isAssignableFrom( field.getType() ) && field.isAnnotationPresent( FindBy.class )) {49 return getEnhancedObject( field.getType(), getElementHandler( field ) );50 }else{51 return defaultFieldDecorator.decorate( loader, field );52 }53 }5455 private Locator.ElementHandler getElementHandler( Field field ) {56 //return new locator.ElementHandler( field, getLocator( field ), webDriver, errCollector );57 return new Locator.ElementHandler( field, getLocator( field ), webDriver, testReport );58 }5960 private ElementLocator getLocator( Field field ) {61 return new DefaultElementLocatorFactory( searchContext ).createLocator( field );62 }63}6465 ...

Full Screen

Full Screen

Source:ExtendedFieldDecorator.java Github

copy

Full Screen

...22 super(new DefaultElementLocatorFactory(searchContext));23 }2425 @Override26 public Object decorate(final ClassLoader loader, final Field field) {27 if (Container.class.isAssignableFrom(field.getType())) {28 return decorateContainer(loader, field);29 }30 if (Element.class.isAssignableFrom(field.getType())) {31 return decorateElement(loader, field);32 }33 return super.decorate(loader, field);34 }3536 private Object decorateElement(final ClassLoader loader, final Field field) {37 final WebElement wrappedElement = proxyForLocator(loader, createLocator(field));38 return elementFactory.create((Class<? extends Element>) field.getType(), wrappedElement);39 }4041 private ElementLocator createLocator(final Field field) {42 return factory.createLocator(field);43 }4445 private Object decorateContainer(final ClassLoader loader, final Field field) {46 final WebElement wrappedElement = proxyForLocator(loader, createLocator(field));47 final Container container = containerFactory.create((Class<? extends Container>) field.getType(), wrappedElement);4849 PageFactory.initElements(new ExtendedFieldDecorator(wrappedElement), container);50 return container;51 }52} ...

Full Screen

Full Screen

Source:Page.java Github

copy

Full Screen

...39 public ExtendedFieldDecorator(ElementLocatorFactory factory) {40 super(factory);41 }42 @Override43 public Object decorate(ClassLoader loader, Field field) {44 if (field.getType().equals(Select.class)) {45 ElementLocator locator = factory.createLocator(field);46 if (locator == null) {47 return null;48 }49 return new Select(proxyForLocator(loader, locator));50 }51 return super.decorate(loader, field);52 }53 }54}...

Full Screen

Full Screen

Source:PageElementLocatorDecorator.java Github

copy

Full Screen

...15 this.searchContext = searchContext;16 this.defaultFieldDecorator = new DefaultFieldDecorator(new DefaultElementLocatorFactory(searchContext));17 }18 @Override19 public Object decorate(ClassLoader loader, Field field) {20 if (PageElement.class.isAssignableFrom(field.getType()) && field.isAnnotationPresent(FindBy.class)) {21 return getEnhancedObject(field.getType(), getElementHandler(field));22 } else {23 return defaultFieldDecorator.decorate(loader, field);24 }25 }26 public Object getEnhancedObject(Class clazz, MethodInterceptor methodInterceptor) {27 Enhancer e = new Enhancer();28 e.setSuperclass(clazz);29 e.setCallback(methodInterceptor);30 return e.create();31 }32 private PageElementLocatorHandler getElementHandler(Field field) {33 return new PageElementLocatorHandler(getLocator(field));34 }35 private ElementLocator getLocator(Field field) {36 return new DefaultElementLocatorFactory(searchContext).createLocator(field);37 }...

Full Screen

Full Screen

Source:ElementDecorator.java Github

copy

Full Screen

...12 public ElementDecorator(ElementLocatorFactory factory) {13 super(factory);14 }15 @Override16 public Object decorate(ClassLoader loader, Field field) {17 if(WebElement.class.isAssignableFrom(field.getType())){18 return super.decorate(loader,field);19 }20 else {21 if(CheckBoxImpl.class.isAssignableFrom(field.getType())){22 ElementLocator elementLocator = factory.createLocator(field);23 CheckBoxImpl checkBox = new CheckBoxImpl(proxyForLocator(loader,elementLocator));24 return checkBox;25 }26 }27 return super.decorate(loader, field);28 }29}...

Full Screen

Full Screen

Source:DecoratedField.java Github

copy

Full Screen

...9 public DecoratedField(ElementLocatorFactory factory) {10 super(factory);11 }12 @Override13 public Object decorate(ClassLoader loader, Field field) {14 if (WebElement.class.isAssignableFrom(field.getType())) {15 return super.decorate(loader, field);16 }17 else {18 if (Button.class.isAssignableFrom(field.getType())) {19 ElementLocator locator = factory.createLocator(field);20 Button button = new Button(proxyForLocator(loader, locator));21 return button;22 }23 }24 return null;25 }26 }...

Full Screen

Full Screen

decorate

Using AI Code Generation

copy

Full Screen

1import java.lang.reflect.Field;2import java.util.List;3import org.openqa.selenium.By;4import org.openqa.selenium.SearchContext;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.support.FindBy;8import org.openqa.selenium.support.PageFactory;9import org.openqa.selenium.support.pagefactory.AbstractAnnotations;10import org.openqa.selenium.support.pagefactory.Annotations;11import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;12import org.openqa.selenium.support.pagefactory.ElementLocator;13import org.openqa.selenium.support.pagefactory.FieldDecorator;14public class DecoratorExample {15 public static class MyElementLocator implements ElementLocator {16 private final SearchContext searchContext;17 private final By by;18 public MyElementLocator(SearchContext searchContext, By by) {19 this.searchContext = searchContext;20 this.by = by;21 }22 public WebElement findElement() {23 System.out.println("Inside findElement() method");24 return searchContext.findElement(by);25 }26 public List<WebElement> findElements() {27 System.out.println("Inside findElements() method");28 return searchContext.findElements(by);29 }30 }31 public static class MyAnnotations extends AbstractAnnotations {32 public MyAnnotations(Field field) {33 super(field);34 }35 public By buildBy() {36 return By.id(getFieldValue().toString());37 }38 public boolean isLookupCached() {39 return false;40 }41 }42 public static class MyFieldDecorator implements FieldDecorator {43 private final SearchContext searchContext;44 public MyFieldDecorator(SearchContext searchContext) {45 this.searchContext = searchContext;46 }47 public Object decorate(ClassLoader loader, Field field) {48 if (!(WebElement.class.isAssignableFrom(field.getType()) || List.class.isAssignableFrom(field.getType()))) {49 return null;50 }51 ElementLocator locator = createLocator(field);52 if (locator == null) {53 return null;54 }55 if (WebElement.class.equals(field.getType())) {56 return proxyForLocator(loader, locator);57 } else if (List.class.isAssignableFrom(field.getType())) {58 return proxyForListLocator(loader, locator);59 } else {60 return null;61 }62 }63 protected ElementLocator createLocator(Field field) {64 return new MyElementLocator(searchContext, new MyAnnotations(field).buildBy());65 }66 protected WebElement proxyForLocator(ClassLoader loader, ElementLocator locator) {67 return null;68 }

Full Screen

Full Screen

decorate

Using AI Code Generation

copy

Full Screen

1package com.selenium.test;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.FindBy;5import org.openqa.selenium.support.PageFactory;6import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;7import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;8public class PageObject {9 @FindBy(id = "id")10 private WebElement element;11 public PageObject(WebDriver driver) {12 ElementLocatorFactory finder = new DefaultFieldDecorator(new ElementLocatorFactory() {13 public org.openqa.selenium.support.pagefactory.ElementLocator createLocator(Field field) {14 return new ElementLocator() {15 public WebElement findElement() {16 return driver.findElement(By.id("id"));17 }18 public List<WebElement> findElements() {19 return driver.findElements(By.id("id"));20 }21 };22 }23 });24 PageFactory.initElements(finder, this);25 }26 public WebElement getElement() {27 return element;28 }29}30package com.selenium.test;31import org.openqa.selenium.WebDriver;32import org.openqa.selenium.WebElement;33import org.openqa.selenium.support.FindBy;34import org.openqa.selenium.support.PageFactory;35import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;36import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;37public class PageObject {38 @FindBy(id = "id")39 private WebElement element;40 public PageObject(WebDriver driver) {41 ElementLocatorFactory finder = new DefaultFieldDecorator(new ElementLocatorFactory() {42 public org.openqa.selenium.support.pagefactory.ElementLocator createLocator(Field field) {43 return new ElementLocator() {44 public WebElement findElement() {45 return driver.findElement(By.id("id"));46 }47 public List<WebElement> findElements() {48 return driver.findElements(By.id("id"));49 }50 };51 }52 });53 PageFactory.initElements(finder, this);54 }55 public WebElement getElement() {56 return element;57 }58}59package com.selenium.test;60import org.openqa.selenium.WebDriver;61import org.openqa.selenium.WebElement;62import org.openqa.selenium.support.FindBy;63import org.openqa.selenium.support.PageFactory;64import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;65import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;66public class PageObject {67 @FindBy(id = "id")68 private WebElement element;69 public PageObject(WebDriver driver) {70 ElementLocatorFactory finder = new DefaultFieldDecorator(new ElementLocatorFactory() {

Full Screen

Full Screen

decorate

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;2import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;3public class CustomFieldDecorator extends DefaultFieldDecorator {4public CustomFieldDecorator(ElementLocatorFactory factory) {5super(factory);6}7protected boolean isDecoratableList(Class<?> fieldType) {8return super.isDecoratableList(fieldType);9}10protected List<WebElement> proxyForListLocator(LoadableComponent<?> loadableComponent, ElementLocator locator, Class<?> fieldType) {11return super.proxyForListLocator(loadableComponent, locator, fieldType);12}13protected boolean isDecoratableElement(Class<?> fieldType) {14return super.isDecoratableElement(fieldType);15}16protected WebElement proxyForLocator(LoadableComponent<?> loadableComponent, ElementLocator locator) {17return super.proxyForLocator(loadableComponent, locator);18}19}20import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;21import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;22import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;23public class PageFactory {24public static void initElements(WebDriver driver, Object page, LoadableComponent<?> loadableComponent) {25ElementLocatorFactory finder = new DefaultElementLocatorFactory(driver);26initElements(new CustomFieldDecorator(finder), page, loadableComponent);27}28public static void initElements(SearchContext searchContext, Object page, LoadableComponent<?> loadableComponent) {29initElements(new CustomFieldDecorator(new DefaultElementLocatorFactory(searchContext)), page, loadableComponent);30}31public static void initElements(DefaultFieldDecorator decorator, Object page, LoadableComponent<?> loadableComponent) {32Class<?> proxyIn = page.getClass();33while (proxyIn != Object.class) {34proxyFields(decorator, page, proxyIn, loadableComponent);35proxyIn = proxyIn.getSuperclass();36}37}38private static void proxyFields(DefaultFieldDecorator decorator, Object page, Class<?> proxyIn, LoadableComponent<?> loadableComponent) {39Field[] fields = proxyIn.getDeclaredFields();40for (Field field : fields) {41Object value = decorator.decorate(loadableComponent, field);42if (value != null) {43try {44field.setAccessible(true);45field.set(page, value);46} catch (IllegalAccessException e) {47throw new RuntimeException(e);48}49}50}51}52}

Full Screen

Full Screen

decorate

Using AI Code Generation

copy

Full Screen

1public class Decorator {2 public static void main(String[] args) {3 WebDriver driver = new FirefoxDriver();4 PageFactory.initElements(new DefaultFieldDecorator(new DefaultElementLocatorFactory(driver)), new Decorator());5 }6 @FindBy(name = "q")7 private WebElement searchBox;8 public void searchFor(String text) {9 searchBox.sendKeys(text);10 searchBox.submit();11 }12}

Full Screen

Full Screen

decorate

Using AI Code Generation

copy

Full Screen

1public class DecorateWebElement {2 public static void main(String[] args) {3 WebDriver driver = new FirefoxDriver();4 WebElement element = driver.findElement(By.name("q"));5 WebElement proxyElement = new DefaultFieldDecorator(new DefaultElementLocatorFactory(driver)).decorate(driver.getClass().getClassLoader(), element);6 proxyElement.clear();7 proxyElement.sendKeys("Selenium");8 proxyElement.submit();9 }10}

Full Screen

Full Screen

decorate

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.WebElement;2import org.openqa.selenium.support.FindBy;3import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;4import org.openqa.selenium.support.pagefactory.DefaultLocatingElementHandler;5import org.openqa.selenium.support.pagefactory.ElementLocator;6import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;7import java.lang.reflect.Field;8import java.lang.reflect.InvocationHandler;9import java.lang.reflect.Proxy;10public class CustomFieldDecorator extends DefaultFieldDecorator {11 public CustomFieldDecorator(ElementLocatorFactory factory) {12 super(factory);13 }14 public Object decorate(ClassLoader loader, Field field) {15 if (WebElement.class.isAssignableFrom(field.getType())) {16 return proxyForLocator(loader, factory.createLocator(field));17 } else {18 return super.decorate(loader, field);19 }20 }21 private Object proxyForLocator(ClassLoader loader, ElementLocator locator) {22 InvocationHandler handler = new CustomElementHandler(locator);23 return Proxy.newProxyInstance(loader, new Class[]{WebElement.class, CustomElement.class}, handler);24 }25 private class CustomElementHandler extends DefaultLocatingElementHandler {26 public CustomElementHandler(ElementLocator locator) {27 super(locator);28 }29 protected WebElement proxyForLocator(ClassLoader loader, ElementLocator locator) {30 return (WebElement) super.proxyForLocator(loader, locator);31 }32 public Object invoke(Object object, java.lang.reflect.Method method, Object[] objects) throws Throwable {33 WebElement element = proxyForLocator(loader, locator);34 if (method.getName().equals("click")) {35 element.click();36 return null;37 }38 if (method.getName().equals("getCustomText")) {39 return element.getText();40 }41 return method.invoke(element, objects);42 }43 }44}45interface CustomElement {46 void click();47 String getCustomText();48}49public class MyPageObject {50 @FindBy(id = "fieldId")51 private CustomElement customElement;52 public void clickCustomElement() {53 customElement.click();54 }55 public String getCustomText() {56 return customElement.getCustomText();57 }58}59import org.junit.Test;60import org.junit.runner.RunWith;61import org.openqa.selenium.WebDriver;62import org.openqa.selenium

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in DefaultFieldDecorator

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful