ru.yandex.qatools.htmlelements.utils.HtmlElementUtils Maven / Gradle / Ivy
package ru.yandex.qatools.htmlelements.utils;
import com.google.common.collect.Lists;
import org.apache.commons.lang.WordUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import ru.yandex.qatools.htmlelements.annotations.Name;
import ru.yandex.qatools.htmlelements.element.HtmlElement;
import ru.yandex.qatools.htmlelements.element.TypifiedElement;
import ru.yandex.qatools.htmlelements.exceptions.HtmlElementsException;
import java.lang.reflect.*;
import java.net.URL;
import java.util.List;
import static org.apache.commons.lang3.reflect.ConstructorUtils.invokeConstructor;
/**
* Contains utility methods used in framework.
*
* @author Alexander Tolmachev [email protected]
* Date: 21.08.12
*/
public final class HtmlElementUtils {
private HtmlElementUtils() {
}
public static T newInstance(Class clazz, Object... args) throws IllegalAccessException,
InstantiationException, NoSuchMethodException, InvocationTargetException {
if (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) {
Class outerClass = clazz.getDeclaringClass();
Object outerObject = outerClass.newInstance();
return invokeConstructor(clazz, Lists.asList(outerObject, args).toArray());
} else {
return invokeConstructor(clazz, args);
}
}
public static boolean isHtmlElement(Field field) {
return isHtmlElement(field.getType());
}
public static boolean isHtmlElement(Class> clazz) {
return HtmlElement.class.isAssignableFrom(clazz);
}
public static boolean isHtmlElement(T instance) {
return HtmlElement.class.isInstance(instance);
}
public static boolean isTypifiedElement(Field field) {
return isTypifiedElement(field.getType());
}
public static boolean isTypifiedElement(Class> clazz) {
return TypifiedElement.class.isAssignableFrom(clazz);
}
public static boolean isWebElement(Field field) {
return isWebElement(field.getType());
}
public static boolean isWebElement(Class> clazz) {
return WebElement.class.isAssignableFrom(clazz);
}
public static boolean isHtmlElementList(Field field) {
if (!isParameterizedList(field)) {
return false;
}
Class listParameterClass = getGenericParameterClass(field);
return isHtmlElement(listParameterClass);
}
public static boolean isTypifiedElementList(Field field) {
if (!isParameterizedList(field)) {
return false;
}
Class listParameterClass = getGenericParameterClass(field);
return isTypifiedElement(listParameterClass);
}
public static boolean isWebElementList(Field field) {
if (!isParameterizedList(field)) {
return false;
}
Class listParameterClass = getGenericParameterClass(field);
return isWebElement(listParameterClass);
}
public static Class getGenericParameterClass(Field field) {
if (!hasGenericParameter(field)) {
return null;
}
Type genericType = field.getGenericType();
return (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
}
private static boolean isParameterizedList(Field field) {
return isList(field) && hasGenericParameter(field);
}
private static boolean isList(Field field) {
return List.class.isAssignableFrom(field.getType());
}
private static boolean hasGenericParameter(Field field) {
return field.getGenericType() instanceof ParameterizedType;
}
public static String getElementName(Field field) {
if (field.isAnnotationPresent(Name.class)) {
return field.getAnnotation(Name.class).value();
}
if (field.getType().isAnnotationPresent(Name.class)) {
return field.getType().getAnnotation(Name.class).value();
} else {
return splitCamelCase(field.getName());
}
}
public static String getElementName(Class clazz) {
if (clazz.isAnnotationPresent(Name.class)) {
return clazz.getAnnotation(Name.class).value();
} else {
return splitCamelCase(clazz.getSimpleName());
}
}
private static String splitCamelCase(String camel) {
return WordUtils.capitalizeFully(camel.replaceAll(
String.format("%s|%s|%s",
"(?<=[A-Z])(?=[A-Z][a-z])",
"(?<=[^A-Z])(?=[A-Z])",
"(?<=[A-Za-z])(?=[^A-Za-z])"
),
" "
));
}
public static boolean isRemoteWebElement(WebElement element) {
return element.getClass().equals(RemoteWebElement.class);
}
public static boolean isOnRemoteWebDriver(WebElement element) {
if (!isRemoteWebElement(element)) {
return false;
}
// Since subclasses of RemoteWebElement were finally removed in Selenium 2.26.0, WebElements on local drivers
// are also instances of RemoteWebElement class. The only way that we found at the current moment to find out
// whether WebElement instance is on remote driver is to check the class of RemoteWebElement "parent" filed,
// which contains WebDriver instance to which this RemoteWebElement belongs.
// As this field has protected access this is done by reflection.
// TODO It's is a kind of a dirty hack to be improved in future versions.
RemoteWebElement remoteWebElement = (RemoteWebElement) element;
try {
Field elementParentFiled = RemoteWebElement.class.getDeclaredField("parent");
elementParentFiled.setAccessible(true);
WebDriver elementParent = (WebDriver) elementParentFiled.get(remoteWebElement);
return elementParent.getClass().equals(RemoteWebDriver.class);
} catch (NoSuchFieldException e) {
throw new HtmlElementsException("Unable to find out if WebElement is on remote driver", e);
} catch (IllegalAccessException e) {
throw new HtmlElementsException("Unable to find out if WebElement is on remote driver", e);
}
}
public static boolean existsInClasspath(final String fileName) {
return getResourceFromClasspath(fileName) != null;
}
public static URL getResourceFromClasspath(final String fileName) {
return Thread.currentThread().getContextClassLoader().getResource(fileName);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy