Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator.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
1package com.qaprosoft.carina.core.foundation.webdriver.decorator;2import java.lang.reflect.Field;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.pagefactory.Annotations;8import org.openqa.selenium.support.pagefactory.ElementLocator;9import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;10import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator;11public class ExtendedElementLocatorFactory implements ElementLocatorFactory {12 private final SearchContext searchContext;13 public ExtendedElementLocatorFactory(SearchContext searchContext) {14 this.searchContext = searchContext;15 }16 public ElementLocator createLocator(Field field) {17 return new ExtendedElementLocator(searchContext, new Annotations(field));18 }19}20package com.qaprosoft.carina.core.foundation.webdriver.decorator;21import java.lang.reflect.Field;22import org.openqa.selenium.By;23import org.openqa.selenium.WebDriver;24import org.openqa.selenium.WebElement;25import org.openqa.selenium.support.PageFactory;26import org.openqa.selenium.support.pagefactory.Annotations;27import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;28import org.openqa.selenium.support.pagefactory.ElementLocator;29import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;30import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator;31import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocatorFactory;32public class ExtendedFieldDecorator extends DefaultFieldDecorator {33 private final WebDriver driver;34 public ExtendedFieldDecorator(ElementLocatorFactory factory, WebDriver driver) {35 super(factory, driver);36 this.driver = driver;37 }38 public Object decorate(ClassLoader loader, Field field) {39 ElementLocator locator = factory.createLocator(field);40 if (locator == null) {41 return null;42 }43 if (WebElement.class.isAssignableFrom(field.getType())) {44 return proxyForLocator(loader, locator);45 } else if (isDecoratableList(field)) {46 return proxyForListLocator(loader, locator);47 } else {48 return null;49 }50 }51 protected WebElement proxyForLocator(ClassLoader loader, ElementLocator locator) {52 return (WebElement) new WebElementProxy(new ExtendedElementHandler(locator)).newInstance(loader);53 }
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.SearchContext;5import org.openqa.selenium.support.pagefactory.ElementLocator;6public class ExtendedElementLocator implements ElementLocator {7 protected static final Logger LOGGER = Logger.getLogger(ExtendedElementLocator.class);8 private final SearchContext searchContext;9 private final boolean shouldCache;10 private ExtendedElementLocatorFactory factory;11 private Field field;12 public ExtendedElementLocator(SearchContext searchContext, Field field, boolean shouldCache,13 ExtendedElementLocatorFactory factory) {14 this.searchContext = searchContext;15 this.shouldCache = shouldCache;16 this.factory = factory;17 this.field = field;18 }19 public org.openqa.selenium.WebElement findElement() {20 return factory.createLocator(searchContext, field).findElement();21 }22 public java.util.List<org.openqa.selenium.WebElement> findElements() {23 return factory.createLocator(searchContext, field).findElements();24 }25}26package com.qaprosoft.carina.core.foundation.webdriver.locator;27import java.lang.reflect.Field;28import org.openqa.selenium.SearchContext;29import org.openqa.selenium.support.pagefactory.Annotations;30import org.openqa.selenium.support.pagefactory.ElementLocator;31import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;32public class ExtendedElementLocatorFactory implements ElementLocatorFactory {33 private final SearchContext searchContext;34 public ExtendedElementLocatorFactory(SearchContext searchContext) {35 this.searchContext = searchContext;36 }37 public ElementLocator createLocator(Field field) {38 return new ExtendedElementLocator(searchContext, field, true, this);39 }40 public ElementLocator createLocator(SearchContext searchContext, Field field) {41 return new ExtendedElementLocator(searchContext, field, true, this);42 }43}44package com.qaprosoft.carina.core.foundation.webdriver.locator;45import java.lang.reflect.Field;46import org.openqa.selenium.SearchContext;47import org.openqa.selenium.support.pagefactory.Annotations;48import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;49import org.openqa.selenium.support.pagefactory.Element
ExtendedElementLocator
Using AI Code Generation
1public void click() {2 if (isElementPresent()) {3 if (isElementClickable()) {4 getWrappedElement().click();5 } else {6 throw new ExtendedElementException("Unable to click on element: " + toString());7 }8 } else {9 throw new ExtendedElementException("Unable to click on element: " + toString());10 }11}12public void click() {13 if (isElementPresent()) {14 if (isElementClickable()) {15 getWrappedElement().click();16 } else {17 throw new ExtendedElementException("Unable to click on element: " + toString());18 }19 } else {20 throw new ExtendedElementException("Unable to click on element: " + toString());21 }22}23public void click() {24 if (isElementPresent()) {25 if (isElementClickable()) {26 getWrappedElement().click();27 } else {28 throw new ExtendedElementException("Unable to click on element: " + toString());29 }30 } else {31 throw new ExtendedElementException("Unable to click on element: " + toString());32 }33}34public void click() {35 if (isElementPresent()) {36 if (isElementClickable()) {37 getWrappedElement().click();38 } else {39 throw new ExtendedElementException("Unable to click on element: " + toString());40 }41 } else {42 throw new ExtendedElementException("Unable to click on element: " + toString());43 }44}45public void click() {46 if (isElementPresent()) {47 if (isElementClickable()) {48 getWrappedElement().click();49 } else {50 throw new ExtendedElementException("Unable to click on element: " + toString());51 }52 } else {53 throw new ExtendedElementException("Unable to click on element: " + toString());54 }55}
ExtendedElementLocator
Using AI Code Generation
1package com.qaprosoft.carina.demo.gui;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.ElementLocatorFactory;7import org.openqa.selenium.support.pagefactory.FieldDecorator;8import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;9import org.openqa.selenium.support.ui.ExpectedConditions;10import org.openqa.selenium.support.ui.WebDriverWait;11import org.testng.Assert;12import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedElementLocatorFactory;13import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedFieldDecorator;14import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;15import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator;16import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocatorFactory;17import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedFieldDecorator;18import com.qaprosoft.carina.core.gui.AbstractUIObject;19public class HomePage extends AbstractUIObject {20 private ExtendedWebElement aboutUsLink;21 private ExtendedWebElement contactUsLink;22 private ExtendedWebElement newsLink;23 private ExtendedWebElement productsLink;24 private ExtendedWebElement careersLink;25 private ExtendedWebElement supportLink;26 private ExtendedWebElement loginLink;27 private ExtendedWebElement registerLink;28 private ExtendedWebElement logoutLink;29 private ExtendedWebElement cartLink;30 private ExtendedWebElement wishlistLink;31 private ExtendedWebElement compareLink;
ExtendedElementLocator
Using AI Code Generation
1public class 1 {2 private static final Logger LOGGER = Logger.getLogger(1.class);3 private static WebDriver driver;4 public void setup() {5 driver = new ChromeDriver();6 driver.manage().window().maximize();7 }8 public void test() {9 driver.get(URL);10 searchField.sendKeys("Carina");11 searchField.sendKeys(Keys.ENTER);12 link.click();13 button.click();14 }15 public void tearDown() {16 driver.quit();17 }18}19public class 2 {20 private static final Logger LOGGER = Logger.getLogger(2.class);21 private static WebDriver driver;22 public void setup() {23 driver = new ChromeDriver();24 driver.manage().window().maximize();25 }26 public void test() {27 driver.get(URL);28 searchField.sendKeys("Carina");29 searchField.sendKeys(Keys.ENTER);30 link.click();31 button.click();32 }33 public void tearDown() {34 driver.quit();35 }36}37public class 3 {38 private static final Logger LOGGER = Logger.getLogger(
ExtendedElementLocator
Using AI Code Generation
1package com.qaprosoft.carina.demo.gui.components;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.ElementLocatorFactory;8import org.openqa.selenium.support.pagefactory.FieldDecorator;9import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedElementLocatorFactory;10import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedFieldDecorator;11import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedElementLocator;12import com.qaprosoft.carina.core.gui.AbstractUIObject;13public class ExtendedElementLocatorPage extends AbstractUIObject {14 public ExtendedElementLocatorPage(WebDriver driver) {15 super(driver);16 PageFactory.initElements(new ExtendedFieldDecorator(d
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!!