Search in sources :

Example 1 with SingleScenario

use of com.trivago.rta.vo.SingleScenario in project cucable-plugin by trivago.

the class RunnerFileContentRendererTest method getRenderedFeatureFileContentTest.

@Test
public void getRenderedFeatureFileContentTest() throws Exception {
    String template = "package parallel;\n" + "\n" + "import cucumber.api.CucumberOptions;\n" + "\n" + "@CucumberOptions(\n" + "    monochrome = false,\n" + "    features = {\"classpath:parallel/features/[FEATURE_FILE_NAME].feature\"},\n" + "    plugin = {\"json:target/cucumber-report/[FEATURE_FILE_NAME].json\"}\n" + ")\n" + "public class [FEATURE_FILE_NAME] {\n" + "}\n";
    when(fileIO.readContentFromFile(anyString())).thenReturn(template);
    String expectedOutput = "package parallel;\n" + "\n" + "import cucumber.api.CucumberOptions;\n" + "\n" + "@CucumberOptions(\n" + "    monochrome = false,\n" + "    features = {\"classpath:parallel/features/featureFileName.feature\"},\n" + "    plugin = {\"json:target/cucumber-report/featureFileName.json\"}\n" + ")\n" + "public class featureFileName {\n" + "}\n" + "\n" + "\n" + "// Source Feature: \n" + "// Generated by Cucable\n";
    SingleScenarioRunner singleScenarioRunner = new SingleScenarioRunner("pathToTemplate", "featureFileName");
    SingleScenario singleScenario = new SingleScenario("", "", "", "", "", "", null, null);
    String renderedRunnerFileContent = runnerFileContentRenderer.getRenderedRunnerFileContent(singleScenarioRunner, singleScenario);
    assertThat(renderedRunnerFileContent, is(expectedOutput));
}
Also used : SingleScenarioRunner(com.trivago.rta.vo.SingleScenarioRunner) SingleScenario(com.trivago.rta.vo.SingleScenario) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 2 with SingleScenario

use of com.trivago.rta.vo.SingleScenario in project cucable-plugin by trivago.

the class FeatureFileContentRendererTest method formatDocStringTest.

@Test
public void formatDocStringTest() {
    String expectedOutput = "Feature: featureName\n" + "\n" + "Scenario: scenarioName\n" + "\n" + "Step 1\n" + "\"\"\"\n" + "DOCSTRING LINE 1\n" + "DOCSTRING LINE 2\n" + "\"\"\"\n" + "\n# Source feature: TESTPATH\n" + "# Generated by Cucable\n";
    String featureName = "Feature: featureName";
    String scenarioName = "Scenario: scenarioName";
    List<Step> steps = Collections.singletonList(new Step("Step 1", null, "DOCSTRING LINE 1\nDOCSTRING LINE 2"));
    String featureFilePath = "TESTPATH";
    SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, null, null, scenarioName, null, new ArrayList<>(), new ArrayList<>());
    singleScenario.setSteps(steps);
    String renderedFeatureFileContent = featureFileContentRenderer.getRenderedFeatureFileContent(singleScenario);
    assertThat(renderedFeatureFileContent, is(expectedOutput));
}
Also used : SingleScenario(com.trivago.rta.vo.SingleScenario) Step(com.trivago.rta.vo.Step) Test(org.junit.Test)

Example 3 with SingleScenario

use of com.trivago.rta.vo.SingleScenario in project cucable-plugin by trivago.

the class GherkinDocumentParserTest method validFeatureWithScenarioOutlineTest.

@Test
public void validFeatureWithScenarioOutlineTest() throws Exception {
    String featureContent = "Feature: test feature 3\n" + "\n" + "  Scenario Outline: This is a scenario outline\n" + "    When I search for key <key>\n" + "    Then I see the value '<value>'\n" + "\n" + "    Examples:\n" + "      | key | value |\n" + "      | 1   | one   |\n" + "      | 2   | two   |";
    List<SingleScenario> singleScenariosFromFeature = gherkinDocumentParser.getSingleScenariosFromFeature(featureContent, "", null, null, null);
    assertThat(singleScenariosFromFeature.size(), is(2));
    SingleScenario scenario = singleScenariosFromFeature.get(0);
    assertThat(scenario.getScenarioName(), is("Scenario: This is a scenario outline"));
    assertThat(scenario.getSteps().size(), is(2));
    assertThat(scenario.getBackgroundSteps().size(), is(0));
    assertThat(scenario.getSteps().get(0).getDataTable(), is(nullValue()));
    assertThat(scenario.getSteps().get(0).getName(), is("When I search for key 1"));
    assertThat(scenario.getSteps().get(1).getName(), is("Then I see the value 'one'"));
    scenario = singleScenariosFromFeature.get(1);
    assertThat(scenario.getScenarioName(), is("Scenario: This is a scenario outline"));
    assertThat(scenario.getSteps().size(), is(2));
    assertThat(scenario.getBackgroundSteps().size(), is(0));
    assertThat(scenario.getSteps().get(0).getDataTable(), is(nullValue()));
    assertThat(scenario.getSteps().get(0).getName(), is("When I search for key 2"));
    assertThat(scenario.getSteps().get(1).getName(), is("Then I see the value 'two'"));
}
Also used : SingleScenario(com.trivago.rta.vo.SingleScenario) Test(org.junit.Test)

