use of com.qaprosoft.carina.core.foundation.utils.factory.DeviceType.Type in project carina by qaprosoft.
the class CustomTypePageFactory method initPage.
public static <T extends AbstractPage> T initPage(WebDriver driver, Class<T> parentClass, Object... parameters) {
if (driver == null) {
LOGGER.error("Page isn't created. There is no any initialized driver for thread: " + Thread.currentThread().getId());
throw new RuntimeException("Page isn't created. Driver isn't initialized.");
}
Set<Class<? extends T>> setClasses = reflections.getSubTypesOf(parentClass);
LOGGER.debug("Relatives classes count:" + setClasses.size());
Class<? extends T> versionClass = null, majorVersionClass = null, deviceClass = null, familyClass = null, requiredClass = null;
Type screenType = DevicePool.getDevice().getDeviceType();
Device device = DevicePool.getDevice();
// default version in case if it is desktop driver
String deviceVersion = "1";
if (!device.getOsVersion().isEmpty()) {
deviceVersion = device.getOsVersion();
}
String majorVersionNumber = deviceVersion.split(VERSION_SPLITTER)[0];
LOGGER.debug("Major version of device OS: " + majorVersionNumber);
for (Class<? extends T> clazz : setClasses) {
if (clazz.getAnnotation(DeviceType.class) == null || clazz.getAnnotation(DeviceType.class).parentClass() != parentClass) {
LOGGER.debug("Removing as parentClass is not satisfied or due to absence of @DeviceType annotation:" + clazz.getClass().getName());
continue;
}
DeviceType dt = clazz.getAnnotation(DeviceType.class);
if (dt.pageType().equals(screenType)) {
LOGGER.debug("Expected screenType: " + screenType);
LOGGER.debug("Actual screenType: " + dt.pageType());
if (Arrays.asList(dt.version()).contains(deviceVersion)) {
LOGGER.debug("Expected version: " + deviceVersion);
LOGGER.debug("Actual versions: " + dt.version());
versionClass = clazz;
break;
}
for (String version : dt.version()) {
if (version.split(VERSION_SPLITTER)[0].equals(majorVersionNumber)) {
majorVersionClass = clazz;
LOGGER.debug("Class was chosen by major version number of device");
break;
}
}
deviceClass = clazz;
continue;
}
if (dt.pageType().getFamily().equals(screenType.getFamily())) {
LOGGER.debug(String.format("Family class '%s' correspond to required page.", screenType.getFamily()));
familyClass = clazz;
}
}
Constructor<? extends T> ctor;
try {
if (versionClass != null) {
LOGGER.debug("Instance by version and platform will be created.");
requiredClass = versionClass;
} else if (majorVersionClass != null) {
LOGGER.debug("Instance by major version and platform will be created.");
requiredClass = majorVersionClass;
} else if (deviceClass != null) {
LOGGER.debug("Instance by platform will be created.");
requiredClass = deviceClass;
} else if (familyClass != null) {
LOGGER.debug("Instance by family will be created.");
requiredClass = familyClass;
} else {
throw new RuntimeException(String.format("There is no any class that satisfy to required conditions: [parent class - %s], [device type - %s]", parentClass.getName(), screenType));
}
// handle cases where we have only WebDriver as ctor parameter
if (parameters.length == 0) {
parameters = new Object[] { driver };
}
LOGGER.debug("Invoking constructor for " + requiredClass);
ctor = getConstructorByParams(requiredClass, parameters);
return ctor.newInstance(parameters);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
LOGGER.debug("Discovered one of the InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException");
throw new RuntimeException("Unable to instantiate page!", e);
}
}
Aggregations