gherkin.ast.ScenarioDefinition Java Examples

The following examples show how to use gherkin.ast.ScenarioDefinition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private void parseGherkinSource(final String path) {
    if (!pathToReadEventMap.containsKey(path)) {
        return;
    }
    final Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
    final TokenMatcher matcher = new TokenMatcher();
    try {
        final GherkinDocument gherkinDocument = parser.parse(pathToReadEventMap.get(path).source, matcher);
        pathToAstMap.put(path, gherkinDocument);
        final Map<Integer, AstNode> nodeMap = new HashMap<>();
        final AstNode currentParent = new AstNode(gherkinDocument.getFeature(), null);
        for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) {
            processScenarioDefinition(nodeMap, child, currentParent);
        }
        pathToNodeMap.put(path, nodeMap);
    } catch (ParserException e) {
        LOGGER.trace(e.getMessage(), e);
    }
}
 
Example #2
Source File: BddParser.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
private void createTestCases(Scenario scenario, Feature feature) {
    for (ScenarioDefinition scenarioDef : feature.getChildren()) {
        String scenDefName = scenarioDef.getName();
        TestCase testCase = updateInfo(createTestCase(scenario, scenDefName), scenarioDef);
        testCase.clearSteps();
        for (Step step : scenarioDef.getSteps()) {
            String reusableName = convert(step.getText());
            TestCase reusable = updateInfo(createReusable(create("StepDefinitions"), reusableName), step);
            if (reusable != null) {
                reusable.addNewStep()
                        .setInput(getInputField(testCase.getName(), step.getText()))
                        .setDescription(getDescription(step));
            }
            testCase.addNewStep()
                    .asReusableStep("StepDefinitions", reusableName)
                    .setDescription(getDescription(step));
        }
        if (scenarioDef instanceof ScenarioOutline) {
            ScenarioOutline scOutline = (ScenarioOutline) scenarioDef;
            createTestData(testCase, scOutline.getExamples());
        }
    }
}
 
Example #3
Source File: TestSourcesModel.java    From extentreports-cucumber4-adapter with Apache License 2.0 6 votes vote down vote up
static String calculateId(AstNode astNode) {
    Node node = astNode.node;
    if (node instanceof ScenarioDefinition) {
        return calculateId(astNode.parent) + ";" + convertToId(((ScenarioDefinition) node).getName());
    }
    if (node instanceof ExamplesRowWrapperNode) {
        return calculateId(astNode.parent) + ";" + Integer.toString(((ExamplesRowWrapperNode) node).bodyRowIndex + 2);
    }
    if (node instanceof TableRow) {
        return calculateId(astNode.parent) + ";" + Integer.toString(1);
    }
    if (node instanceof Examples) {
        return calculateId(astNode.parent) + ";" + convertToId(((Examples) node).getName());
    }
    if (node instanceof Feature) {
        return convertToId(((Feature) node).getName());
    }
    return "";
}
 
Example #4
Source File: TestSourcesModel.java    From extentreports-cucumber4-adapter with Apache License 2.0 6 votes vote down vote up
private void parseGherkinSource(String path) {
    if (!pathToReadEventMap.containsKey(path)) {
        return;
    }
    Parser<GherkinDocument> parser = new Parser<GherkinDocument>(new AstBuilder());
    TokenMatcher matcher = new TokenMatcher();
    try {
        GherkinDocument gherkinDocument = parser.parse(pathToReadEventMap.get(path).source, matcher);
        pathToAstMap.put(path, gherkinDocument);
        Map<Integer, AstNode> nodeMap = new HashMap<Integer, AstNode>();
        AstNode currentParent = new AstNode(gherkinDocument.getFeature(), null);
        for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) {
            processScenarioDefinition(nodeMap, child, currentParent);
        }
        pathToNodeMap.put(path, nodeMap);
    } catch (ParserException e) {
        // Ignore exceptions
    }
}
 