Example 4 with SingleScenario

use of com.trivago.rta.vo.SingleScenario in project cucable-plugin by trivago.

the class FeatureFileConverter method convertToSingleScenariosAndRunners.

/**
 * Converts all scenarios in the given feature file to single
 * scenario feature files and their respective runners.
 *
 * @param featureFilePath feature file to process.
 * @return Number of created scenarios.
 * @throws CucablePluginException see {@link CucablePluginException}
 */
private int convertToSingleScenariosAndRunners(final Path featureFilePath) throws CucablePluginException {
    String featureFilePathString = featureFilePath.toString();
    if (featureFilePathString == null || featureFilePathString.equals("")) {
        throw new MissingFileException(featureFilePathString);
    }
    List<Integer> lineNumbers = propertyManager.getScenarioLineNumbers();
    List<String> includeScenarioTags = propertyManager.getIncludeScenarioTags();
    List<String> excludeScenarioTags = propertyManager.getExcludeScenarioTags();
    String featureFileContent = fileIO.readContentFromFile(featureFilePathString);
    List<SingleScenario> singleScenarios;
    try {
        singleScenarios = gherkinDocumentParser.getSingleScenariosFromFeature(featureFileContent, featureFilePathString, lineNumbers, includeScenarioTags, excludeScenarioTags);
    } catch (CucablePluginException e) {
        throw new FeatureFileParseException(featureFilePathString);
    }
    // that means that the provided line number is wrong.
    if (propertyManager.hasValidScenarioLineNumbers() && singleScenarios.size() == 0) {
        throw new CucablePluginException("There is no parseable scenario or scenario outline at line " + lineNumbers);
    }
    for (SingleScenario singleScenario : singleScenarios) {
        String renderedFeatureFileContent = featureFileContentRenderer.getRenderedFeatureFileContent(singleScenario);
        String featureFileName = getFeatureFileNameFromPath(featureFilePath);
        Integer featureCounter = singleFeatureCounters.getOrDefault(featureFileName, 0);
        featureCounter++;
        String scenarioCounterFilenamePart = String.format(SCENARIO_COUNTER_FORMAT, featureCounter);
        for (int testRuns = 1; testRuns <= propertyManager.getNumberOfTestRuns(); testRuns++) {
            String testRunsCounterFilenamePart = String.format(TEST_RUNS_COUNTER_FORMAT, testRuns);
            // Append the scenario and test run counters to the filename.
            // Also add the "_IT" postfix so Maven Failsafe considers it an integration test automatically.
            String generatedFileName = featureFileName.concat(scenarioCounterFilenamePart).concat(testRunsCounterFilenamePart).concat(INTEGRATION_TEST_POSTFIX);
            String generatedFeatureFilePath = propertyManager.getGeneratedFeatureDirectory().concat(PATH_SEPARATOR).concat(generatedFileName).concat(FEATURE_FILE_EXTENSION);
            singleFeatureCounters.put(featureFileName, featureCounter);
            // Save scenario information to new feature file
            fileIO.writeContentToFile(renderedFeatureFileContent, generatedFeatureFilePath);
            // Generate runner for the newly generated single scenario feature file
            SingleScenarioRunner singleScenarioRunner = new SingleScenarioRunner(propertyManager.getSourceRunnerTemplateFile(), generatedFileName);
            String renderedRunnerFileContent = runnerFileContentRenderer.getRenderedRunnerFileContent(singleScenarioRunner, singleScenario);
            String generatedRunnerFilePath = propertyManager.getGeneratedRunnerDirectory().concat(PATH_SEPARATOR).concat(generatedFileName).concat(RUNNER_FILE_EXTENSION);
            fileIO.writeContentToFile(renderedRunnerFileContent, generatedRunnerFilePath);
        }
    }
    int createdScenarios = singleScenarios.size();
    logProcessCompleteMessage(featureFilePathString, createdScenarios);
    return createdScenarios;
}
Also used : SingleScenarioRunner(com.trivago.rta.vo.SingleScenarioRunner) SingleScenario(com.trivago.rta.vo.SingleScenario) FeatureFileParseException(com.trivago.rta.exceptions.filesystem.FeatureFileParseException) CucablePluginException(com.trivago.rta.exceptions.CucablePluginException) MissingFileException(com.trivago.rta.exceptions.filesystem.MissingFileException)

