Skip to content

Commit ac80088

Browse files
committed
#187: CR comments addressed
1 parent 5f75c69 commit ac80088

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

README.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,25 +334,44 @@ and add `failsafe.rerunFailingTestsCount` or `surefire.rerunFailingTestsCount` p
334334
335335
#### BDD Scenario test name customization
336336

337-
n some cases, you may need to customize the test name for BDD Scenario Outlines (for example, to insert parameters into
338-
the test name in ReportPortal). To achieve this, you need to implement the desired customization logic in a class that
339-
implements the `TestNameTransformer` interface. This class should then be specified in the `ReportIntegrationConfig`.
337+
In some cases, you may need to customize the test name for BDD Scenario Outlines (for example, to insert parameters into
338+
the test name in ReportPortal). To achieve this, you need to implement the desired customization logic in a class which
339+
implements the `TestNameProvider` interface. This class should then be specified in the `ReportIntegrationConfig`.
340+
For example, to provide ability for test name to insert parameters, the following steps should be performed:
340341

341-
```
342-
ReportIntegrationConfig.get().useTestNameTransformer(new MyTransformer());
343-
```
342+
1. Create class `ParametrizedNameProvider`
344343

345344
```
346-
public class MyTransformer implements TestNameTransformer {
345+
public class ParametrizedNameProvider implements TestNameProvider {
347346
348347
@Override
349-
public String transformName(TestOutcome testOutcome, int scenarioIndex) {
350-
<...
351-
//transformation logic
352-
...>
348+
public String provideName(TestOutcome testOutcome, int scenarioIndex) {
349+
// Regular expression to match content within < >
350+
final String parameterRegexp = "<([^>]+)>";
351+
final Pattern pattern = Pattern.compile(parameterRegexp);
352+
final Matcher matcher = pattern.matcher(testOutcome.getName());
353+
final StringBuilder result = new StringBuilder();
354+
int index = 0;
355+
List<String> replacements = testOutcome.getDataTable().getRows().get(scenarioIndex).getStringValues();
356+
// Iterate through the matches and replace with values from the ArrayList
357+
while (matcher.find()) {
358+
if (index < replacements.size()) {
359+
matcher.appendReplacement(result, replacements.get(index));
360+
index++;
361+
}
362+
}
363+
// Append the rest of the string
364+
matcher.appendTail(result);
365+
return result.toString();
353366
}
354367
```
355368

369+
2. Update integration configuration
370+
371+
```
372+
ReportIntegrationConfig.get().useTestNameTransformer(new ParametrizedNameProvider());
373+
```
374+
356375
#### Other settings
357376

358377
Section includes minor settings that available to configure and describes their usage.

src/main/java/com/github/invictum/reportportal/ReportIntegrationConfig.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.invictum.reportportal;
22

3-
import com.github.invictum.reportportal.recorder.DefaultBddOutlineTestNameTransformer;
4-
import com.github.invictum.reportportal.recorder.TestNameTransformer;
3+
import com.github.invictum.reportportal.recorder.DefaultBddOutlineTestNameProvider;
4+
import com.github.invictum.reportportal.recorder.TestNameProvider;
55
import net.serenitybdd.annotations.Narrative;
66

77
import java.util.Objects;
@@ -21,7 +21,7 @@ public class ReportIntegrationConfig {
2121

2222
private LogsPreset preset = LogsPreset.DEFAULT;
2323
private Function<Narrative, String> classNarrativeFormatter = n -> String.join("\n", n.text());
24-
private TestNameTransformer testNameTransformer = new DefaultBddOutlineTestNameTransformer();
24+
private TestNameProvider testNameProvider = new DefaultBddOutlineTestNameProvider();
2525
boolean harvestSeleniumLogs = false;
2626
boolean truncateNames = false;
2727

@@ -62,13 +62,13 @@ public ReportIntegrationConfig useClassNarrativeFormatter(Function<Narrative, St
6262
/**
6363
* Overrides default test name, passed from Serenity, with custom logic.
6464
*/
65-
public ReportIntegrationConfig useTestNameTransformer(TestNameTransformer testNameTransformer) {
66-
this.testNameTransformer = testNameTransformer;
65+
public ReportIntegrationConfig useTestNameTransformer(TestNameProvider testNameProvider) {
66+
this.testNameProvider = testNameProvider;
6767
return this;
6868
}
6969

70-
public TestNameTransformer getTestNameTransformer() {
71-
return testNameTransformer;
70+
public TestNameProvider getTestNameTransformer() {
71+
return testNameProvider;
7272
}
7373

7474
/**

src/main/java/com/github/invictum/reportportal/recorder/BddDataDriven.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public void record(TestOutcome out) {
3232
.withDescription(out.getUserStory().getNarrative())
3333
.build();
3434
Maybe<String> id = suiteStorage.start(out.getUserStory().getId(), () -> launch.startTestItem(startStory));
35-
TestNameTransformer testNameTransformer = ReportIntegrationConfig.get().getTestNameTransformer();
35+
TestNameProvider testNameProvider = ReportIntegrationConfig.get().getTestNameTransformer();
3636
// Start test
3737
StartTestItemRQ startScenario = new StartEventBuilder(ItemType.STEP)
38-
.withName(testNameTransformer.transformName(out, last))
38+
.withName(testNameProvider.provideName(out, last))
3939
.withStartTime(currentTest.getStartTime())
4040
.withParameters(out.getDataTable().row(last))
4141
.withTags(out.getTags())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
/**
66
* Default implementation of name as seen in Serenity.
77
*/
8-
public class DefaultBddOutlineTestNameTransformer implements TestNameTransformer {
8+
public class DefaultBddOutlineTestNameProvider implements TestNameProvider {
99

1010
@Override
11-
public String transformName(TestOutcome testOutcome, int scenarioIndex) {
11+
public String provideName(TestOutcome testOutcome, int scenarioIndex) {
1212
return testOutcome.getName();
1313
}
1414
}

src/main/java/com/github/invictum/reportportal/recorder/TestNameTransformer.java renamed to src/main/java/com/github/invictum/reportportal/recorder/TestNameProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/**
66
* Allows to transform particular test name in ReportPortal according to specific user requirements.
77
*/
8-
public interface TestNameTransformer {
8+
public interface TestNameProvider {
99

1010
/**
1111
* @param testOutcome original test data
1212
* @param scenarioIndex index of scenario which is being reported
1313
* @return transformed test name
1414
*/
15-
String transformName(TestOutcome testOutcome, int scenarioIndex);
15+
String provideName(TestOutcome testOutcome, int scenarioIndex);
1616
}

0 commit comments

Comments
 (0)