Skip to content

#187: Scenario outlines do not support dynamic parameters in the scenario name #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.invictum</groupId>
<artifactId>serenity-reportportal-integration</artifactId>
<version>1.6.0-SNAPSHOT</version>
<version>1.6.2-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down Expand Up @@ -39,8 +39,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<serenity.version>4.0.21</serenity.version>
<rp.client.version>5.1.22</rp.client.version>
<rp.commons.version>5.8.1</rp.commons.version>
<rp.client.version>5.2.14</rp.client.version>
<rp.commons.version>5.11.1</rp.commons.version>
</properties>

<dependencies>
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/github/invictum/reportportal/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 <number1> & <number2>" => "Add two numbers -2 & 3".
*
* @return modified string
*/
public static String replacePlaceholders(String originalString, List<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,16 +33,18 @@ public void record(TestOutcome out) {
.withDescription(out.getUserStory().getNarrative())
.build();
Maybe<String> id = suiteStorage.start(out.getUserStory().getId(), () -> launch.startTestItem(startStory));
final List<DataTableRow> dataRows = out.getDataTable().getRows();
final List<String> 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<String> 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()))
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> replacements;
private final String expectedResult;

public UtilsReplacePlaceholdersTest(String input, List<String> replacements, String expectedResult) {
this.input = input;
this.replacements = replacements;
this.expectedResult = expectedResult;
}

@Parameterized.Parameters
public static Collection<Object[]> testData() {
return Arrays.asList(new Object[][]{
{"Add two numbers <num1> & <num2>", List.of("10", "20"), "Add two numbers 10 & 20"},
{"This is <placeholder1> and <placeholder2>.", List.of("first", "second", "third"),
"This is first and second."},
{"Hello <name>, welcome to <place>!", 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"},
{"<single>", List.of("only"), "only"}
});
}

@Test
public void testReplacePlaceholders() {
String actualResult = Utils.replacePlaceholders(input, replacements);
Assert.assertEquals(expectedResult, actualResult);
}
}
Loading