Example 5 with SingleScenario

use of com.trivago.rta.vo.SingleScenario in project cucable-plugin by trivago.

the class GherkinDocumentParser method getSingleScenariosFromOutline.

/**
 * Returns a list of Cucable single scenarios from a Gherkin scenario outline.
 *
 * @param scenarioOutline     A Gherkin {@link ScenarioOutline}.
 * @param featureName         The name of the feature this scenario outline belongs to.
 * @param featureFilePath     The source path of the feature file.
 * @param featureLanguage     The feature language this scenario outline belongs to.
 * @param featureTags         The feature tags of the parent feature.
 * @param backgroundSteps     Return a Cucable {@link SingleScenario} list.
 * @param includeScenarioTags Optional scenario tags to include in scenario generation.
 * @throws CucablePluginException Thrown when the scenario outline does not contain an example table.
 */
private List<SingleScenario> getSingleScenariosFromOutline(final ScenarioOutline scenarioOutline, final String featureName, final String featureFilePath, final String featureLanguage, final String featureDescription, final List<String> featureTags, final List<Step> backgroundSteps, final List<String> includeScenarioTags, final List<String> excludeScenarioTags) throws CucablePluginException {
    // Retrieve the translation of "Scenario" in the target language and add it to the scenario
    String translatedScenarioKeyword = gherkinTranslations.getScenarioKeyword(featureLanguage);
    String scenarioName = translatedScenarioKeyword + ": " + scenarioOutline.getName();
    String scenarioDescription = scenarioOutline.getDescription();
    List<String> scenarioTags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(scenarioOutline.getTags());
    if (!scenarioShouldBeIncluded(featureTags, scenarioTags, includeScenarioTags, excludeScenarioTags)) {
        return Collections.emptyList();
    }
    List<SingleScenario> outlineScenarios = new ArrayList<>();
    List<Step> steps = gherkinToCucableConverter.convertGherkinStepsToCucableSteps(scenarioOutline.getSteps());
    if (scenarioOutline.getExamples().isEmpty()) {
        throw new CucablePluginException("Scenario outline without examples table!");
    }
    Examples exampleTable = scenarioOutline.getExamples().get(0);
    Map<String, List<String>> exampleMap = gherkinToCucableConverter.convertGherkinExampleTableToCucableExampleMap(exampleTable);
    String firstColumnHeader = (String) exampleMap.keySet().toArray()[0];
    int rowCount = exampleMap.get(firstColumnHeader).size();
    // For each example row, create a new single scenario
    for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
        SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, featureLanguage, featureDescription, substituteScenarioNameExamplePlaceholders(scenarioName, exampleMap, rowIndex), scenarioDescription, featureTags, backgroundSteps);
        List<Step> substitutedSteps = substituteStepExamplePlaceholders(steps, exampleMap, rowIndex);
        singleScenario.setSteps(substitutedSteps);
        singleScenario.setScenarioTags(scenarioTags);
        outlineScenarios.add(singleScenario);
    }
    return outlineScenarios;
}
Also used : SingleScenario(com.trivago.rta.vo.SingleScenario) CucablePluginException(com.trivago.rta.exceptions.CucablePluginException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Step(com.trivago.rta.vo.Step) Examples(gherkin.ast.Examples)

Aggregations

SingleScenario (com.trivago.rta.vo.SingleScenario)13 Test (org.junit.Test)10 Step (com.trivago.rta.vo.Step)5 SingleScenarioRunner (com.trivago.rta.vo.SingleScenarioRunner)3 ArrayList (java.util.ArrayList)3 CucablePluginException (com.trivago.rta.exceptions.CucablePluginException)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 FeatureFileParseException (com.trivago.rta.exceptions.filesystem.FeatureFileParseException)1 MissingFileException (com.trivago.rta.exceptions.filesystem.MissingFileException)1 DataTable (com.trivago.rta.vo.DataTable)1 Background (gherkin.ast.Background)1 Examples (gherkin.ast.Examples)1 Feature (gherkin.ast.Feature)1 GherkinDocument (gherkin.ast.GherkinDocument)1 Scenario (gherkin.ast.Scenario)1 ScenarioDefinition (gherkin.ast.ScenarioDefinition)1 ScenarioOutline (gherkin.ast.ScenarioOutline)1 Path (java.nio.file.Path)1 List (java.util.List)1