com.qmetry.qaf.automation.cucumber.bdd2.parser.Bdd2Compiler 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.bdd2.parser;
import static com.qmetry.qaf.automation.core.ConfigurationManager.getBundle;
import static com.qmetry.qaf.automation.data.MetaDataScanner.applyMetafilter;
import static com.qmetry.qaf.automation.data.MetaDataScanner.hasDP;
import static com.qmetry.qaf.automation.util.ClassUtil.setField;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.json.JSONArray;
import org.json.JSONObject;
import com.qmetry.qaf.automation.core.AutomationError;
import com.qmetry.qaf.automation.testng.dataprovider.QAFInetrceptableDataProvider;
import com.qmetry.qaf.automation.util.JSONUtil;
import gherkin.SymbolCounter;
import gherkin.ast.Background;
import gherkin.ast.DataTable;
import gherkin.ast.DocString;
import gherkin.ast.Examples;
import gherkin.ast.Feature;
import gherkin.ast.GherkinDocument;
import gherkin.ast.Location;
import gherkin.ast.Node;
import gherkin.ast.Scenario;
import gherkin.ast.ScenarioDefinition;
import gherkin.ast.ScenarioOutline;
import gherkin.ast.Step;
import gherkin.ast.TableCell;
import gherkin.ast.TableRow;
import gherkin.ast.Tag;
import gherkin.pickles.Argument;
import gherkin.pickles.Pickle;
import gherkin.pickles.PickleCell;
import gherkin.pickles.PickleLocation;
import gherkin.pickles.PickleRow;
import gherkin.pickles.PickleStep;
import gherkin.pickles.PickleString;
import gherkin.pickles.PickleTable;
import gherkin.pickles.PickleTag;
/**
*
* @author chirag.jayswal
*
*/
public class Bdd2Compiler {
public List compile(GherkinDocument gherkinDocument) {
List pickles = new ArrayList<>();
Feature feature = gherkinDocument.getFeature();
if (feature == null) {
return pickles;
}
String language = feature.getLanguage();
List featureTags = new ArrayList<>(feature.getTags());
featureTags.add(new Tag(feature.getLocation(), "@Feature:"+feature.getName()));
List backgroundSteps = new ArrayList<>();
for (ScenarioDefinition scenarioDefinition : feature.getChildren()) {
if (scenarioDefinition instanceof Background) {
backgroundSteps =scenarioDefinition.getSteps(); //pickleSteps(scenarioDefinition);
} else if (scenarioDefinition instanceof Scenario) {
Map metadata = getMetaData(featureTags, ((Scenario) scenarioDefinition).getTags(),
pickleLocation(scenarioDefinition.getLocation()));
if (applyMetafilter(metadata)) {
if (hasDP(metadata)) {
ScenarioOutline outline = new ScenarioOutline(((Scenario) scenarioDefinition).getTags(),
scenarioDefinition.getLocation(), scenarioDefinition.getKeyword(),
scenarioDefinition.getName(), scenarioDefinition.getDescription(),
scenarioDefinition.getSteps(), Collections.emptyList());
compileScenarioOutline(pickles, backgroundSteps, outline, featureTags, language, metadata);
} else {
compileScenario(pickles, backgroundSteps, (Scenario) scenarioDefinition, featureTags, language,
metadata);
}
}
} else {
Map metadata = getMetaData(featureTags,
((ScenarioOutline) scenarioDefinition).getTags(),
pickleLocation(scenarioDefinition.getLocation()));
if (applyMetafilter(metadata)) {
compileScenarioOutline(pickles, backgroundSteps, (ScenarioOutline) scenarioDefinition, featureTags,
language, metadata);
}
}
}
return pickles;
}
private void compileScenario(List pickles, List backgroundSteps, Scenario scenario,
List featureTags, String language, Map metadata) {
List steps = new ArrayList<>();
if (!scenario.getSteps().isEmpty())
steps.addAll(pickleSteps(backgroundSteps));
List scenarioTags = new ArrayList<>();
scenarioTags.addAll(featureTags);
scenarioTags.addAll(scenario.getTags());
steps.addAll(pickleSteps(scenario.getSteps()));
Pickle pickle = new Bdd2Pickle(scenario.getName(), language, steps, pickleTags(scenarioTags),
singletonList(pickleLocation(scenario.getLocation())), metadata);
pickles.add(pickle);
}
private void compileScenarioOutline(List pickles, List backgroundSteps,
ScenarioOutline scenarioOutline, List featureTags, String language, Map metadata) {
List scenariotags = new ArrayList<>();
scenariotags.addAll(featureTags);
scenariotags.addAll(scenarioOutline.getTags());
List examplesToUse = getExamples(metadata, scenarioOutline);
for (final Examples examples : examplesToUse) {
if (examples.getTableHeader() == null)
continue;
List variableCells = examples.getTableHeader().getCells();
for (final TableRow values : examples.getTableBody()) {
List valueCells = values.getCells();
List steps = new ArrayList<>();
if (!scenarioOutline.getSteps().isEmpty()) {
//steps.addAll(backgroundSteps);
steps.addAll(pickleSteps(backgroundSteps, variableCells, valueCells, pickleLocation(values.getLocation())));
}
steps.addAll(pickleSteps(scenarioOutline.getSteps(), variableCells, valueCells, pickleLocation(values.getLocation())));
/* for (Step scenarioOutlineStep : scenarioOutline.getSteps()) {
String stepText = interpolate(scenarioOutlineStep.getText(), variableCells, valueCells);
PickleStep pickleStep = new PickleStep(stepText,
createPickleArguments(scenarioOutlineStep.getArgument(), variableCells, valueCells),
asList(pickleLocation(values.getLocation()), pickleStepLocation(scenarioOutlineStep)));
steps.add(pickleStep);
}*/
List tags = new ArrayList<>(scenariotags);
tags.addAll(examples.getTags());
Pickle pickle = new Bdd2Pickle(interpolate(scenarioOutline.getName(), variableCells, valueCells),
language, steps, pickleTags(tags),
asList(pickleLocation(values.getLocation()), pickleLocation(scenarioOutline.getLocation())),
examples.getTableHeader().getCells(), valueCells, getMetaData(metadata, examples.getTags()));
pickles.add(pickle);
}
}
}
@SuppressWarnings("unchecked")
private List getExamples(Map metadata, ScenarioOutline scenarioOutline) {
if (null == metadata || !hasDP(metadata)) {
return scenarioOutline.getExamples();
}
List listToReturn = new ArrayList();
List