Example #5
Source File: TestSourcesModel.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private void parseGherkinSource(final URI path) {
    if (!pathToReadEventMap.containsKey(path)) {
        return;
    }
    final Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
    final TokenMatcher matcher = new TokenMatcher();
    try {
        final GherkinDocument gherkinDocument = parser.parse(pathToReadEventMap.get(path).getSource(),
                matcher);
        pathToAstMap.put(path, gherkinDocument);
        final Map<Integer, AstNode> nodeMap = new HashMap<>();
        final AstNode currentParent = new AstNode(gherkinDocument.getFeature(), null);
        for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) {
            processScenarioDefinition(nodeMap, child, currentParent);
        }
        pathToNodeMap.put(path, nodeMap);
    } catch (ParserException e) {
        throw new IllegalStateException("You are using a plugin that only supports till Gherkin 5.\n"
                + "Please check if the Gherkin provided follows the standard of Gherkin 5\n", e
        );
    }
}
 
Example #6
Source File: BddParser.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
private TestCase updateInfo(TestCase tc, ScenarioDefinition sdef) {
    if (tc != null) {
        DataItem tcInfo = pModel().getData().find(tc.getName(), tc.getScenario().getName())
                .orElseGet(() -> {
                    DataItem di = DataItem.createTestCase(tc.getName(), tc.getScenario().getName());
                    pModel().addData(di);
                    return di;
                });
        List<gherkin.ast.Tag> tags = getTags(sdef);
        if (tags != null) {
            tcInfo.setTags(toTag(tags));
        }
        tcInfo.getAttributes().update(Attribute.create("feature.children.line", sdef.getLocation().getLine()));
        tcInfo.getAttributes().update(Attribute.create("feature.children.name", sdef.getName()));
        tcInfo.getAttributes().update(Attribute.create("description", sdef.getDescription()));
        tcInfo.getAttributes().update(Attribute.create("feature.children.keyword", sdef.getKeyword()));
        pModel().addData(tcInfo);
    }
    return tc;
}
 
Example #7
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private void parseGherkinSource(final String path) {
    if (!pathToReadEventMap.containsKey(path)) {
        return;
    }
    final Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
    final TokenMatcher matcher = new TokenMatcher();
    try {
        final GherkinDocument gherkinDocument = parser.parse(pathToReadEventMap.get(path).source, matcher);
        pathToAstMap.put(path, gherkinDocument);
        final Map<Integer, AstNode> nodeMap = new HashMap<>();
        final AstNode currentParent = new AstNode(gherkinDocument.getFeature(), null);
        for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) {
            processScenarioDefinition(nodeMap, child, currentParent);
        }
        pathToNodeMap.put(path, nodeMap);
    } catch (ParserException e) {
        LOGGER.trace(e.getMessage(), e);
    }
}
 
Example #8
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 5 votes vote down vote up
private void processScenarioDefinition(
        final Map<Integer, AstNode> nodeMap, final ScenarioDefinition child, final AstNode currentParent
) {
    final AstNode childNode = new AstNode(child, currentParent);
    nodeMap.put(child.getLocation().getLine(), childNode);

    child.getSteps().forEach(
        step -> nodeMap.put(step.getLocation().getLine(), new AstNode(step, childNode))
    );

    if (child instanceof ScenarioOutline) {
        processScenarioOutlineExamples(nodeMap, (ScenarioOutline) child, childNode);
    }
}
 
Example #9
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 5 votes vote down vote up
private void processScenarioDefinition(
        final Map<Integer, AstNode> nodeMap, final ScenarioDefinition child, final AstNode currentParent
) {
    final AstNode childNode = new AstNode(child, currentParent);
    nodeMap.put(child.getLocation().getLine(), childNode);

    child.getSteps().forEach(
        step -> nodeMap.put(step.getLocation().getLine(), new AstNode(step, childNode))
    );

    if (child instanceof ScenarioOutline) {
        processScenarioOutlineExamples(nodeMap, (ScenarioOutline) child, childNode);
    }
}
 
Example #10
Source File: BddParser.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
private List getTags(ScenarioDefinition sdef) {
    if (sdef instanceof ScenarioOutline) {
        return ((ScenarioOutline) sdef).getTags();
    } else if (sdef instanceof gherkin.ast.Scenario) {
        return ((gherkin.ast.Scenario) sdef).getTags();
    } else {
        return null;
    }
}
 
Example #11
Source File: TestSourcesModel.java    From allure-java with Apache License 2.0 5 votes vote down vote up
private void processScenarioDefinition(final Map<Integer, AstNode> nodeMap, final ScenarioDefinition child,
                                       final AstNode currentParent) {
    final AstNode childNode = new AstNode(child, currentParent);
    nodeMap.put(child.getLocation().getLine(), childNode);
    for (Step step : child.getSteps()) {
        nodeMap.put(step.getLocation().getLine(), createAstNode(step, childNode));
    }
    if (child instanceof ScenarioOutline) {
        processScenarioOutlineExamples(nodeMap, (ScenarioOutline) child, childNode);
    }
}
 
Example #12
Source File: TestCommand.java    From rug-cli with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void scenarioCompleted(ScenarioDefinition sd, ScenarioResult sr) {
    String duration = String.format("%.2f", scenarioTimer.duration());
    String msg = "  Completed test scenario " + sd.getName() + " "
            + (sr.passed() ? Style.green("passed") : Style.red("failed"));
    if (CommandLineOptions.hasOption("timer")) {
        msg = msg + " in " + duration + "s";
    }
    reporter.report(msg);
}
 
Example #13
Source File: ExtentCucumberAdapter.java    From extentreports-cucumber4-adapter with Apache License 2.0 5 votes vote down vote up
private synchronized void createTestCase(TestCase testCase) {
    TestSourcesModel.AstNode astNode = testSources.getAstNode(currentFeatureFile.get(), testCase.getLine());
    if (astNode != null) {
        ScenarioDefinition scenarioDefinition = TestSourcesModel.getScenarioDefinition(astNode);
        ExtentTest parent = scenarioOutlineThreadLocal.get() != null ? scenarioOutlineThreadLocal.get() : featureTestThreadLocal.get();
        ExtentTest t = parent.createNode(com.aventstack.extentreports.gherkin.model.Scenario.class, scenarioDefinition.getName(), scenarioDefinition.getDescription());
        scenarioThreadLocal.set(t);
    }
    if (!testCase.getTags().isEmpty()) {
        testCase.getTags()
 		.stream()
 		.map(PickleTag::getName)
 		.forEach(scenarioThreadLocal.get()::assignCategory);
    }
}
 
Example #14
Source File: TestSourcesModel.java    From extentreports-cucumber4-adapter with Apache License 2.0 5 votes vote down vote up
private void processScenarioDefinition(Map<Integer, AstNode> nodeMap, ScenarioDefinition child, AstNode currentParent) {
    AstNode childNode = new AstNode(child, currentParent);
    nodeMap.put(child.getLocation().getLine(), childNode);
    for (Step step : child.getSteps()) {
        nodeMap.put(step.getLocation().getLine(), new AstNode(step, childNode));
    }
    if (child instanceof ScenarioOutline) {
        processScenarioOutlineExamples(nodeMap, (ScenarioOutline) child, childNode);
    }
}
 
Example #15
Source File: CucumberITGeneratorByScenario.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 5 votes vote down vote up
private ScenarioDefinition findScenarioDefinitionViaLocation(Location location, GherkinDocument gherkinDocument) {
    List<ScenarioDefinition> scenarioDefinitions = gherkinDocument.getFeature().getChildren();
    for (ScenarioDefinition definition : scenarioDefinitions) {
        if (isLocationSame(definition.getLocation(), location)) {
            return definition;
        }
    }
    return null;
}
 
