org.openqa.selenium.support.FindBys Java Examples

The following examples show how to use org.openqa.selenium.support.FindBys. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: FindBysConverter.java    From webtester-core with Apache License 2.0 4 votes vote down vote up
public FindBysConverter(FindBys findBys) {
    this.findBys = findBys;
}
 
Example #11
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 FindBys @FindBys} instance.
 *
 * @param findBys the annotation instance to use.
 * @return the created identification
 * @since 0.9.9
 */
public static Identification fromAnnotation(FindBys findBys) {
    return new Identification(new FindBysConverter(findBys).buildBy());
}