How to use LocalizedAnnotations class of com.qaprosoft.carina.core.foundation.webdriver.locator package

Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations

Source:ExtendedFieldDecorator.java Github

copy

Full Screen

...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) {...

Full Screen

Full Screen

Source:LocalizedAnnotations.java Github

copy

Full Screen

...25import com.qaprosoft.carina.core.foundation.webdriver.decorator.annotations.AccessibilityId;26import com.qaprosoft.carina.core.foundation.webdriver.decorator.annotations.ClassChain;27import com.qaprosoft.carina.core.foundation.webdriver.decorator.annotations.Predicate;28import io.appium.java_client.MobileBy;29public class LocalizedAnnotations extends Annotations {30 private static Pattern L10N_PATTERN = Pattern.compile(SpecialKeywords.L10N_PATTERN);31 public LocalizedAnnotations(Field field) {32 super(field);33 }34 @Override35 public By buildBy() {36 By by = super.buildBy();37 String param = by.toString();38 39 // replace by using localization pattern40 Matcher matcher = L10N_PATTERN.matcher(param);41 while (matcher.find()) {42 int start = param.indexOf(SpecialKeywords.L10N + ":") + 5;43 int end = param.indexOf("}");44 String key = param.substring(start, end);45 if (!L10N.isUTF) {...

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1import java.lang.annotation.ElementType;2import java.lang.annotation.Retention;3import java.lang.annotation.RetentionPolicy;4import java.lang.annotation.Target;5import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;6@Retention(RetentionPolicy.RUNTIME)7@Target({ElementType.FIELD, ElementType.TYPE})8public @interface Localized {9 String value();10}11import java.lang.annotation.Annotation;12import java.lang.reflect.Field;13import java.util.Locale;14import org.apache.log4j.Logger;15import org.openqa.selenium.By;16import org.openqa.selenium.support.FindBy;17import org.openqa.selenium.support.pagefactory.Annotations;18import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;19public class LocalizedAnnotationsProcessor {20 private static final Logger LOGGER = Logger.getLogger(LocalizedAnnotationsProcessor.class);21 public static void process(Object page) {22 for (Field field : page.getClass().getDeclaredFields()) {23 if (field.isAnnotationPresent(LocalizedAnnotations.class)) {24 process(field, page);25 }26 }27 }28 public static void process(Field field, Object page) {29 try {30 field.setAccessible(true);31 Object object = field.get(page);32 if (object == null) {33 LOGGER.error("Object is null! Cannot process annotations.");34 return;35 }36 for (Field f : object.getClass().getDeclaredFields()) {37 f.setAccessible(true);38 for (Annotation a : f.getDeclaredAnnotations()) {39 if (a.annotationType().isAnnotationPresent(LocalizedAnnotations.class)) {40 Object value = f.get(object);41 if (value != null) {42 f.set(object, getLocalizedValue(value.toString()));43 }44 }45 }46 }47 } catch (Exception e) {48 LOGGER.error("Unable to process annotations for field: " + field.getName() + " on page: " + page.getClass().getSimpleName(), e);49 }50 }51 public static String getLocalizedValue(String value) {52 String localizedValue = value;53 try {54 String locale = Locale.getDefault().toString();55 String[] parts = value.split("\\.");56 if (parts.length > 1) {57 localizedValue = parts[0] + "_" + locale + "." + parts[1];58 }59 } catch (Exception e) {

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.core.foundation.webdriver.locator;2import java.io.File;3import java.io.IOException;4import java.lang.reflect.Field;5import java.util.HashMap;6import java.util.Map;7import org.apache.commons.io.FileUtils;8import org.apache.log4j.Logger;9import org.openqa.selenium.By;10import com.qaprosoft.carina.core.foundation.utils.Configuration;11public class LocalizedAnnotations {12 private static final Logger LOGGER = Logger.getLogger(LocalizedAnnotations.class);13 private static final String DEFAULT_LOCALE = "en";14 private static final String LOCALE = Configuration.get(Configuration.Parameter.LOCALE);15 private static final String LOCALE_DIR = "src/main/resources/locators";16 private static final String LOCATORS_FILE = "locators.properties";17 private static final Map<String, String> LOCATORS = new HashMap<String, String>();18 static {19 try {20 LOCATORS.putAll(FileUtils.readLines(new File(LOCALE_DIR, LOCATORS_FILE), "UTF-8").stream()21 .filter(line -> !line.startsWith("#"))22 .collect(HashMap::new, (m, v) -> m.put(v.substring(0, v.indexOf("=")), v.substring(v.indexOf("=") + 1)), HashMap::putAll));23 } catch (IOException e) {24 LOGGER.error("Unable to read locators file!", e);25 }26 }27 public static By getLocator(Field field) {28 String locator = LOCATORS.getOrDefault(field.getDeclaringClass().getSimpleName() + "." + field.getName(), LOCATORS.get(field.getName()));29 if (locator == null) {30 throw new IllegalArgumentException("Unable to find locator for field: " + field.getName());31 }32 String[] locatorParts = locator.split("=", 2);33 if (locatorParts.length != 2) {34 throw new IllegalArgumentException("Invalid locator format: " + locator);35 }36 String locatorType = locatorParts[0];37 String locatorValue = locatorParts[1];38 if (locatorValue.contains("{locale}")) {39 locatorValue = locatorValue.replace("{locale}", LOCALE);40 }41 switch (locatorType) {42 return By.id(locatorValue);43 return By.name(locatorValue);44 return By.className(locatorValue);45 return By.cssSelector(locatorValue);

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;2public class 1 {3public static void main(String[] args) {4LocalizedAnnotations localizedAnnotations = new LocalizedAnnotations();5localizedAnnotations.setLocale("de");6}7}8import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;9public class 2 {10public static void main(String[] args) {11LocalizedAnnotations localizedAnnotations = new LocalizedAnnotations();12localizedAnnotations.setLocale("de");13}14}15import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;16public class 3 {17public static void main(String[] args) {18LocalizedAnnotations localizedAnnotations = new LocalizedAnnotations();19localizedAnnotations.setLocale("de");20}21}22import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;23public class 4 {24public static void main(String[] args) {25LocalizedAnnotations localizedAnnotations = new LocalizedAnnotations();26localizedAnnotations.setLocale("de");27}28}29import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;30public class 5 {31public static void main(String[] args) {32LocalizedAnnotations localizedAnnotations = new LocalizedAnnotations();33localizedAnnotations.setLocale("de");34}35}36import com.qaprosoft.carina.core

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.demo.gui.pages;2import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;3import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations.LocatorType;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.support.FindBy;6import org.openqa.selenium.support.PageFactory;7import java.util.Locale;8public class HomePage extends BasePage {9 @FindBy(id = "searchInput")10 private ExtendedWebElement searchInput;11 private ExtendedWebElement searchBtn;12 private ExtendedWebElement loginBtn;13 private ExtendedWebElement logoutBtn;14 private ExtendedWebElement userBtn;15 private ExtendedWebElement anonUserBtn;16 private ExtendedWebElement createAccountBtn;17 private ExtendedWebElement myTalkBtn;18 private ExtendedWebElement preferencesBtn;19 private ExtendedWebElement watchlistBtn;20 private ExtendedWebElement myContributionsBtn;21 private ExtendedWebElement loginBtn;22 private ExtendedWebElement logoutBtn;23 private ExtendedWebElement userBtn;24 private ExtendedWebElement anonUserBtn;25 private ExtendedWebElement createAccountBtn;26 private ExtendedWebElement myTalkBtn;27 private ExtendedWebElement preferencesBtn;28 @FindBy(xpath =

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;2import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;3import org.openqa.selenium.support.FindBy;4public class LocalizedAnnotationsExample {5 private ExtendedWebElement testElement;6 public void testLocalizedAnnotations() {7 LocalizedAnnotations localizedAnnotations = new LocalizedAnnotations();8 localizedAnnotations.initAnnotations(this);9 System.out.println(testElement.getElementName());10 }11}12import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;13import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;14import org.openqa.selenium.support.FindBy;15public class LocalizedAnnotationsExample {16 private ExtendedWebElement testElement;17 public void testLocalizedAnnotations() {18 LocalizedAnnotations localizedAnnotations = new LocalizedAnnotations();19 localizedAnnotations.initAnnotations(this);20 System.out.println(testElement.getElementName());21 }22}23import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;24import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;25import org.openqa.selenium.support.FindBy;26public class LocalizedAnnotationsExample {27 private ExtendedWebElement testElement;28 public void testLocalizedAnnotations() {29 LocalizedAnnotations localizedAnnotations = new LocalizedAnnotations();30 localizedAnnotations.initAnnotations(this);31 System.out.println(testElement.getElementName());32 }33}34import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;35import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;36import org.openqa.selenium.support.FindBy;37public class LocalizedAnnotationsExample {

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1public class LocalizedAnnotationsExample {2 public WebElement localizedElement;3}4public class LocalizedAnnotationsExample {5 public WebElement localizedElement;6}7To use the LocalizedAnnotations class, you need to import the class from the following packages:8import the class from the com.qaprosoft.carina.core.foundation.webdriver.locator package and use it in the following way:9import the class from the org.openqa.selenium.support.pagefactory package and use it in the following way:10To use the LocalizedAnnotations class, you need to import the class from the following packages:11To use the LocalizedAnnotations class, you need to import the class from the following packages:12import the class from the com.qaprosoft.carina.core.foundation.webdriver.locator package and use it in the following way:13import the class from the org.openqa.selenium.support.pagefactory package and use it in the following way:14To use the LocalizedAnnotations class, you need to import the class from the following packages:15import the class from the com.qaprosoft.carina.core.foundation.webdriver.locator package and use it in the following way:16import the class from the org.openqa.selenium.support.pagefactory package and use it in the following way:17To use the LocalizedAnnotations class, you need to import the class from the following packages:

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1private ExtendedWebElement myElement;2private List<ExtendedWebElement> myElements;3private List<ExtendedWebElement> myElements;4private ExtendedWebElement myElement;5private List<ExtendedWebElement> myElements;6private List<ExtendedWebElement> myElements;7private ExtendedWebElement myElement;8private List<ExtendedWebElement> myElements;

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.core.foundation.webdriver.locator;2import java.util.HashMap;3import java.util.Map;4import com.qaprosoft.carina.core.foundation.utils.Configuration;5public class LocalizedAnnotations {6 private static final Map<String, Map<String, String>> localizedAnnotationsMap = new HashMap<>();7 public static Map<String, String> getLocalizedAnnotations(String language) {8 Map<String, String> localizedAnnotations = localizedAnnotationsMap.get(language);9 if (localizedAnnotations == null) {10 localizedAnnotations = new HashMap<>();11 String prefix = "localized_annotations." + language + ".";12 for (String key : Configuration.getKeys(prefix)) {13 String value = Configuration.get(key);14 localizedAnnotations.put(key.replace(prefix, ""), value);15 }16 localizedAnnotationsMap.put(language, localizedAnnotations);17 }18 return localizedAnnotations;19 }20}21package com.qaprosoft.carina.core.foundation.webdriver.locator;22import java.util.HashMap;23import java.util.Map;24import com.qaprosoft.carina.core.foundation.utils.Configuration;25public class LocalizedAnnotations {26 private static final Map<String, Map<String, String>> localizedAnnotationsMap = new HashMap<>();27 public static Map<String, String> getLocalizedAnnotations(String language) {28 Map<String, String> localizedAnnotations = localizedAnnotationsMap.get(language);29 if (localizedAnnotations == null) {30 localizedAnnotations = new HashMap<>();31 String prefix = "localized_annotations." + language + ".";32 for (String key : Configuration.getKeys(prefix)) {33 String value = Configuration.get(key);34 localizedAnnotations.put(key.replace(prefix, ""), value);35 }36 localizedAnnotationsMap.put(language, localizedAnnotations);37 }38 return localizedAnnotations;39 }40}

Full Screen

Full Screen

LocalizedAnnotations

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.demo;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.support.FindBy;4import org.openqa.selenium.support.PageFactory;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.support.PageFactoryFinder;7import org.testng.Assert;8import org.testng.annotations.AfterMethod;9import org.testng.annotations.BeforeMethod;10import org.testng.annotations.Test;11import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;12import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;13import com.qaprosoft.carina.demo.gui.pages.HomePage;14public class LocalizedAnnotationsTest {15 private WebDriver driver;16 private HomePage homePage;17 @FindBy(name = "q", using = "name")18 private WebElement searchField;19 @FindBy(name = "btnK", using = "name")20 private WebElement searchButton;21 @FindBy(name = "btnI", using = "name")22 private WebElement luckyButton;23 @FindBy(name = "q", using = "name")24 private ExtendedWebElement searchField1;25 @FindBy(name = "btnK", using = "name")26 private ExtendedWebElement searchButton1;27 @FindBy(name = "btnI", using = "name")28 private ExtendedWebElement luckyButton1;29 public void beforeMethod() {30 driver = LocalizedAnnotations.initDriver("chrome");31 homePage = new HomePage(driver);32 homePage.open();33 }34 public void testLocalizedAnnotations() {35 Assert.assertTrue(searchField.isDisplayed());36 Assert.assertTrue(searchButton.isDisplayed());37 Assert.assertTrue(luckyButton.isDisplayed());38 }39 public void testExtendedWebElement() {40 Assert.assertTrue(searchField1.isElementPresent());41 Assert.assertTrue(searchButton1.isElementPresent());42 Assert.assertTrue(luckyButton1.isElementPresent());43 }44 public void afterMethod() {45 driver.quit();46 }47}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Carina automation tests on LambdaTest cloud grid

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

Most used methods in LocalizedAnnotations

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful