下面列出了怎么用org.openqa.selenium.support.FindBys的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
@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;
}
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");
}
}
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;
}
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);
}
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;
}
@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);
}
@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);
}
}
}
private boolean shouldInitializeField(Field field) {
return field.getAnnotation(IdentifyUsing.class) != null || field.getAnnotation(FindBy.class) != null
|| field.getAnnotation(FindBys.class) != null;
}
public FindBysConverter(FindBys findBys) {
this.findBys = findBys;
}
/**
* 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());
}