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

com.qmetry.qaf.automation.cucumber.CucumberStepsFinder Maven / Gradle / Ivy

Go to download

Functional test automation framework for web, mobile-web, mobile native and web-service

There is a newer version: 4.0.0-RC3
Show newest version
/**
 * 
 */
package com.qmetry.qaf.automation.cucumber;

import static com.qmetry.qaf.automation.core.ConfigurationManager.getBundle;
import static com.qmetry.qaf.automation.core.ConfigurationManager.getStepMapping;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.qmetry.qaf.automation.core.TestBaseProvider;
import com.qmetry.qaf.automation.keys.ApplicationProperties;
import com.qmetry.qaf.automation.step.ObjectFactory;

import io.cucumber.core.backend.Backend;
import io.cucumber.core.backend.DataTableTypeDefinition;
import io.cucumber.core.backend.DefaultDataTableCellTransformerDefinition;
import io.cucumber.core.backend.DefaultDataTableEntryTransformerDefinition;
import io.cucumber.core.backend.DefaultParameterTransformerDefinition;
import io.cucumber.core.backend.DocStringTypeDefinition;
import io.cucumber.core.backend.Glue;
import io.cucumber.core.backend.HookDefinition;
import io.cucumber.core.backend.ParameterTypeDefinition;
import io.cucumber.core.backend.StepDefinition;
import io.cucumber.core.feature.GluePath;
import io.cucumber.core.options.RuntimeOptions;
import io.cucumber.core.runtime.BackendServiceLoader;
import io.cucumber.core.runtime.ObjectFactoryServiceLoader;
import io.cucumber.core.runtime.ObjectFactorySupplier;
import io.cucumber.core.runtime.ThreadLocalObjectFactorySupplier;

/**
 * This class will be used to load cucumber steps when running your BDD with QAF
 * BDD Factory. If you are not using cucumber steps or running with Cucumber
 * runner this class will not have any effect. If you have added any object
 * factory dependency, it will use that object factory.
 * 
 * @author chirag.jayswal
 *
 */
public class CucumberStepsFinder implements Glue {
	private static final String CUCUMBER_BACKENDS = "cucumber.runtime.backend";
	private static final String CUCUMBER_GLUE = "cucumber.runtime.glue";

	private static final ObjectFactoryServiceLoader serviceLoader = new ObjectFactoryServiceLoader(
			RuntimeOptions.defaultOptions());
	private static final ObjectFactorySupplier objectFactorySupplier = new ThreadLocalObjectFactorySupplier(
			serviceLoader);

	@Override
	public void addStepDefinition(StepDefinition stepDefinition) {
		CucumberStep cucumberStep = new CucumberStep(stepDefinition);
		getStepMapping().put(cucumberStep.getName().toUpperCase(), cucumberStep);
	}

	@Override
	public void addBeforeHook(HookDefinition beforeHook) {
	}

	@Override
	public void addAfterHook(HookDefinition afterHook) {
	}

	@Override
	public void addBeforeStepHook(HookDefinition beforeStepHook) {
	}

	@Override
	public void addAfterStepHook(HookDefinition afterStepHook) {
	}

	@Override
	public void addParameterType(ParameterTypeDefinition parameterTypeDefinition) {
	}

	@Override
	public void addDataTableType(DataTableTypeDefinition dataTableTypeDefinition) {
	}

	@Override
	public void addDefaultParameterTransformer(DefaultParameterTransformerDefinition defaultParameterTransformer) {
	}

	@Override
	public void addDefaultDataTableEntryTransformer(
			DefaultDataTableEntryTransformerDefinition defaultDataTableEntryTransformer) {
	}

	@Override
	public void addDefaultDataTableCellTransformer(
			DefaultDataTableCellTransformerDefinition defaultDataTableCellTransformer) {

	}

	@Override
	public void addDocStringType(DocStringTypeDefinition docStringTypeDefinition) {

	}

	public static void buildBackendWorlds() {
		try {
			Collection backends = getBackends();
			objectFactorySupplier.get().start();
			// Lambda expressions get loaded during buildWorld instead of loadGlue method
			for (Backend backend : backends) {
				backend.buildWorld();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void disposeBackendWorlds() {
		try {
			Collection backends = getBackends();
			for (Backend backend : backends) {
				backend.disposeWorld();

			}
			objectFactorySupplier.get().stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	private static Collection getBackends() {
		Collection backends = (Collection) TestBaseProvider.instance().get()
				.getContext().getObject(CUCUMBER_BACKENDS);
		if (null == backends) {
			backends = new BackendServiceLoader(() -> Thread.currentThread().getContextClassLoader(),
					objectFactorySupplier).get().stream().filter(b -> !(b instanceof QAFBackend))
							.collect(Collectors.toList());

			TestBaseProvider.instance().get().getContext().setProperty(CUCUMBER_BACKENDS, backends);
			CucumberStepsFinder glue = new CucumberStepsFinder();
			TestBaseProvider.instance().get().getContext().setProperty(CUCUMBER_GLUE, glue);
			List uris = new ArrayList();
			for (String pkg : getBundle().getStringArray(ApplicationProperties.STEP_PROVIDER_PKG.key)) {
				uris.add(GluePath.parse(pkg));
			}
			for (Backend backend : getBackends()) {
				backend.loadGlue(glue, uris);
			}
			if (!(objectFactorySupplier.get().getClass().getName().endsWith("DefaultJavaObjectFactory"))) {
				ObjectFactory objFactoryImpl = new ObjectFactoryImpl(objectFactorySupplier);
				ObjectFactory.INSTANCE.setFactory(objFactoryImpl);
			}
		}
		return backends;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy