All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.behaviortest.api.engine.ProcedureExecution Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package br.com.behaviortest.api.engine;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.openqa.selenium.WebDriver;

import br.com.behaviortest.api.engine.holder.ExecutionHolder;
import br.com.behaviortest.api.engine.inicializer.InicializerService;
import br.com.behaviortest.api.engine.loader.AnnotationLoader;
import br.com.behaviortest.api.engine.proxy.PageProxy;
import br.com.behaviortest.model.design.Dataset;
import br.com.behaviortest.model.design.Procedure;
import net.sf.cglib.proxy.Enhancer;

/**
 * @author Felipe Rudolfe
 * @since 12 de jan de 2020
 */
public class ProcedureExecution {

	private ProcedureExecution() {}

	public static void execute(WebDriver driver, Dataset dataset, List procedures)
			throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {

		ExecutionHolder.cleanActions();
		for (Object item : procedures) {

			if (item instanceof Method) {

				Method method = (Method) item;

				Object objectScript = InicializerService.newInstance(method.getDeclaringClass());

				List pages = AnnotationLoader.getPages(method.getDeclaringClass());
				for (Field page : pages) {

					PageObject pageObject = (PageObject) page.getType().cast(Enhancer.create(page.getType(), new PageProxy()));

					pageObject.setDriver(driver);
					page.setAccessible(Boolean.TRUE);
					page.set(objectScript, pageObject);
				}

				if (method.getGenericParameterTypes().length == 0) {
					method.invoke(objectScript);
				} else {
					method.invoke(objectScript, dataset);
				}


			} else {

				Field field = (Field) item;
				Class type = field.getType();

				Object objectScript = InicializerService.newInstance(field.getDeclaringClass());
				Object objectProcedure = InicializerService.newInstance(type);

				field.setAccessible(Boolean.TRUE);
				field.set(objectScript, objectProcedure);

				List pages = AnnotationLoader.getPages(type);
				for (Field page : pages) {

					PageObject pageObject = (PageObject) page.getType().cast(Enhancer.create(page.getType(), new PageProxy()));

					pageObject.setDriver(driver);
					page.setAccessible(Boolean.TRUE);
					page.set(objectProcedure, pageObject);
				}

				((Procedure) objectProcedure).run(dataset);
			}

		}
	}
}