From 3b19a229e5615f560256a08ba74460d46cb80f2a Mon Sep 17 00:00:00 2001 From: Daria Yershova Date: Mon, 26 Aug 2024 19:34:49 -0500 Subject: [PATCH] #187: Scenario outlines do not support dynamic parameters in the scenario name --- pom.xml | 6 +-- .../github/invictum/reportportal/Utils.java | 28 +++++++++++++ .../reportportal/recorder/BddDataDriven.java | 9 ++-- .../UtilsReplacePlaceholdersTest.java | 42 +++++++++++++++++++ 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/github/invictum/reportportal/UtilsReplacePlaceholdersTest.java diff --git a/pom.xml b/pom.xml index f1d2530..2bd74c6 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.invictum serenity-reportportal-integration - 1.6.0-SNAPSHOT + 1.6.2-SNAPSHOT jar @@ -39,8 +39,8 @@ UTF-8 UTF-8 4.0.21 - 5.1.22 - 5.8.1 + 5.2.14 + 5.11.1 diff --git a/src/main/java/com/github/invictum/reportportal/Utils.java b/src/main/java/com/github/invictum/reportportal/Utils.java index f6c9ee8..e6f104b 100644 --- a/src/main/java/com/github/invictum/reportportal/Utils.java +++ b/src/main/java/com/github/invictum/reportportal/Utils.java @@ -7,6 +7,9 @@ import java.time.Duration; import java.time.ZonedDateTime; import java.util.Date; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Utils { @@ -54,4 +57,29 @@ public static Date testEndDate(TestOutcome test) { public static Date stepStartDate(TestStep step) { return Date.from(step.getStartTime().toInstant()); } + + /** + * Replaces entries of parameters in string with its values, + * e.g. "Add two numbers & " => "Add two numbers -2 & 3". + * + * @return modified string + */ + public static String replacePlaceholders(String originalString, List replacements) { + // Regular expression to match content within < > + final String parameterRegexp = "<([^>]+)>"; + final Pattern pattern = Pattern.compile(parameterRegexp); + final Matcher matcher = pattern.matcher(originalString); + final StringBuilder result = new StringBuilder(); + int index = 0; + // Iterate through the matches and replace with values from the ArrayList + while (matcher.find()) { + if (index < replacements.size()) { + matcher.appendReplacement(result, replacements.get(index)); + index++; + } + } + // Append the rest of the string + matcher.appendTail(result); + return result.toString(); + } } diff --git a/src/main/java/com/github/invictum/reportportal/recorder/BddDataDriven.java b/src/main/java/com/github/invictum/reportportal/recorder/BddDataDriven.java index 1810c42..cb6057f 100644 --- a/src/main/java/com/github/invictum/reportportal/recorder/BddDataDriven.java +++ b/src/main/java/com/github/invictum/reportportal/recorder/BddDataDriven.java @@ -6,10 +6,11 @@ import com.github.invictum.reportportal.*; import com.google.inject.Inject; import io.reactivex.Maybe; +import net.thucydides.model.domain.DataTableRow; import net.thucydides.model.domain.TestOutcome; import net.thucydides.model.domain.TestStep; -import java.util.Arrays; +import java.util.List; /** * Recorder aware of parameterized BDD style test specific handling @@ -32,16 +33,18 @@ public void record(TestOutcome out) { .withDescription(out.getUserStory().getNarrative()) .build(); Maybe id = suiteStorage.start(out.getUserStory().getId(), () -> launch.startTestItem(startStory)); + final List dataRows = out.getDataTable().getRows(); + final List currentTestParams = dataRows.get(last).getStringValues(); // Start test StartTestItemRQ startScenario = new StartEventBuilder(ItemType.STEP) - .withName(out.getName()) + .withName(Utils.replacePlaceholders(out.getName(), currentTestParams)) .withStartTime(currentTest.getStartTime()) .withParameters(out.getDataTable().row(last)) .withTags(out.getTags()) .build(); Maybe testId = launch.startTestItem(id, startScenario); // Steps - proceedSteps(testId, Arrays.asList(currentTest)); + proceedSteps(testId, List.of(currentTest)); // Stop test FinishTestItemRQ finishScenario = new FinishEventBuilder() .withStatus(Status.mapTo(currentTest.getResult())) diff --git a/src/test/java/com/github/invictum/reportportal/UtilsReplacePlaceholdersTest.java b/src/test/java/com/github/invictum/reportportal/UtilsReplacePlaceholdersTest.java new file mode 100644 index 0000000..3e9dcf2 --- /dev/null +++ b/src/test/java/com/github/invictum/reportportal/UtilsReplacePlaceholdersTest.java @@ -0,0 +1,42 @@ +package com.github.invictum.reportportal; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.*; + +@RunWith(Parameterized.class) +public class UtilsReplacePlaceholdersTest { + + private final String input; + private final List replacements; + private final String expectedResult; + + public UtilsReplacePlaceholdersTest(String input, List replacements, String expectedResult) { + this.input = input; + this.replacements = replacements; + this.expectedResult = expectedResult; + } + + @Parameterized.Parameters + public static Collection testData() { + return Arrays.asList(new Object[][]{ + {"Add two numbers & ", List.of("10", "20"), "Add two numbers 10 & 20"}, + {"This is and .", List.of("first", "second", "third"), + "This is first and second."}, + {"Hello , welcome to !", List.of("Alice", "Wonderland"), + "Hello Alice, welcome to Wonderland!"}, + {"No placeholders here", List.of(), "No placeholders here"}, + {"No placeholders here", List.of("first"), "No placeholders here"}, + {"", List.of("only"), "only"} + }); + } + + @Test + public void testReplacePlaceholders() { + String actualResult = Utils.replacePlaceholders(input, replacements); + Assert.assertEquals(expectedResult, actualResult); + } +}