org.openqa.selenium.support.FindBy Java Examples

The following examples show how to use org.openqa.selenium.support.FindBy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PageBase.java    From SeleniumCucumber with GNU General Public License v3.0 6 votes vote down vote up
private By getFindByAnno(FindBy anno){
	log.info(anno);
	switch (anno.how()) {
	
	case CLASS_NAME:
		return new By.ByClassName(anno.using());
	case CSS:
		return new By.ByCssSelector(anno.using());
	case ID:
		return new By.ById(anno.using());
	case LINK_TEXT:
		return new By.ByLinkText(anno.using());
	case NAME:
		return new By.ByName(anno.using());
	case PARTIAL_LINK_TEXT:
		return new By.ByPartialLinkText(anno.using());
	case XPATH:
		return new By.ByXPath(anno.using());
	default :
		throw new IllegalArgumentException("Locator not Found : " + anno.how() + " : " + anno.using());
	}
}
 
Example #2
Source File: ExtendedElementLocator.java    From carina with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new element locator.
 * 
 * @param searchContext The context to use when finding the element
 * @param field The field on the Page Object that will hold the located
 *            value
 */
public ExtendedElementLocator(SearchContext searchContext, Field field) {
    this.searchContext = searchContext;

    if (field.isAnnotationPresent(FindBy.class) || field.isAnnotationPresent(ExtendedFindBy.class)) {
        LocalizedAnnotations annotations = new LocalizedAnnotations(field);
        this.shouldCache = true;
        this.caseInsensitive = false;
        this.by = annotations.buildBy();
        if (field.isAnnotationPresent(DisableCacheLookup.class)) {
            this.shouldCache = false;
        }
        if (field.isAnnotationPresent(CaseInsensitiveXPath.class)) {
            this.caseInsensitive = true;
        }
    }
    // Elements to be recognized by Alice
    if (field.isAnnotationPresent(FindByAI.class)) {
        this.aiCaption = field.getAnnotation(FindByAI.class).caption();
        this.aiLabel = field.getAnnotation(FindByAI.class).label();
    }

}
 
Example #3
Source File: DefaultFieldDecorator.java    From selenium with Apache License 2.0 6 votes vote down vote up
protected boolean isDecoratableList(Field field) {
  if (!List.class.isAssignableFrom(field.getType())) {
    return false;
  }

  // Type erasure in Java isn't complete. Attempt to discover the generic
  // type of the list.
  Type genericType = field.getGenericType();
  if (!(genericType instanceof ParameterizedType)) {
    return false;
  }

  Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];

  if (!WebElement.class.equals(listType)) {
    return false;
  }

  return field.getAnnotation(FindBy.class) != null ||
         field.getAnnotation(FindBys.class) != null ||
         field.getAnnotation(FindAll.class) != null;
}
 
Example #4
Source File: ElementFactory.java    From qaf with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private boolean isDecoratable(Field field) {
	if (!hasAnnotation(field, com.qmetry.qaf.automation.ui.annotations.FindBy.class, FindBy.class, FindBys.class)) {
		return false;
	}
	if (WebElement.class.isAssignableFrom(field.getType())) {
		return true;
	}
	if (!(List.class.isAssignableFrom(field.getType()))) {
		return false;
	}
	Type genericType = field.getGenericType();
	if (!(genericType instanceof ParameterizedType)) {
		return false;
	}
	Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];

	return WebElement.class.isAssignableFrom((Class<?>) listType);
}
 
Example #5
Source File: Annotations.java    From selenium with Apache License 2.0 6 votes vote down vote up
protected void assertValidAnnotations() {
  FindBys findBys = field.getAnnotation(FindBys.class);
  FindAll findAll = field.getAnnotation(FindAll.class);
  FindBy findBy = field.getAnnotation(FindBy.class);
  if (findBys != null && findBy != null) {
    throw new IllegalArgumentException("If you use a '@FindBys' annotation, " +
         "you must not also use a '@FindBy' annotation");
  }
  if (findAll != null && findBy != null) {
    throw new IllegalArgumentException("If you use a '@FindAll' annotation, " +
         "you must not also use a '@FindBy' annotation");
  }
  if (findAll != null && findBys != null) {
    throw new IllegalArgumentException("If you use a '@FindAll' annotation, " +
         "you must not also use a '@FindBys' annotation");
  }
}
 
Example #6
Source File: DefaultElementByBuilder.java    From java-client with Apache License 2.0 6 votes vote down vote up
@Override
protected By buildDefaultBy() {
    AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
    By defaultBy = null;
    FindBy findBy = annotatedElement.getAnnotation(FindBy.class);
    if (findBy != null) {
        defaultBy = new FindBy.FindByBuilder().buildIt(findBy, (Field) annotatedElement);
    }

    if (defaultBy == null) {
        FindBys findBys = annotatedElement.getAnnotation(FindBys.class);
        if (findBys != null) {
            defaultBy = new FindBys.FindByBuilder().buildIt(findBys, (Field) annotatedElement);
        }
    }

    if (defaultBy == null) {
        FindAll findAll = annotatedElement.getAnnotation(FindAll.class);
        if (findAll != null) {
            defaultBy = new FindAll.FindByBuilder().buildIt(findAll, (Field) annotatedElement);
        }
    }
    return defaultBy;
}
 
