下面列出了org.openqa.selenium.support.FindBys#org.openqa.selenium.support.FindBy 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
}
@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;
}
/**
* 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();
}
}
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);
}
/**
* Получение элемента-списка со страницы по имени
*/
@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));
}
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;
}
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);
}
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;
}
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);
}
@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 FindByConverter(FindBy findBy) {
this.findBy = findBy;
}
public String getElementLocatorFromReferencedView(String viewName, String elementName) {
FindBy findByAnnotatio = getFindByDescriptor(viewName, elementName);
return findByAnnotatio.how() + ":" + findByAnnotatio.using();
}
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;
}
}
/**
* 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());
}