Search in sources :

Example 1 with Feature

use of gherkin.ast.Feature in project cucable-plugin by trivago.

the class GherkinDocumentParser method getSingleScenariosFromFeature.

/**
 * Returns a {@link com.trivago.rta.vo.SingleScenario} list from a given feature file.
 *
 * @param featureContent      A feature string.
 * @param featureFilePath     The path to the source feature file.
 * @param scenarioLineNumbers An optional line number of a scenario inside a feature file.
 * @param includeScenarioTags Optional scenario tags to include into scenario generation.
 * @param excludeScenarioTags Optional scenario tags to exclude from scenario generation.
 * @return A {@link com.trivago.rta.vo.SingleScenario} list.
 * @throws CucablePluginException see {@link CucablePluginException}.
 */
public List<SingleScenario> getSingleScenariosFromFeature(final String featureContent, final String featureFilePath, final List<Integer> scenarioLineNumbers, final List<String> includeScenarioTags, final List<String> excludeScenarioTags) throws CucablePluginException {
    GherkinDocument gherkinDocument = getGherkinDocumentFromFeatureFileContent(featureContent);
    Feature feature = gherkinDocument.getFeature();
    String featureName = feature.getKeyword() + ": " + feature.getName();
    String featureLanguage = feature.getLanguage();
    String featureDescription = feature.getDescription();
    List<String> featureTags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(feature.getTags());
    ArrayList<SingleScenario> singleScenarioFeatures = new ArrayList<>();
    List<Step> backgroundSteps = new ArrayList<>();
    List<ScenarioDefinition> scenarioDefinitions = feature.getChildren();
    for (ScenarioDefinition scenarioDefinition : scenarioDefinitions) {
        String scenarioName = scenarioDefinition.getKeyword() + ": " + scenarioDefinition.getName();
        String scenarioDescription = scenarioDefinition.getDescription();
        if (scenarioDefinition instanceof Background) {
            // Save background steps in order to add them to every scenario inside the same feature
            Background background = (Background) scenarioDefinition;
            backgroundSteps = gherkinToCucableConverter.convertGherkinStepsToCucableSteps(background.getSteps());
            continue;
        }
        if (scenarioDefinition instanceof Scenario) {
            Scenario scenario = (Scenario) scenarioDefinition;
            if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenario.getLocation().getLine())) {
                SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, featureLanguage, featureDescription, scenarioName, scenarioDescription, featureTags, backgroundSteps);
                addGherkinScenarioInformationToSingleScenario(scenario, singleScenario);
                if (scenarioShouldBeIncluded(singleScenario.getScenarioTags(), singleScenario.getFeatureTags(), includeScenarioTags, excludeScenarioTags)) {
                    singleScenarioFeatures.add(singleScenario);
                }
            }
            continue;
        }
        if (scenarioDefinition instanceof ScenarioOutline) {
            ScenarioOutline scenarioOutline = (ScenarioOutline) scenarioDefinition;
            if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenarioOutline.getLocation().getLine())) {
                List<SingleScenario> outlineScenarios = getSingleScenariosFromOutline(scenarioOutline, featureName, featureFilePath, featureLanguage, featureDescription, featureTags, backgroundSteps, includeScenarioTags, excludeScenarioTags);
                singleScenarioFeatures.addAll(outlineScenarios);
            }
        }
    }
    return singleScenarioFeatures;
}
Also used : SingleScenario(com.trivago.rta.vo.SingleScenario) Background(gherkin.ast.Background) ArrayList(java.util.ArrayList) GherkinDocument(gherkin.ast.GherkinDocument) Step(com.trivago.rta.vo.Step) Feature(gherkin.ast.Feature) SingleScenario(com.trivago.rta.vo.SingleScenario) Scenario(gherkin.ast.Scenario) ScenarioOutline(gherkin.ast.ScenarioOutline) ScenarioDefinition(gherkin.ast.ScenarioDefinition)

Example 2 with Feature

use of gherkin.ast.Feature in project page-factory-2 by sbtqa.

the class GherkinSerializer method reserializeFeatures.

/**
 * As soon as fragments are inserted, all features structure will unaligned by its file locations.
 * So we need to realign it by reserializing features sources.
 *
 * @param cucumberFeatures unaligned features
 * @return reserealized features
 */
public List<CucumberFeature> reserializeFeatures(List<CucumberFeature> cucumberFeatures) {
    FeatureBuilder featureBuilder = new FeatureBuilder();
    cucumberFeatures.forEach(cucumberFeature -> {
        builder = new StringBuilder();
        Feature feature = cucumberFeature.getGherkinFeature().getFeature();
        builder.append("#language: ").append(feature.getLanguage());
        nl(1);
        if (!feature.getTags().isEmpty()) {
            feature.getTags().forEach(tag -> builder.append(tag.getName()).append(SPACE));
            nl(1);
        }
        builder.append(feature.getKeyword()).append(":").append(SPACE).append(feature.getName());
        nl(1);
        if (feature.getDescription() != null) {
            tab(1);
            builder.append(feature.getDescription());
            nl(1);
        }
        nl(1);
        feature.getChildren().forEach(scenarioDefinition -> buildScenario(builder, scenarioDefinition));
        Resource gherkinResource = new GherkinResource(builder.toString(), cucumberFeature.getUri());
        featureBuilder.parse(gherkinResource);
    });
    return featureBuilder.build();
}
Also used : FeatureBuilder(cucumber.runtime.model.FeatureBuilder) Resource(cucumber.runtime.io.Resource) CucumberFeature(cucumber.runtime.model.CucumberFeature) Feature(gherkin.ast.Feature)

Example 3 with Feature

use of gherkin.ast.Feature in project cucable-plugin by trivago.

the class GherkinDocumentParser method getSingleScenariosFromFeature.

/**
 * Returns a {@link SingleScenario} list from a given feature file.
 *
 * @param featureContent  A feature string.
 * @param featureFilePath The path to the source feature file.
 * @return A {@link SingleScenario} list.
 * @throws CucablePluginException see {@link CucablePluginException}.
 */
public List<SingleScenario> getSingleScenariosFromFeature(final String featureContent, final String featureFilePath, final List<Integer> scenarioLineNumbers) throws CucablePluginException {
    String escapedFeatureContent = featureContent.replace("\\n", "\\\\n");
    GherkinDocument gherkinDocument;
    try {
        gherkinDocument = getGherkinDocumentFromFeatureFileContent(escapedFeatureContent);
    } catch (CucablePluginException e) {
        throw new FeatureFileParseException(featureFilePath, e.getMessage());
    }
    Feature feature = gherkinDocument.getFeature();
    if (feature == null) {
        return Collections.emptyList();
    }
    String featureName = feature.getKeyword() + ": " + feature.getName();
    String featureLanguage = feature.getLanguage();
    String featureDescription = feature.getDescription();
    List<String> featureTags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(feature.getTags());
    ArrayList<SingleScenario> singleScenarioFeatures = new ArrayList<>();
    List<Step> backgroundSteps = new ArrayList<>();
    List<ScenarioDefinition> scenarioDefinitions = feature.getChildren();
    for (ScenarioDefinition scenarioDefinition : scenarioDefinitions) {
        String scenarioName = scenarioDefinition.getKeyword() + ": " + scenarioDefinition.getName();
        String scenarioDescription = scenarioDefinition.getDescription();
        if (scenarioDefinition instanceof Background) {
            // Save background steps in order to add them to every scenario inside the same feature
            Background background = (Background) scenarioDefinition;
            backgroundSteps = gherkinToCucableConverter.convertGherkinStepsToCucableSteps(background.getSteps());
            continue;
        }
        if (scenarioDefinition instanceof Scenario) {
            Scenario scenario = (Scenario) scenarioDefinition;
            if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenario.getLocation().getLine())) {
                SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, featureLanguage, featureDescription, scenarioName, scenarioDescription, featureTags, backgroundSteps);
                addGherkinScenarioInformationToSingleScenario(scenario, singleScenario);
                if (scenarioShouldBeIncluded(singleScenario)) {
                    singleScenarioFeatures.add(singleScenario);
                }
            }
            continue;
        }
        if (scenarioDefinition instanceof ScenarioOutline) {
            ScenarioOutline scenarioOutline = (ScenarioOutline) scenarioDefinition;
            if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenarioOutline.getLocation().getLine())) {
                List<SingleScenario> outlineScenarios = getSingleScenariosFromOutline(scenarioOutline, featureName, featureFilePath, featureLanguage, featureDescription, featureTags, backgroundSteps);
                for (SingleScenario singleScenario : outlineScenarios) {
                    if (scenarioShouldBeIncluded(singleScenario)) {
                        singleScenarioFeatures.add(singleScenario);
                    }
                }
            }
        }
    }
    return singleScenarioFeatures;
}
Also used : SingleScenario(com.trivago.vo.SingleScenario) Background(gherkin.ast.Background) CucablePluginException(com.trivago.exceptions.CucablePluginException) ArrayList(java.util.ArrayList) GherkinDocument(gherkin.ast.GherkinDocument) Step(com.trivago.vo.Step) Feature(gherkin.ast.Feature) Scenario(gherkin.ast.Scenario) SingleScenario(com.trivago.vo.SingleScenario) FeatureFileParseException(com.trivago.exceptions.filesystem.FeatureFileParseException) ScenarioOutline(gherkin.ast.ScenarioOutline) ScenarioDefinition(gherkin.ast.ScenarioDefinition)

Aggregations

Feature (gherkin.ast.Feature)3 Background (gherkin.ast.Background)2 GherkinDocument (gherkin.ast.GherkinDocument)2 Scenario (gherkin.ast.Scenario)2 ScenarioDefinition (gherkin.ast.ScenarioDefinition)2 ScenarioOutline (gherkin.ast.ScenarioOutline)2 ArrayList (java.util.ArrayList)2 CucablePluginException (com.trivago.exceptions.CucablePluginException)1 FeatureFileParseException (com.trivago.exceptions.filesystem.FeatureFileParseException)1 SingleScenario (com.trivago.rta.vo.SingleScenario)1 Step (com.trivago.rta.vo.Step)1 SingleScenario (com.trivago.vo.SingleScenario)1 Step (com.trivago.vo.Step)1 Resource (cucumber.runtime.io.Resource)1 CucumberFeature (cucumber.runtime.model.CucumberFeature)1 FeatureBuilder (cucumber.runtime.model.FeatureBuilder)1