Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator
Source:ExtendedFieldDecorator.java
...32import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;33import org.openqa.selenium.support.pagefactory.FieldDecorator;34import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;35import com.qaprosoft.carina.core.foundation.webdriver.ai.FindByAI;36import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator;37import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocatorFactory;38import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;39import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.AbstractUIObjectListHandler;40import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingElementListHandler;41import com.qaprosoft.carina.core.gui.AbstractUIObject;42public class ExtendedFieldDecorator implements FieldDecorator {43 private Logger LOGGER = Logger.getLogger(ExtendedFieldDecorator.class);44 protected ElementLocatorFactory factory;45 private WebDriver webDriver;46 47 public ExtendedFieldDecorator(ElementLocatorFactory factory, WebDriver webDriver) {48 this.factory = factory;49 this.webDriver = webDriver;50 }51 public Object decorate(ClassLoader loader, Field field) {52 if ((!field.isAnnotationPresent(FindBy.class) && !field.isAnnotationPresent(FindByAI.class)) /*53 * Enable field decorator logic only in case of54 * presence the FindBy/FindByAI annotation in the55 * field56 */ ||57 !(ExtendedWebElement.class.isAssignableFrom(field.getType()) || AbstractUIObject.class.isAssignableFrom(field.getType())58 || isDecoratableList(field)) /*59 * also verify that it is ExtendedWebElement or derived from AbstractUIObject or DecoratableList60 */) {61 // returning null is ok in this method.62 return null;63 }64 ElementLocator locator;65 try {66 locator = factory.createLocator(field);67 } catch (Exception e) {68 LOGGER.error(e.getMessage(), e);69 return null;70 }71 if (locator == null) {72 return null;73 }74 if (((ExtendedElementLocatorFactory) factory).isRootElementUsed()) {75 LOGGER.debug("Setting setShouldCache=false for locator: " + getLocatorBy(locator).toString());76 ((ExtendedElementLocator) locator).setShouldCache(false);77 }78 if (ExtendedWebElement.class.isAssignableFrom(field.getType())) {79 return proxyForLocator(loader, field, locator);80 }81 if (AbstractUIObject.class.isAssignableFrom(field.getType())) {82 return proxyForAbstractUIObject(loader, field, locator);83 } else if (List.class.isAssignableFrom(field.getType())) {84 Type listType = getListType(field);85 if (ExtendedWebElement.class.isAssignableFrom((Class<?>) listType)) {86 return proxyForListLocator(loader, field, locator);87 } else if (AbstractUIObject.class.isAssignableFrom((Class<?>) listType)) {88 return proxyForListUIObjects(loader, field, locator);89 } else {90 return null;91 }92 } else {93 return null;94 }95 }96 private boolean isDecoratableList(Field field) {97 if (!List.class.isAssignableFrom(field.getType())) {98 return false;99 }100 Type listType = getListType(field);101 if (listType == null) {102 return false;103 }104 try {105 if (!(ExtendedWebElement.class.equals(listType) || AbstractUIObject.class.isAssignableFrom((Class<?>) listType))) {106 return false;107 }108 } catch (ClassCastException e) {109 return false;110 }111 return true;112 }113 protected ExtendedWebElement proxyForLocator(ClassLoader loader, Field field, ElementLocator locator) {114 InvocationHandler handler = new LocatingElementHandler(locator);115 WebElement proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[] { WebElement.class, WrapsElement.class, Locatable.class },116 handler);117 return new ExtendedWebElement(proxy, field.getName(),118 field.isAnnotationPresent(FindBy.class) ? new LocalizedAnnotations(field).buildBy() : null);119 }120 @SuppressWarnings("unchecked")121 protected <T extends AbstractUIObject> T proxyForAbstractUIObject(ClassLoader loader, Field field,122 ElementLocator locator) {123 LOGGER.debug("Setting setShouldCache=false for locator: " + getLocatorBy(locator).toString());124 ((ExtendedElementLocator) locator).setShouldCache(false);125 InvocationHandler handler = new LocatingElementHandler(locator);126 WebElement proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[] { WebElement.class, WrapsElement.class, Locatable.class },127 handler);128 Class<? extends AbstractUIObject> clazz = (Class<? extends AbstractUIObject>) field.getType();129 T uiObject;130 try {131 uiObject = (T) clazz.getConstructor(WebDriver.class, SearchContext.class).newInstance(132 webDriver, proxy);133 } catch (NoSuchMethodException e) {134 LOGGER.error("Implement appropriate AbstractUIObject constructor for auto-initialization: "135 + e.getMessage());136 throw new RuntimeException(137 "Implement appropriate AbstractUIObject constructor for auto-initialization: "138 + e.getMessage(),139 e);140 } catch (Exception e) {141 LOGGER.error("Error creating UIObject: " + e.getMessage());142 throw new RuntimeException("Error creating UIObject: " + e.getMessage(), e);143 }144 uiObject.setName(field.getName());145 uiObject.setRootElement(proxy);146 uiObject.setRootBy(getLocatorBy(locator));147 return uiObject;148 }149 @SuppressWarnings("unchecked")150 protected List<ExtendedWebElement> proxyForListLocator(ClassLoader loader, Field field, ElementLocator locator) {151 InvocationHandler handler = new LocatingElementListHandler(webDriver, locator, field.getName(), new LocalizedAnnotations(field).buildBy());152 List<ExtendedWebElement> proxies = (List<ExtendedWebElement>) Proxy.newProxyInstance(loader, new Class[] { List.class }, handler);153 return proxies;154 }155 @SuppressWarnings("unchecked")156 protected <T extends AbstractUIObject> List<T> proxyForListUIObjects(ClassLoader loader, Field field,157 ElementLocator locator) {158 LOGGER.debug("Setting setShouldCache=false for locator: " + getLocatorBy(locator).toString());159 ((ExtendedElementLocator) locator).setShouldCache(false);160 InvocationHandler handler = new AbstractUIObjectListHandler<T>((Class<?>) getListType(field), webDriver,161 locator, field.getName());162 List<T> proxies = (List<T>) Proxy.newProxyInstance(loader, new Class[] { List.class }, handler);163 return proxies;164 }165 private Type getListType(Field field) {166 // Type erasure in Java isn't complete. Attempt to discover the generic167 // type of the list.168 Type genericType = field.getGenericType();169 if (!(genericType instanceof ParameterizedType)) {170 return null;171 }172 return ((ParameterizedType) genericType).getActualTypeArguments()[0];173 }...
Source:ExtendedElementLocator.java
...44 * {@link org.openqa.selenium.support.PageFactory} and understands the45 * annotations {@link org.openqa.selenium.support.FindBy} and46 * {@link org.openqa.selenium.support.CacheLookup}.47 */48public class ExtendedElementLocator implements ElementLocator {49 private static final Logger LOGGER = LoggerFactory.getLogger(ExtendedElementLocator.class);50 private final SearchContext searchContext;51 private boolean shouldCache;52 private boolean caseInsensitive;53 private By by;54 private WebElement cachedElement;55 private String aiCaption;56 private Label aiLabel;57 58 /**59 * Creates a new element locator.60 * 61 * @param searchContext The context to use when finding the element62 * @param field The field on the Page Object that will hold the located63 * value64 */65 public ExtendedElementLocator(SearchContext searchContext, Field field) {66 this.searchContext = searchContext;67 if (field.isAnnotationPresent(FindBy.class)) {68 LocalizedAnnotations annotations = new LocalizedAnnotations(field);69 this.shouldCache = true;70 this.caseInsensitive = false;71 this.by = annotations.buildBy();72 if (field.isAnnotationPresent(DisableCacheLookup.class)) {73 this.shouldCache = false;74 }75 if (field.isAnnotationPresent(CaseInsensitiveXPath.class)) {76 this.caseInsensitive = true;77 }78 }79 // Elements to be recognized by Alice...
ExtendedElementLocator
Using AI Code Generation
1import java.util.List;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.FindBy;5import org.openqa.selenium.support.PageFactory;6import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedElementLocatorFactory;7import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;8import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator;9public class PageObject {10 private List<ExtendedWebElement> table1Column;11 private List<WebElement> table1Column1;12 public PageObject(WebDriver driver) {13 PageFactory.initElements(new ExtendedElementLocatorFactory(driver), this);14 }15 public List<WebElement> getTable1Column1() {16 return table1Column1;17 }18 public List<ExtendedWebElement> getTable1Column() {19 return table1Column;20 }21 public static void main(String[] args) {22 WebDriver driver = null;23 PageObject page = new PageObject(driver);24 List<WebElement> table1Column1 = page.getTable1Column1();25 List<ExtendedWebElement> table1Column = page.getTable1Column();26 }27}28import java.util.List;29import org.openqa.selenium.WebDriver;30import org.openqa.selenium.WebElement;31import org.openqa.selenium.support.FindBy;32import org.openqa.selenium.support.PageFactory;33import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedElementLocatorFactory;34import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;35import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator;36public class PageObject {37 private List<ExtendedWebElement> table1Column;38 private List<WebElement> table1Column1;39 public PageObject(WebDriver driver) {40 PageFactory.initElements(new ExtendedElementLocatorFactory(driver), this);41 }42 public List<WebElement> getTable1Column1() {43 return table1Column1;44 }
ExtendedElementLocator
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.FindBy;5import org.openqa.selenium.support.FindBys;6import org.openqa.selenium.support.PageFactory;7import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;8import org.openqa.selenium.support.pagefactory.ElementLocator;9import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;10import org.openqa.selenium.support.pagefactory.FieldDecorator;11import org.testng.annotations.Test;12import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedElementLocator;13import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedFieldDecorator;14import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedFieldDecorator;15import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedElementLocator;16import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedFieldDecorator;17public class TestClass {18 private WebElement testDiv;19 @FindBys({20 })21 private WebElement testDiv2;22 public void test1() {23 WebDriver driver = null;24 PageFactory.initElements(new ExtendedFieldDecorator(new DefaultElementLocatorFactory(driver)), this);25 }26 public void test2() {27 WebDriver driver = null;28 PageFactory.initElements(new ExtendedFieldDecorator(new DefaultElementLocatorFactory(driver)), this);29 }30 public void test3() {31 WebDriver driver = null;32 PageFactory.initElements(new ExtendedFieldDecorator(new DefaultElementLocatorFactory(driver)), this);33 }34 public void test4() {35 WebDriver driver = null;36 PageFactory.initElements(new ExtendedFieldDecorator(new DefaultElementLocatorFactory(driver)), this);37 }38 public void test5() {39 WebDriver driver = null;40 PageFactory.initElements(new ExtendedFieldDecorator(new DefaultElementLocatorFactory(driver)), this);41 }42 public void test6() {43 WebDriver driver = null;44 PageFactory.initElements(new ExtendedFieldDecorator(new DefaultElementLocatorFactory(driver)), this);45 }46 public void test7() {47 WebDriver driver = null;48 PageFactory.initElements(new ExtendedFieldDecorator(new DefaultElementLocatorFactory(driver)), this);
ExtendedElementLocator
Using AI Code Generation
1package com.qaprosoft.carina.core.foundation.webdriver.locator;2import java.lang.reflect.Field;3import org.apache.log4j.Logger;4import org.openqa.selenium.support.pagefactory.Annotations;5import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedFieldDecorator;6public class ExtendedElementLocatorFactory implements org.openqa.selenium.support.pagefactory.ElementLocatorFactory {7 private static final Logger LOGGER = Logger.getLogger(ExtendedElementLocatorFactory.class);8 private final ExtendedFieldDecorator decorator;9 public ExtendedElementLocatorFactory(ExtendedFieldDecorator decorator) {10 this.decorator = decorator;11 }12 public org.openqa.selenium.support.pagefactory.ElementLocator createLocator(Field field) {13 return new ExtendedElementLocator(decorator, field);14 }15}16package com.qaprosoft.carina.core.foundation.webdriver.decorator;17import java.lang.reflect.Field;18import java.util.ArrayList;19import java.util.List;20import org.apache.log4j.Logger;21import org.openqa.selenium.By;22import org.openqa.selenium.SearchContext;23import org.openqa.selenium.WebElement;24import org.openqa.selenium.support.pagefactory.Annotations;25import org.openqa.selenium.support.pagefactory.ElementLocator;26import com.qaprosoft.carina.core.foundation.commons.SpecialKeywords;27import com.qaprosoft.carina.core.foundation.utils.Configuration;28import com.qaprosoft.carina.core.foundation.utils.R;29import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocatorFactory;30public class ExtendedFieldDecorator extends AbstractElementLocatorFactory implements org.openqa.selenium.support.pagefactory.FieldDecorator {31 private static final Logger LOGGER = Logger.getLogger(ExtendedFieldDecorator.class);32 private final SearchContext searchContext;33 public ExtendedFieldDecorator(SearchContext searchContext) {34 super(new ExtendedElementLocatorFactory(searchContext));35 this.searchContext = searchContext;36 }37 public ExtendedFieldDecorator(SearchContext searchContext, long timeout) {38 super(new ExtendedElementLocatorFactory(searchContext), timeout);39 this.searchContext = searchContext;40 }41 public Object decorate(ClassLoader loader, Field field) {42 if (!WebElement.class.isAssignableFrom(field.getType())) {43 return null;44 }45 ElementLocator locator = factory.createLocator(field);46 if (locator == null) {47 return null;48 }49 String name = field.getName();50 String[] names = name.split("_");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!