com.qmetry.qaf.automation.cucumber.CucumberStep Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qaf-cucumber Show documentation
Show all versions of qaf-cucumber Show documentation
Functional test automation framework for web, mobile-web, mobile native and web-service
package com.qmetry.qaf.automation.cucumber;
import static com.qmetry.qaf.automation.core.ConfigurationManager.getBundle;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.configuration.ConfigurationMap;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.lang.text.StrSubstitutor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.qmetry.qaf.automation.core.AutomationError;
import com.qmetry.qaf.automation.gson.GsonDeserializerObjectWrapper;
import com.qmetry.qaf.automation.gson.ObjectWrapper;
import com.qmetry.qaf.automation.step.BaseTestStep;
import com.qmetry.qaf.automation.step.QAFTestStepArgumentFormatter;
import com.qmetry.qaf.automation.step.QAFTestStepArgumentFormatterImpl;
import com.qmetry.qaf.automation.step.TestStep;
import io.cucumber.core.backend.CucumberInvocationTargetException;
import io.cucumber.core.backend.ParameterInfo;
import io.cucumber.core.backend.StepDefinition;
import io.cucumber.datatable.DataTable;
/**
* This is a wrapper class for cucumber step implementation. This class will be
* used when QAF BDD factory is used as runner.
*
* @author chirag.jayswal
*
*/
public class CucumberStep extends BaseTestStep {
StepDefinition s;
public CucumberStep(StepDefinition s) {
super(s.toString(), getPattern(s));
this.s = s;
}
private static String getPattern(StepDefinition s) {
String pattern = s.getPattern();
if (s.parameterInfos().isEmpty() || pattern.contains("{")) {
return pattern;
}
return pattern + "{0}";
}
@Override
public String getSignature() {
return s.getLocation();
}
@Override
public TestStep clone() {
CucumberStep cloneObj = new CucumberStep(s);
if (null != actualArgs) {
cloneObj.actualArgs = actualArgs.clone();
}
return cloneObj;
}
@Override
protected Object doExecute() {
try {
Object[] args = processArgs(s.parameterInfos(), actualArgs);
s.execute(args);
} catch (CucumberInvocationTargetException cie) {
// e.printStackTrace();
AutomationError ae = new AutomationError(cie.getInvocationTargetExceptionCause() + "-" + s.getLocation(),
cie.getInvocationTargetExceptionCause());
ae.setStackTrace(cie.getInvocationTargetExceptionCause().getStackTrace());
throw ae;
} catch (Exception e) {
// e.printStackTrace();
throw new AutomationError(s.getLocation(), e.getCause());
}
return null;
}
protected Object[] processArgs(List list, Object... objects) {
int noOfParams = list.size();
if (noOfParams == 0) {
return null;
}
Object[] params = new Object[noOfParams];
Map context = getStepExecutionTracker().getContext();
try {
if ((noOfParams == (objects.length - 1)) && list.get(noOfParams - 1).getType().getClass().isArray()) {
// case of optional arguments!...
System.arraycopy(objects, 0, params, 0, objects.length);
params[noOfParams - 1] = "[]";
} else {
System.arraycopy(objects, 0, params, 0, noOfParams);
}
} catch (Exception e) {
throw new RuntimeException("Wrong number of parameters, Expected " + noOfParams
+ " parameters but Actual is " + (objects == null ? "0" : objects.length));
}
description = StrSubstitutor.replace(description, context);
description = getBundle().getSubstitutor().replace(description);
// Annotation[][] paramsAnnotations = list.getParameterAnnotations();
// context.put("__method", list);
QAFTestStepArgumentFormatter