Example #7
Source File: AkitaPage.java    From akita with Apache License 2.0 5 votes vote down vote up
/**
 * Получение элемента-списка со страницы по имени
 */
@SuppressWarnings("unchecked")
public ElementsCollection getElementsList(String listName) {
    Object value = namedElements.get(listName);
    if (!(value instanceof List)) {
        throw new IllegalArgumentException("Список " + listName + " не описан на странице " + this.getClass().getName());
    }
    FindBy listSelector = Arrays.stream(this.getClass().getDeclaredFields())
            .filter(f -> f.getDeclaredAnnotation(Name.class) != null && f.getDeclaredAnnotation(Name.class).value().equals(listName))
            .map(f -> f.getDeclaredAnnotation(FindBy.class))
            .findFirst().get();
    FindBy.FindByBuilder findByBuilder = new FindBy.FindByBuilder();
    return $$(findByBuilder.buildIt(listSelector, null));
}
 
Example #8
Source File: ExtendedFieldDecorator.java    From carina with Apache License 2.0 5 votes vote down vote up
protected ExtendedWebElement proxyForLocator(ClassLoader loader, Field field, ElementLocator locator) {
    InvocationHandler handler = new LocatingElementHandler(locator);
    WebElement proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[] { WebElement.class, WrapsElement.class, Locatable.class },
            handler);
    return new ExtendedWebElement(proxy, field.getName(),
            field.isAnnotationPresent(FindBy.class) || field.isAnnotationPresent(ExtendedFindBy.class)? new LocalizedAnnotations(field).buildBy() : null);
}
 
Example #9
Source File: ElementService.java    From senbot with MIT License 5 votes vote down vote up
public FindBy getFindByDescriptor(String viewName, String elementName) {
	Class pageRepresentationReference = getReferenceService().getPageRepresentationReference(viewName);
	Field field = getElementFieldFromReferencedView(pageRepresentationReference, viewName, elementName);
	
	FindBy findByAnnotatio = field.getAnnotation(FindBy.class);
	return findByAnnotatio;
}
 
Example #10
Source File: DefaultElementByBuilder.java    From java-client with Apache License 2.0 5 votes vote down vote up
@Override
protected void assertValidAnnotations() {
    AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
    FindBy findBy = annotatedElement.getAnnotation(FindBy.class);
    FindBys findBys = annotatedElement.getAnnotation(FindBys.class);
    checkDisallowedAnnotationPairs(findBy, findBys);
    FindAll findAll = annotatedElement.getAnnotation(FindAll.class);
    checkDisallowedAnnotationPairs(findBy, findAll);
    checkDisallowedAnnotationPairs(findBys, findAll);
}
 
Example #11
Source File: DefaultPageObjectFactory.java    From webtester-core with Apache License 2.0 5 votes vote down vote up
private Identification getIdentificationForField(Field field) {
    IdentifyUsing identifyUsing = field.getAnnotation(IdentifyUsing.class);
    if (identifyUsing != null) {
        return Identifications.fromAnnotation(identifyUsing);
    }
    FindBy findBy = field.getAnnotation(FindBy.class);
    if (findBy != null) {
        return Identifications.fromAnnotation(findBy);
    }
    FindBys findBys = field.getAnnotation(FindBys.class);
    if (findBys != null) {
        return Identifications.fromAnnotation(findBys);
    }
    return null;
}
 
Example #12
Source File: PageBase.java    From SeleniumCucumber with GNU General Public License v3.0 5 votes vote down vote up
protected By getElemetLocator(Object obj,String element) throws SecurityException,NoSuchFieldException {
	Class childClass = obj.getClass();
	By locator = null;
	try {
		locator = getFindByAnno(childClass.
				 getDeclaredField(element).
				 getAnnotation(FindBy.class));
	} catch (SecurityException | NoSuchFieldException e) {
		log.equals(e);
		throw e;
	}
	log.debug(locator);
	return locator;
}
 
Example #13
Source File: SelenideAppiumFieldDecorator.java    From selenide-appium with MIT License 5 votes vote down vote up
private boolean isDecoratableList(Field field, Class<?> type) {
  if (!List.class.isAssignableFrom(field.getType())) {
    return false;
  }

  Class<?> listType = getListGenericType(field);

  return listType != null && type.isAssignableFrom(listType)
    && (field.getAnnotation(FindBy.class) != null || field.getAnnotation(FindBys.class) != null);
}
 
Example #14
Source File: FindByConverter.java    From webtester-core with Apache License 2.0 4 votes vote down vote up
public FindByConverter(FindBy findBy) {
    this.findBy = findBy;
}
 
Example #15
Source File: ElementService.java    From senbot with MIT License 4 votes vote down vote up
public String getElementLocatorFromReferencedView(String viewName, String elementName) {
	FindBy findByAnnotatio = getFindByDescriptor(viewName, elementName);
	return findByAnnotatio.how() + ":" + findByAnnotatio.using();
}
 
Example #16
Source File: DefaultPageObjectFactory.java    From webtester-core with Apache License 2.0 4 votes vote down vote up
private boolean shouldInitializeField(Field field) {
    return field.getAnnotation(IdentifyUsing.class) != null || field.getAnnotation(FindBy.class) != null
        || field.getAnnotation(FindBys.class) != null;
}
 
Example #17
Source File: ElementFactory.java    From qaf with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void initFields(Object classObj) {
	Field[] flds = ClassUtil.getAllFields(classObj.getClass(), AbstractTestPage.class);
	for (Field field : flds) {
		try {
			field.setAccessible(true);
			if (isDecoratable(field)) {
				Object value = null;
				if (hasAnnotation(field, FindBy.class, FindBys.class)) {
					Annotations annotations = new Annotations(field);
					boolean cacheElement = annotations.isLookupCached();
					By by = annotations.buildBy();
					if (List.class.isAssignableFrom(field.getType())) {
						value = initList(by, context);
					} else {
						if (context instanceof WebElement) {
							value = new QAFExtendedWebElement((QAFExtendedWebElement) context, by);
						} else {
							value = new QAFExtendedWebElement((QAFExtendedWebDriver) context, by, cacheElement);
						}
						initMetadata(classObj, field, (QAFExtendedWebElement) value);

					}
				} else {
					com.qmetry.qaf.automation.ui.annotations.FindBy findBy = field
							.getAnnotation(com.qmetry.qaf.automation.ui.annotations.FindBy.class);
					if (List.class.isAssignableFrom(field.getType())) {
						value = initList(field, findBy.locator(), context, classObj);
					} else {
						if (QAFWebComponent.class.isAssignableFrom(field.getType())) {
							value = ComponentFactory.getObject(field.getType(), findBy.locator(), classObj,
									context);

						} else {
							value = $(findBy.locator());

							if (context instanceof QAFExtendedWebElement) {
								((QAFExtendedWebElement) value).parentElement = (QAFExtendedWebElement) context;
							}
						}
						initMetadata(classObj, field, (QAFExtendedWebElement) value);
					}
				}

				field.set(classObj, value);
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
}
 
Example #18
Source File: ExtendedFieldDecorator.java    From carina with Apache License 2.0 4 votes vote down vote up
public Object decorate(ClassLoader loader, Field field) {
      if ((!field.isAnnotationPresent(FindBy.class) && !field.isAnnotationPresent(ExtendedFindBy.class) && !field.isAnnotationPresent(FindByAI.class))
              /*
               * Enable field decorator logic only in case of
               * presence the FindBy/FindByCarina/FindByAI annotation in the
               * field
               */ ||
              !(ExtendedWebElement.class.isAssignableFrom(field.getType()) || AbstractUIObject.class.isAssignableFrom(field.getType())
                      || isDecoratableList(field)) /*
                                                    * also verify that it is ExtendedWebElement or derived from AbstractUIObject or DecoratableList
                                                    */) {
          // returning null is ok in this method.
          return null;
      }

      ElementLocator locator;
      try {
          locator = factory.createLocator(field);
      } catch (Exception e) {
          LOGGER.error(e.getMessage(), e);
          return null;
      }
      if (locator == null) {
          return null;
      }
if (((ExtendedElementLocatorFactory) factory).isRootElementUsed()) {
	LOGGER.debug("Setting setShouldCache=false for locator: " + getLocatorBy(locator).toString());
	((ExtendedElementLocator) locator).setShouldCache(false);
}

      if (ExtendedWebElement.class.isAssignableFrom(field.getType())) {
          return proxyForLocator(loader, field, locator);
      }
      if (AbstractUIObject.class.isAssignableFrom(field.getType())) {
          return proxyForAbstractUIObject(loader, field, locator);
      } else if (List.class.isAssignableFrom(field.getType())) {
          Type listType = getListType(field);
          if (ExtendedWebElement.class.isAssignableFrom((Class<?>) listType)) {
              return proxyForListLocator(loader, field, locator);
          } else if (AbstractUIObject.class.isAssignableFrom((Class<?>) listType)) {
              return proxyForListUIObjects(loader, field, locator);
          } else {
              return null;
          }
      } else {
          return null;
      }
  }
 
Example #19
Source File: Identifications.java    From webtester-core with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an {@link Identification identification} from the given
 * {@link FindBy @FindBy} instance.
 *
 * @param findBy the annotation instance to use.
 * @return the created identification
 * @since 0.9.9
 */
public static Identification fromAnnotation(FindBy findBy) {
    return new Identification(new FindByConverter(findBy).buildBy());
}