Skip to content

Commit a5a7b14

Browse files
authored
#187: Add support for Scenario outlines custom name strategies (#191)
1 parent ee58c32 commit a5a7b14

File tree

5 files changed

+117
-31
lines changed

5 files changed

+117
-31
lines changed

README.md

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Table of Contents
3232
2. [Log units](#log-units)
3333
3. [Merge launches](#merge-launches)
3434
4. [Test retries](#test-retries)
35-
5. [Other settings](#other-settings)
35+
5. [BDD Scenario test name customization](#bdd-scenario-test-name-customization)
36+
6. [Other settings](#other-settings)
3637
3. [Data mapping](#data-mapping)
3738
4. [Versioning](#versioning)
3839
5. [Important release notes](#important-release-notes)
@@ -160,14 +161,14 @@ configuration.usePreset(LogsPreset.FULL);
160161

161162
> **Notice**
162163
>
163-
> All integration configurations should be done before the start of Serenity facility. Otherwise default values will be
164-
> used.
164+
> All integration configurations should be completed before starting the Serenity engine; otherwise, default values will
165+
> be used.
165166
166167
#### Presets
167168

168169
Each Serenity `TestStep` object is passed through chain of configured log units. Each particular log unit analyses step
169-
and creates a collection of records that will be send to RP. This approach allows to flexible configure reporting
170-
behaviour on a step level. By default integration provides a few log presets:
170+
and creates a collection of records that will be sent to RP. This approach allows to flexible configure reporting
171+
behaviour on a step level. By default, integration provides a few log presets:
171172

172173
- DEFAULT
173174
- FULL
@@ -272,7 +273,7 @@ timestamp.
272273

273274
#### Merge launches
274275

275-
By default separate launch will be created in RP for each module in case of multi-module project. This behavior can be
276+
By default, separate launch will be created in RP for each module in case of multi-module project. This behavior can be
276277
changed with merge launches feature.
277278

278279
To understand a concept, let's assume following multi-module structure
@@ -331,14 +332,54 @@ and add `failsafe.rerunFailingTestsCount` or `surefire.rerunFailingTestsCount` p
331332
> more
332333
> details.
333334
335+
#### BDD Scenario test name customization
336+
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:
341+
342+
1. Create class `ParametrizedNameProvider`
343+
344+
```
345+
public class ParametrizedNameProvider implements TestNameProvider {
346+
347+
@Override
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();
366+
}
367+
```
368+
369+
2. Update integration configuration
370+
371+
```
372+
ReportIntegrationConfig.get().useTestNameTransformer(new ParametrizedNameProvider());
373+
```
374+
334375
#### Other settings
335376

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

338-
Setting | Usage | Description
339-
--------------------------|-----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
340-
Selenium logs harvesting | `ReportIntegrationConfig.get().harvestSeleniumLogs(true)` | Special option that works in conjunction with `Selenium.filteredLogs(...)` unit and must be enabled as well in order it to works. By default it is disabled.
341-
Truncate names | `ReportIntegrationConfig.get().truncateNames(true)` | Allows to hide RP server errors that related to entities with long names (more that 1024 symbols) creation. It is not recommended to use it. By default it is disabled.
379+
| Setting | Usage | Description |
380+
|--------------------------|-----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
381+
| Selenium logs harvesting | `ReportIntegrationConfig.get().harvestSeleniumLogs(true)` | Special option that works in conjunction with `Selenium.filteredLogs(...)` unit and must be enabled as well in order it to works. By default it is disabled. |
382+
| Truncate names | `ReportIntegrationConfig.get().truncateNames(true)` | Allows to hide RP server errors that related to entities with long names (more that 1024 symbols) creation. It is not recommended to use it. By default it is disabled. |
342383

343384
## Data mapping
344385

@@ -347,12 +388,12 @@ relates to each other.
347388

348389
**Name** relation is straightforward.
349390

350-
Serenity | Report portal
351-
-------------|---------------
352-
Test Class | Suite
353-
Test Method | Test
354-
Scenario | Test
355-
Step | Log entry
391+
| Serenity | Report portal |
392+
|-------------|---------------|
393+
| Test Class | Suite |
394+
| Test Method | Test |
395+
| Scenario | Test |
396+
| Step | Log entry |
356397

357398
**Description** Each non-log entity in Report Portal may has a description. This field is populated from Serenity
358399
narrative section for both jUnit and BDD test sources.
@@ -446,24 +487,24 @@ Important release notes are described below.
446487
Use [releases](https://github.com/Invictum/serenity-reportportal-integration/releases) section for details regarding
447488
regular release notes.
448489

449-
Version | Note
450-
---------------|-----------------------------------------------------------------------------------------------------------------
451-
1.0.0 - 1.0.6 | Supports RP v3 and below
452-
1.1.0 - 1.1.3 | Minor version update due RP v4 release. Versions older than 1.1.0 are not compatible with RP v4+ and vise versa
453-
1.2.0 - 1.2.1 | Minor version updated due internal mechanisms approach major refactoring
454-
1.3.0 | Minor version updated due to log units approach rework
455-
1.4.0 - 1.4.3 | Minor version update: removed tree handler, refactored to support DDT for BDD
456-
1.5.0+ | Minor version update due RP v5 release
457-
1.6.0+ | Minor version update due Serenity 4 release. Report Portal is updated to 5.8 as well
490+
| Version | Note |
491+
|---------------|-------------------------------------------------------------------------------------------------------------------------|
492+
| 1.0.0 - 1.0.6 | Supports RP v3 and below |
493+
| 1.1.0 - 1.1.3 | Minor version update due RP v4 release. Versions older than 1.1.0 are not compatible with RP v4+ and vise versa |
494+
| 1.2.0 - 1.2.1 | Minor version updated due internal mechanisms approach major refactoring |
495+
| 1.3.0 | Minor version updated due to log units approach rework |
496+
| 1.4.0 - 1.4.3 | Minor version update: removed tree handler, refactored to support DDT for BDD |
497+
| 1.5.0+ | Minor version update due RP v5 release |
498+
| 1.6.0+ | Minor version update due Serenity 4 release. Report Portal is updated to 5.8 as well. Bugfixes and some of new features |
458499

459500
## Limitations
460501

461502
Integration has limited concurrency support.
462503
For versions < 4 concurrency for parametrized Serenity tests execution is not supported.
463504
For version >=4 concurrency is supported on feature level.
464505

465-
The following line should be provided into `junit-platform.properties` file of your project for versions 1.6.0+ in order for integration to
466-
work correctly:
506+
The following line should be provided into `junit-platform.properties` file of your project for versions 1.6.0+ in order
507+
for integration to work correctly:
467508

468509
**JUnit**
469510

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

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

3-
3+
import com.github.invictum.reportportal.recorder.DefaultBddOutlineTestNameProvider;
4+
import com.github.invictum.reportportal.recorder.TestNameProvider;
45
import net.serenitybdd.annotations.Narrative;
56

67
import java.util.Objects;
@@ -20,6 +21,7 @@ public class ReportIntegrationConfig {
2021

2122
private LogsPreset preset = LogsPreset.DEFAULT;
2223
private Function<Narrative, String> classNarrativeFormatter = n -> String.join("\n", n.text());
24+
private TestNameProvider testNameProvider = new DefaultBddOutlineTestNameProvider();
2325
boolean harvestSeleniumLogs = false;
2426
boolean truncateNames = false;
2527

@@ -57,6 +59,18 @@ public ReportIntegrationConfig useClassNarrativeFormatter(Function<Narrative, St
5759
return this;
5860
}
5961

62+
/**
63+
* Overrides default test name, passed from Serenity, with custom logic.
64+
*/
65+
public ReportIntegrationConfig useTestNameTransformer(TestNameProvider testNameProvider) {
66+
this.testNameProvider = testNameProvider;
67+
return this;
68+
}
69+
70+
public TestNameProvider getTestNameTransformer() {
71+
return testNameProvider;
72+
}
73+
6074
/**
6175
* Option allows to enable or disable selenium based logs harvesting
6276
* Designed to be used in conjunction with {@link com.github.invictum.reportportal.log.unit.Selenium} log unit

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import net.thucydides.model.domain.TestOutcome;
1010
import net.thucydides.model.domain.TestStep;
1111

12-
import java.util.Arrays;
12+
import java.util.List;
1313

1414
/**
1515
* Recorder aware of parameterized BDD style test specific handling
@@ -32,16 +32,17 @@ 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+
TestNameProvider testNameProvider = ReportIntegrationConfig.get().getTestNameTransformer();
3536
// Start test
3637
StartTestItemRQ startScenario = new StartEventBuilder(ItemType.STEP)
37-
.withName(out.getName())
38+
.withName(testNameProvider.provideName(out, last))
3839
.withStartTime(currentTest.getStartTime())
3940
.withParameters(out.getDataTable().row(last))
4041
.withTags(out.getTags())
4142
.build();
4243
Maybe<String> testId = launch.startTestItem(id, startScenario);
4344
// Steps
44-
proceedSteps(testId, Arrays.asList(currentTest));
45+
proceedSteps(testId, List.of(currentTest));
4546
// Stop test
4647
FinishTestItemRQ finishScenario = new FinishEventBuilder()
4748
.withStatus(Status.mapTo(currentTest.getResult()))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.invictum.reportportal.recorder;
2+
3+
import net.thucydides.model.domain.TestOutcome;
4+
5+
/**
6+
* Default implementation of name as seen in Serenity.
7+
*/
8+
public class DefaultBddOutlineTestNameProvider implements TestNameProvider {
9+
10+
@Override
11+
public String provideName(TestOutcome testOutcome, int scenarioIndex) {
12+
return testOutcome.getName();
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.invictum.reportportal.recorder;
2+
3+
import net.thucydides.model.domain.TestOutcome;
4+
5+
/**
6+
* Allows to transform particular test name in ReportPortal according to specific user requirements.
7+
*/
8+
public interface TestNameProvider {
9+
10+
/**
11+
* @param testOutcome original test data
12+
* @param scenarioIndex index of scenario which is being reported
13+
* @return transformed test name
14+
*/
15+
String provideName(TestOutcome testOutcome, int scenarioIndex);
16+
}

0 commit comments

Comments
 (0)