Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.locator.internal.AbstractUIObjectListHandler.getLocatorBy
Source:ExtendedFieldDecorator.java
...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 }174 175 private By getLocatorBy(ElementLocator locator) {176 By rootBy = null;177 178 //TODO: get root by annotation from ElementLocator to be able to append by for those elements and reuse fluent waits179 try {180 Field byContextField = null;181 byContextField = locator.getClass().getDeclaredField("by");182 byContextField.setAccessible(true);183 rootBy = (By) byContextField.get(locator);184 } catch (NoSuchFieldException e) {185 e.printStackTrace();186 } catch (IllegalAccessException e) {187 e.printStackTrace();188 } catch (ClassCastException e) {189 e.printStackTrace();...
Source:AbstractUIObjectListHandler.java
...53 this.clazz = clazz;54 this.webDriver = webDriver;55 this.locator = locator;56 this.name = name;57 this.locatorBy = getLocatorBy(locator);58 }59 @SuppressWarnings("unchecked")60 public Object invoke(Object object, Method method, Object[] objects) throws Throwable {61 62 // Hotfix for huge and expected regression in carina: we lost managed63 // time delays with lists manipulations64 // Temporary we are going to restore explicit waiter here with hardcoded65 // timeout before we find better solution66 // Pros: super fast regression issue which block UI execution67 // Cons: there is no way to manage timeouts in this places68 waitUntil(ExpectedConditions.and(ExpectedConditions.presenceOfElementLocated(locatorBy),69 ExpectedConditions.visibilityOfElementLocated(locatorBy)));70 List<WebElement> elements = null;71 try {72 elements = locator.findElements();73 } catch (StaleElementReferenceException | InvalidElementStateException e) {74 LOGGER.debug("catched StaleElementReferenceException: ", e);75 elements = webDriver.findElements(locatorBy);76 }77 List<T> uIObjects = new ArrayList<T>();78 int index = 0;79 if (elements != null) {80 for (WebElement element : elements) {81 T uiObject;82 try {83 uiObject = (T) clazz.getConstructor(WebDriver.class, SearchContext.class)84 .newInstance(85 webDriver, element);86 } catch (NoSuchMethodException e) {87 LOGGER.error("Implement appropriate AbstractUIObject constructor for auto-initialization: "88 + e.getMessage());89 throw new RuntimeException(90 "Implement appropriate AbstractUIObject constructor for auto-initialization: "91 + e.getMessage(),92 e);93 }94 uiObject.setName(String.format("%s - %d", name, index++));95 uiObject.setRootElement(element);96 uiObject.setRootBy(locatorBy);97 uIObjects.add(uiObject);98 }99 }100 try {101 return method.invoke(uIObjects, objects);102 } catch (InvocationTargetException e) {103 throw e.getCause();104 }105 }106 107 private By getLocatorBy(ElementLocator locator) {108 By rootBy = null;109 110 //TODO: get root by annotation from ElementLocator to be able to append by for those elements and reuse fluent waits111 try {112 Field byContextField = null;113 byContextField = locator.getClass().getDeclaredField("by");114 byContextField.setAccessible(true);115 rootBy = (By) byContextField.get(locator);116 } catch (NoSuchFieldException e) {117 e.printStackTrace();118 } catch (IllegalAccessException e) {119 e.printStackTrace();120 } catch (ClassCastException e) {121 e.printStackTrace();...
getLocatorBy
Using AI Code Generation
1package com.qaprosoft.carina.core.foundation.webdriver.locator.internal;2import java.util.List;3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.support.pagefactory.ElementLocator;7import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;8import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;9import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;10public class ExtendedElementListHandler extends LocatingElementListHandler {11 public ExtendedElementListHandler(ElementLocatorFactory factory) {12 super(factory);13 }14 public ExtendedElementListHandler(ElementLocator locator) {15 super(locator);16 }17 protected List<WebElement> proxyForListLocator(WebDriver driver, ElementLocator locator) {18 return new ExtendedWebElementList(driver, locator);19 }20 public static class ExtendedWebElementList extends AbstractUIObjectListHandler {21 public ExtendedWebElementList(WebDriver driver, ElementLocator locator) {22 super(driver, locator);23 }24 protected ExtendedWebElement proxyForLocator(WebDriver driver, ElementLocator locator) {25 return new ExtendedWebElement(driver, locator);26 }27 protected ExtendedWebElement proxyForLocator(WebDriver driver, By by) {28 return new ExtendedWebElement(driver, by);29 }30 }31}32package com.qaprosoft.carina.core.foundation.webdriver.locator.internal;33import java.util.List;34import org.openqa.selenium.By;35import org.openqa.selenium.WebDriver;36import org.openqa.selenium.WebElement;37import org.openqa.selenium.support.pagefactory.ElementLocator;38import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;39import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;40import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;41public class ExtendedElementListHandler extends LocatingElementListHandler {42 public ExtendedElementListHandler(ElementLocatorFactory factory) {43 super(factory);44 }45 public ExtendedElementListHandler(ElementLocator locator) {46 super(locator);47 }48 protected List<WebElement> proxyForListLocator(WebDriver driver, ElementLocator locator) {49 return new ExtendedWebElementList(driver, locator);50 }51 public static class ExtendedWebElementList extends AbstractUIObjectListHandler {52 public ExtendedWebElementList(WebDriver driver, ElementLocator
getLocatorBy
Using AI Code Generation
1package com.qaprosoft.carina.core.foundation.webdriver.locator;2import java.lang.reflect.Field;3import java.lang.reflect.Method;4import java.util.ArrayList;5import java.util.List;6import org.openqa.selenium.By;7import org.openqa.selenium.SearchContext;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.support.pagefactory.AbstractAnnotations;10import org.openqa.selenium.support.pagefactory.Annotations;11import org.openqa.selenium.support.pagefactory.ElementLocator;12import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;13import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;14import org.openqa.selenium.support.ui.Quotes;15import org.slf4j.Logger;16import org.slf4j.LoggerFactory;17public class UIObjectListHandler extends LocatingElementListHandler {18 private static final Logger LOGGER = LoggerFactory.getLogger(UIObjectListHandler.class);19 private final ElementLocatorFactory factory;20 private final SearchContext context;21 private final Field field;22 public UIObjectListHandler(ElementLocatorFactory factory, SearchContext context, Field field) {23 super(factory.createLocator(field));24 this.factory = factory;25 this.context = context;26 this.field = field;27 }28 protected List<WebElement> invoke(Object object, Method method, Object... objects) {29 try {30 ElementLocator locator = factory.createLocator(field);31 List<WebElement> elements = locator.findElements();32 List<WebElement> toReturn = new ArrayList<WebElement>();33 for (WebElement element : elements) {34 toReturn.add(new UIObjectHandler(field, element, context).getProxy());35 }36 return toReturn;37 } catch (Exception e) {38 LOGGER.error("Can't create proxy for List<WebElement>!", e);39 throw new RuntimeException(e);40 }41 }42 protected By getLocatorBy() {43 AbstractAnnotations annotations = new Annotations(field);44 String name = annotations.buildBy().toString();45 String description = annotations.buildBy().toString();46 return By.xpath(name);47 }48 protected String getLocatorDescription() {49 AbstractAnnotations annotations = new Annotations(field);50 String name = annotations.buildBy().toString();51 String description = annotations.buildBy().toString();52 return String.format("By.xpath: %s", Quotes.escape(name));53 }54}
getLocatorBy
Using AI Code Generation
1package com.qaprosoft.carina.core.foundation.webdriver.locator.internal;2import java.lang.reflect.Field;3import java.lang.reflect.Method;4import java.util.ArrayList;5import java.util.List;6import org.openqa.selenium.By;7import org.openqa.selenium.SearchContext;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.support.pagefactory.ByChained;10import org.openqa.selenium.support.pagefactory.ByAll;11import org.openqa.selenium.support.pagefactory.ByOr;12import org.openqa.selenium.support.pagefactory.ByChained.ByChainedBuilder;13import org.openqa.selenium.support.pagefactory.ByAll.ByAllBuilder;14import org.openqa.selenium.support.pagefactory.ByOr.ByOrBuilder;15import org.openqa.selenium.support.pagefactory.ElementLocator;16import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;17import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;18import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler.LocatingElementListInvocationHandler;19import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;20import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.LocatingElementInvocationHandler;21import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;22import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler.LocatingElementListInvocationHandler;23import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;24import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.LocatingElementInvocationHandler;25import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;26import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler.LocatingElementListInvocationHandler;27import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;28import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.LocatingElementInvocationHandler;29import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;30import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler.LocatingElementListInvocationHandler;31import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;32import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.LocatingElementInvocationHandler;33import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;34import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler.LocatingElementListInvocationHandler;35import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;36import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.Loc
getLocatorBy
Using AI Code Generation
1package com.qaprosoft.carina.demo.gui.pages.common;2import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;3import com.qaprosoft.carina.core.foundation.webdriver.locator.GetLocatorUtil;4import com.qaprosoft.carina.core.foundation.webdriver.locator.LocatorType;5import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.AbstractUIObjectListHandler;6import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.ILocator;7import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.Locator;8import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatorTypeGroup;9import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatorTypeGroup.LocatorGroup;10import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.UIObjectList;11import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.UIObjectListFactory;12import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.UIObjectListHandler;13import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.UIObjectListHandlerFactory;14import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.UIObjectListHandlerFactory.UIObjectListHandlerType;15import java.util.List;16import org.apache.log4j.Logger;17import org.openqa.selenium.SearchContext;18import org.openqa.selenium.WebDriver;19import org.openqa.selenium.support.FindBy;20import org.openqa.selenium.support.FindBys;21import org.openqa.selenium.support.PageFactory;22import org.openqa.selenium.support.pagefactory.AbstractAnnotations;23import org.openqa.selenium.support.pagefactory.Annotations;24import org.openqa.selenium.support.pagefactory.DefaultElementLocator;25import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;26public class FooterMenu extends AbstractUIObjectListHandler {27 private static final Logger LOGGER = Logger.getLogger(FooterMenu.class);28 public FooterMenu(WebDriver driver, SearchContext searchContext, ExtendedWebElement el, String name) {29 super(driver, searchContext, el, name);30 }31 public FooterMenu(WebDriver driver, SearchContext searchContext, ExtendedWebElement el, String name,32 UIObjectListHandlerType handlerType) {33 super(driver, searchContext, el, name, handlerType);34 }35 private ExtendedWebElement link;36 private ExtendedWebElement link1;37 private ExtendedWebElement link2;
getLocatorBy
Using AI Code Generation
1package com.qaprosoft.carina.demo.gui.components;2import java.lang.invoke.MethodHandles;3import java.util.List;4import org.apache.log4j.Logger;5import org.openqa.selenium.SearchContext;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.support.FindBy;9import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;10import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedFindBy;11import com.qaprosoft.carina.core.foundation.webdriver.locator.LocatorType;12import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.AbstractUIObjectListHandler;13import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.Locator;14public class NewsList extends AbstractUIObjectListHandler {15 private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass());
getLocatorBy
Using AI Code Generation
1public class 1 extends AbstractUIObjectListHandler {2 public 1(WebDriver driver, String uniqueAttr, String text) {3 super(driver, uniqueAttr, text);4 }5 protected Locator getLocatorBy(String uniqueAttr, String text) {6 }7}8public class 2 extends AbstractUIObjectListHandler {9 public 2(WebDriver driver, String uniqueAttr, String text) {10 super(driver, uniqueAttr, text);11 }12 protected Locator getLocatorBy(String uniqueAttr, String text) {13 }14}
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!!