Example #16
Source File: TestSourcesModel.java    From extentreports-cucumber4-adapter with Apache License 2.0 5 votes vote down vote up
static Background getBackgroundForTestCase(AstNode astNode) {
    Feature feature = getFeatureForTestCase(astNode);
    ScenarioDefinition backgound = feature.getChildren().get(0);
    if (backgound instanceof Background) {
        return (Background) backgound;
    } else {
        return null;
    }
}
 
Example #17
Source File: CucumberITGeneratorByScenario.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 4 votes vote down vote up
private void setParsedScenario(final ScenarioDefinition scenario) {
    parsedScenario = scenario;
}
 
Example #18
Source File: TestCommand.java    From rug-cli with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void scenarioStarting(ScenarioDefinition sd) {
    scenarioTimer = new Timing();
    reporter.report("  Running test scenario " + sd.getName());
}
 
Example #19
Source File: ScenarioAndLocation.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 4 votes vote down vote up
public ScenarioAndLocation(ScenarioDefinition scenarioDefinition, Location location) {
    this.scenarioDefinition = scenarioDefinition;
    this.location = location;
}
 
Example #20
Source File: ScenarioAndLocation.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 4 votes vote down vote up
public ScenarioDefinition getScenario() {
    return scenarioDefinition;
}
 
Example #21
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 4 votes vote down vote up
private ScenarioDefinition getScenarioDefinition(final AstNode astNode) {
    return astNode.getNode() instanceof ScenarioDefinition
            ? (ScenarioDefinition) astNode.getNode()
            : (ScenarioDefinition) astNode.getParent().getParent().getNode();
}
 
Example #22
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 4 votes vote down vote up
public ScenarioDefinition getScenarioDefinition(final String path, final int line) {
    return getScenarioDefinition(getAstNode(path, line));
}
 
Example #23
Source File: CucumberRunner.java    From cucumber-performance with MIT License 4 votes vote down vote up
/**
 * @return List of detected cucumber scenarios
 */
public List<List<ScenarioDefinition>> getScenarios() {
	return FeatureBuilder.GetScenarios(this.getFeatures());
}
 
Example #24
Source File: AllureCucumber3Jvm.java    From allure-java with Apache License 2.0 4 votes vote down vote up
private void handleTestCaseStarted(final TestCaseStarted event) {
    currentTestCase = event.testCase;
    currentFeatureFile = currentTestCase.getUri();
    currentFeature = cucumberSourceUtils.getFeature(currentFeatureFile);
    currentContainer = UUID.randomUUID().toString();
    forbidTestCaseStatusChange = false;


    final Deque<PickleTag> tags = new LinkedList<>(currentTestCase.getTags());

    final LabelBuilder labelBuilder = new LabelBuilder(currentFeature, currentTestCase, tags);

    final String name = currentTestCase.getName();
    final String featureName = currentFeature.getName();

    final TestResult result = new TestResult()
            .setUuid(getTestCaseUuid(currentTestCase))
            .setHistoryId(getHistoryId(currentTestCase))
            .setFullName(featureName + ": " + name)
            .setName(name)
            .setLabels(labelBuilder.getScenarioLabels())
            .setLinks(labelBuilder.getScenarioLinks());

    final ScenarioDefinition scenarioDefinition =
            cucumberSourceUtils.getScenarioDefinition(currentFeatureFile, currentTestCase.getLine());
    if (scenarioDefinition instanceof ScenarioOutline) {
        result.setParameters(
                getExamplesAsParameters((ScenarioOutline) scenarioDefinition)
        );
    }

    if (currentFeature.getDescription() != null && !currentFeature.getDescription().isEmpty()) {
        result.setDescription(currentFeature.getDescription());
    }

    final TestResultContainer resultContainer = new TestResultContainer()
            .setName(String.format("%s: %s", scenarioDefinition.getKeyword(), scenarioDefinition.getName()))
            .setUuid(getTestContainerUuid())
            .setChildren(Collections.singletonList(getTestCaseUuid(currentTestCase)));

    lifecycle.scheduleTestCase(result);
    lifecycle.startTestContainer(getTestContainerUuid(), resultContainer);
    lifecycle.startTestCase(getTestCaseUuid(currentTestCase));
}
 
Example #25
Source File: AllureCucumber4Jvm.java    From allure-java with Apache License 2.0 4 votes vote down vote up
private void handleTestCaseStarted(final TestCaseStarted event) {
    currentFeatureFile.set(event.testCase.getUri());
    currentFeature.set(testSources.getFeature(currentFeatureFile.get()));
    currentTestCase.set(event.testCase);
    currentContainer.set(UUID.randomUUID().toString());
    forbidTestCaseStatusChange.set(false);

    final Deque<PickleTag> tags = new LinkedList<>(currentTestCase.get().getTags());

    final Feature feature = currentFeature.get();
    final LabelBuilder labelBuilder = new LabelBuilder(feature, currentTestCase.get(), tags);

    final String name = currentTestCase.get().getName();
    final String featureName = feature.getName();

    final TestResult result = new TestResult()
            .setUuid(getTestCaseUuid(currentTestCase.get()))
            .setHistoryId(getHistoryId(currentTestCase.get()))
            .setFullName(featureName + ": " + name)
            .setName(name)
            .setLabels(labelBuilder.getScenarioLabels())
            .setLinks(labelBuilder.getScenarioLinks());

    final ScenarioDefinition scenarioDefinition =
            testSources.getScenarioDefinition(currentFeatureFile.get(), currentTestCase.get().getLine());
    if (scenarioDefinition instanceof ScenarioOutline) {
        result.setParameters(
                getExamplesAsParameters((ScenarioOutline) scenarioDefinition, currentTestCase.get())
        );
    }

    final String description = Stream.of(feature.getDescription(), scenarioDefinition.getDescription())
            .filter(Objects::nonNull)
            .filter(s -> !s.isEmpty())
            .collect(Collectors.joining("\n"));

    if (!description.isEmpty()) {
        result.setDescription(description);
    }

    final TestResultContainer resultContainer = new TestResultContainer()
            .setName(String.format("%s: %s", scenarioDefinition.getKeyword(), scenarioDefinition.getName()))
            .setUuid(getTestContainerUuid())
            .setChildren(Collections.singletonList(getTestCaseUuid(currentTestCase.get())));

    lifecycle.scheduleTestCase(result);
    lifecycle.startTestContainer(getTestContainerUuid(), resultContainer);
    lifecycle.startTestCase(getTestCaseUuid(currentTestCase.get()));
}
 
Example #26
Source File: TestSourcesModelProxy.java    From allure-java with Apache License 2.0 4 votes vote down vote up
public ScenarioDefinition getScenarioDefinition(final String path, final int line) {
    return testSources.getScenarioDefinition(path, line);
}
 
Example #27
Source File: TestSourcesModelProxy.java    From allure-java with Apache License 2.0 4 votes vote down vote up
public ScenarioDefinition getScenarioDefinition(final URI path, final int line) {
    return testSources.getScenarioDefinition(testSources.getAstNode(path, line));
}
 
Example #28
Source File: TestSourcesModel.java    From allure-java with Apache License 2.0 4 votes vote down vote up
public static ScenarioDefinition getScenarioDefinition(final AstNode astNode) {
    return astNode.node instanceof ScenarioDefinition ? (ScenarioDefinition) astNode.node
            : (ScenarioDefinition) astNode.parent.parent.node;
}
 
Example #29
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 4 votes vote down vote up
private ScenarioDefinition getScenarioDefinition(final AstNode astNode) {
    return astNode.getNode() instanceof ScenarioDefinition
            ? (ScenarioDefinition) astNode.getNode()
            : (ScenarioDefinition) astNode.getParent().getParent().getNode();
}
 
Example #30
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 4 votes vote down vote up
public ScenarioDefinition getScenarioDefinition(final String path, final int line) {
    return getScenarioDefinition(getAstNode(path, line));
}