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

cucumber.runtime.ThucydidesObjectFactory Maven / Gradle / Ivy

The newest version!
package cucumber.runtime;

import cucumber.runtime.CucumberException;
import cucumber.runtime.java.ObjectFactory;
import net.thucydides.core.Thucydides;
import net.thucydides.core.pages.Pages;
import net.thucydides.core.webdriver.ThucydidesWebDriverSupport;

import java.lang.reflect.Constructor;
import java.util.*;

/**
 * Thucydides factory for cucumber tests.
 *
 * @author Liviu Carausu ([email protected]).
 */

public class ThucydidesObjectFactory implements ObjectFactory {

    private final Set> classes = Collections.synchronizedSet(new HashSet>());

    private final Map, Object> instances = Collections.synchronizedMap(new HashMap, Object>());

    public void start() {
    }

    public void stop() {
        instances.clear();
    }

    public void addClass(Class clazz) {
        classes.add(clazz);
    }

    public  T getInstance(Class type) {
        T instance = type.cast(instances.get(type));
        if (instance == null) {
            instance = cacheNewInstance(type);
        }
        return instance;
    }

    /**
     * Tries to instantiate the type using an empty constructor, if it does not work, tries to instantiate
     * using a constructor wit a Pages parameter.
     * @param type
     * @param 
     * @return
     */
    private  T cacheNewInstance(Class type) {
        T instance;
        try {
            Constructor constructor = type.getConstructor();
            instance = constructor.newInstance();
        } catch (NoSuchMethodException e) {
            instance = createNewPageEnabledStepCandidate(type);
        } catch (Exception e) {
            throw new CucumberException(String.format("Failed to instantiate %s", type), e);
        }
        Thucydides.initializeWithNoStepListener(instance);
        instances.put(type, instance);
        return instance;
    }

    private  T createNewPageEnabledStepCandidate(final Class type) {
        T newInstance = null;
        try {
            Pages pageFactory = ThucydidesWebDriverSupport.getPages();
            Class[] constructorArgs = new Class[1];
            constructorArgs[0] = Pages.class;
            Constructor constructor = type.getConstructor(constructorArgs);
            newInstance = constructor.newInstance(pageFactory);
        } catch (Exception e) {
            throw new CucumberException(String.format("%s doesn't have an empty or a page enabled constructor.", type), e);
        }
        return newInstance;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy