use of ru.yandex.qatools.htmlelements.element.HtmlElement in project page-factory-2 by sbtqa.
the class PageReflectUtil method findBlocks.
/**
* Finds blocks by required path/name in the given context. Block is a
* class that extends HtmlElement. If blockPath contains delimiters, it
* will be treated as a full path, and block should be located by the
* exactly that path. Otherwise, recursive search via all blocks is
* being performed
*
* @param blockPath full path or just a name of the block to search
* @param context object where the search will be performed
* @param returnFirstFound whether the search should be stopped on a
* first found block (for faster searches)
* @return list of found blocks. could be empty
* @throws IllegalAccessException if called with invalid context
*/
private static List<HtmlElement> findBlocks(String blockPath, Object context, boolean returnFirstFound) throws IllegalAccessException {
String[] blockChain;
if (blockPath.contains("->")) {
blockChain = blockPath.split("->");
} else {
blockChain = new String[] { blockPath };
}
List<HtmlElement> found = new ArrayList<>();
for (Field currentField : FieldUtilsExt.getDeclaredFieldsWithInheritance(context.getClass())) {
if (isBlockElement(currentField)) {
if (isRequiredElement(currentField, blockChain[0])) {
currentField.setAccessible(true);
// isBlockElement() ensures that this is a HtmlElement instance
HtmlElement foundBlock = (HtmlElement) currentField.get(context);
if (blockChain.length == 1) {
// Found required block directly inside the context
found.add(foundBlock);
if (returnFirstFound) {
return found;
}
} else {
// Continue to search in the element chain, reducing its length by the first found element
// +2 because '->' adds 2 symbols
String reducedPath = blockPath.substring(blockChain[0].length() + 2);
found.addAll(findBlocks(reducedPath, foundBlock, returnFirstFound));
}
} else if (blockChain.length == 1) {
found.addAll(findBlocks(blockPath, currentField.get(context), returnFirstFound));
}
}
}
return found;
}
use of ru.yandex.qatools.htmlelements.element.HtmlElement in project page-factory-2 by sbtqa.
the class PageReflectUtil method executeMethodByTitleInBlock.
/**
* Execute method with one or more parameters inside of the given block
* element !BEWARE! If there are several elements found by specified block
* path, a first one will be used!
*
* @param page the page on which the method will be executed
* @param blockPath block title, or a block chain string separated with
* '->' symbols
* @param actionTitle title of the action to execute
* @param parameters parameters that will be passed to method
* @throws NoSuchMethodException if required method couldn't be
* found
*/
public static void executeMethodByTitleInBlock(Page page, String blockPath, String actionTitle, Object... parameters) throws NoSuchMethodException {
HtmlElement block = findBlock(page, blockPath);
Method[] methods = block.getClass().getMethods();
for (Method method : methods) {
if (isRequiredAction(method, actionTitle)) {
try {
method.setAccessible(true);
if (parameters == null || parameters.length == 0) {
MethodUtils.invokeMethod(block, method.getName());
} else {
MethodUtils.invokeMethod(block, method.getName(), parameters);
}
return;
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new FactoryRuntimeException(String.format("Failed to execute method '%s' in the following block: '%s'", actionTitle, blockPath), e);
}
}
}
isUsedBlock = true;
usedBlock = block;
List<Method> methodList = getDeclaredMethods(page.getClass());
for (Method method : methodList) {
if (isRequiredAction(method, actionTitle)) {
try {
method.setAccessible(true);
MethodUtils.invokeMethod(page, method.getName(), parameters);
return;
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new FactoryRuntimeException(String.format("Failed to execute method '%s' in the following block: '%s'", actionTitle, blockPath), e);
}
}
}
throw new NoSuchMethodException(String.format("There is no '%s' method in block '%s'", actionTitle, blockPath));
}
use of ru.yandex.qatools.htmlelements.element.HtmlElement in project page-factory-2 by sbtqa.
the class HtmlReflection method executeMethodByTitleInBlock.
/**
* Execute method with one or more parameters inside of the given block
* element
*
* @param blockPath block title, or a block chain string separated with
* {@code ->} symbols
* @param actionTitle title of the action to execute
* @param parameters parameters that will be passed to method
*/
public void executeMethodByTitleInBlock(String blockPath, String actionTitle, Object... parameters) {
try {
HtmlElement block = ((HtmlFindUtils) Environment.getFindUtils()).find(blockPath, HtmlElement.class);
executeMethodByTitle(block, actionTitle, parameters);
} catch (ElementSearchError ex) {
throw new ElementSearchError(format("Block not found by path '%s'", blockPath), ex);
}
}
Aggregations