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

net.thucydides.core.steps.StepData Maven / Gradle / Ivy

There is a newer version: 0.9.275
Show newest version
package net.thucydides.core.steps;

import net.thucydides.core.csv.CSVTestDataSource;
import net.thucydides.core.csv.TestDataSource;

import java.io.IOException;
import java.util.List;

/**
 * Data-driven test step execution.
 * These methods let you load
 */
public final class StepData {

    private final String testDataSource;
    private char separator = ',';
    private StepFactory factory;

    private static final ThreadLocal factoryThreadLocal = new ThreadLocal();

    public StepData(final String testDataSource) {
        this.testDataSource = testDataSource;
    }

    public static StepData withTestDataFrom(final String testDataSource) {
        return new StepData(testDataSource);
    }

    public  T run(final T steps) throws IOException {

        useDefaultStepFactoryIfUnassigned();
        TestDataSource testdata = new CSVTestDataSource(testDataSource, separator);

        Class scenarioStepsClass = (Class) steps.getClass().getSuperclass();
        List instanciatedSteps = (List) testdata.getInstanciatedInstancesFrom(scenarioStepsClass, factory);

        T stepsProxy = (T) DataDrivenStepFactory.newDataDrivenSteps(scenarioStepsClass, instanciatedSteps);

        return stepsProxy;
    }

    private void useDefaultStepFactoryIfUnassigned() {
        if (factory == null) {
            factory = getDefaultStepFactory();
        }
    }

    public StepData usingFactory(final StepFactory factory) {
        this.factory = factory;
        return this;
    }

    public static void setDefaultStepFactory(final StepFactory factory) {
        factoryThreadLocal.set(factory);
    }

    public static StepFactory getDefaultStepFactory() {
        return factoryThreadLocal.get();
    }

    public StepData separatedBy(char newSeparator) {
        this.separator = newSeparator;
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy