Search in sources :

Example 1 with FragmentException

use of ru.sbtqa.tag.pagefactory.exceptions.FragmentException in project page-factory-2 by sbtqa.

the class FragmentUtils method getFragmentName.

/**
 * Find the name of the scenario (fragment) to substitute for this step
 *
 * @param step step to substitute
 * @param language step's language
 * @return name of the scenario (fragment) to substitute
 */
static String getFragmentName(Step step, String language) throws FragmentException {
    Map<String, String> props = getFragmentStepRegex(language);
    for (Map.Entry<String, String> entry : props.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (key.startsWith(FRAGMENT_STEP_REGEX_KEY)) {
            Pattern pattern = Pattern.compile(value);
            Matcher matcher = pattern.matcher(step.getText());
            if (matcher.find()) {
                return matcher.group(1);
            }
        }
    }
    throw new FragmentException("Fragment name not found");
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FragmentException(ru.sbtqa.tag.pagefactory.exceptions.FragmentException) Map(java.util.Map)

Example 2 with FragmentException

use of ru.sbtqa.tag.pagefactory.exceptions.FragmentException in project page-factory-2 by sbtqa.

the class FragmentReplacer method replace.

/**
 * In the graph, terminal fragments are searched and substituted cyclically.
 * The cycle will be completed when all fragments are substituted and there are no edges left in the graph.
 *
 * @throws IllegalAccessException if it was not possible to replace a step with a fragment
 * @throws FragmentException if fragments replacing is in an infinite loop
 */
public void replace() throws IllegalAccessException, FragmentException, DataException {
    while (!fragmentsGraph.edges().isEmpty()) {
        int fragmentsGraphSize = fragmentsGraph.edges().size();
        for (EndpointPair edge : new ArrayList<>(fragmentsGraph.edges())) {
            ScenarioDefinition fragment = (ScenarioDefinition) edge.nodeV();
            ScenarioDefinition scenario = (ScenarioDefinition) edge.nodeU();
            String data = fragmentsGraph.edgeValue(edge.nodeU(), edge.nodeV()).get();
            if (isTerminal(fragment)) {
                replaceFragmentInScenario(scenario, fragment, data);
                fragmentsGraph.removeEdge(scenario, fragment);
            }
        }
        if (fragmentsGraphSize == fragmentsGraph.edges().size()) {
            throw new FragmentException("Fragments replacing is no longer performed, it will lead to an infinite loop. Interrupting...");
        }
    }
}
Also used : ScenarioDefinition(gherkin.ast.ScenarioDefinition) ArrayList(java.util.ArrayList) EndpointPair(com.google.common.graph.EndpointPair) FragmentException(ru.sbtqa.tag.pagefactory.exceptions.FragmentException)

Aggregations

FragmentException (ru.sbtqa.tag.pagefactory.exceptions.FragmentException)2 EndpointPair (com.google.common.graph.EndpointPair)1 ScenarioDefinition (gherkin.ast.ScenarioDefinition)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1