Skip to content

Commit

Permalink
Add runtime Launch attributes update example
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 19, 2024
1 parent e00d652 commit 58f3e7f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion example-testng-logback/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>agent-java-testng</artifactId>
<version>5.4.4</version>
<version>5.4.5</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@
*/
@Listeners({ ParametersAsAttributesTest.ExtendedListener.class })
public class ParametersAsAttributesTest {

private static final Logger LOGGER = LoggerFactory.getLogger(ParametersAsAttributesTest.class);

@Test(threadPoolSize = 2, dataProvider = "bla-bla")
public void testParams(String msg) throws InterruptedException {
for (int i = 0; i < 10; i++) {
LOGGER.info(msg + ": " + i);
LOGGER.info("{}: {}", msg, i);
if (i == 1) {
Thread.sleep(TimeUnit.SECONDS.toMillis(5L));
}
Expand All @@ -76,7 +75,6 @@ public void onTestStart(ITestResult testResult) {
}

public static class ParamTaggingTestNgService extends TestNGService {

public ParamTaggingTestNgService(@Nonnull ReportPortal reportPortal) {
super(reportPortal);
}
Expand All @@ -91,7 +89,6 @@ protected StartTestItemRQ buildStartStepRq(final @Nonnull ITestResult testResult
attributes.add(new ItemAttributesRQ(null, param.toString()));
}
rq.setAttributes(attributes);

}
return rq;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class RetriedTestReasonReportingTest {
public void failOne() {
int retry = COUNTER.incrementAndGet();
if (retry <= MAXIMUM_RETRIES) {
LOGGER.warn("Failed attempt: " + retry);
LOGGER.warn("Failed attempt: {}", retry);
Assert.fail();
}
LOGGER.info("Success attempt");
Expand All @@ -63,14 +63,14 @@ public static class RetryImpl implements IRetryAnalyzer {
@Override
public boolean retry(ITestResult result) {
int retry = retryNumber.incrementAndGet();
LOGGER.info("Retry attempt: " + retry);
LOGGER.info("Retry attempt: {}", retry);
return retry <= MAXIMUM_RETRIES;
}
}

public static class ExtendedListener extends BaseTestNGListener {
public static final Supplier<ITestNGService> SERVICE =
new MemoizingSupplier<>(() -> new SkipReasonLoggingService(ReportPortal.builder().build()));
public static final Supplier<ITestNGService> SERVICE = new MemoizingSupplier<>(() -> new SkipReasonLoggingService(ReportPortal.builder()
.build()));

public ExtendedListener() {
super(SERVICE.get());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.epam.reportportal.example.testng.logback.extension;

import com.epam.reportportal.listeners.ListenerParameters;
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.testng.BaseTestNGListener;
import com.epam.reportportal.testng.ITestNGService;
import com.epam.reportportal.testng.TestNGService;
import com.epam.reportportal.utils.MemoizingSupplier;
import com.epam.ta.reportportal.ws.model.FinishExecutionRQ;
import com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import static java.util.Optional.ofNullable;

/**
* Test class to demonstrate how to add runtime attributes to a Launch.
*/
@Listeners({ RuntimeLaunchAttributesTest.ExtendedListener.class })
public class RuntimeLaunchAttributesTest {
private static final Set<ItemAttributesRQ> RUNTIME_ATTRIBUTES = Collections.newSetFromMap(new ConcurrentHashMap<>());

@Test
public void testRuntimeLaunchAttributes() {
System.out.println("Test stub to demonstrate runtime launch attributes.");
// Add runtime attributes
RUNTIME_ATTRIBUTES.add(new ItemAttributesRQ("runtime1", "value1"));
}

public static class ExtendedListener extends BaseTestNGListener {
public static final Supplier<ITestNGService> SERVICE = new MemoizingSupplier<>(() -> new TestNGService(ReportPortal.builder()
.build()) {
@Override
protected FinishExecutionRQ buildFinishLaunchRq(ListenerParameters parameters) {
FinishExecutionRQ rq = super.buildFinishLaunchRq(parameters);
Set<ItemAttributesRQ> attributes = ofNullable(rq.getAttributes()).orElseGet(HashSet::new);
attributes.addAll(RUNTIME_ATTRIBUTES);
rq.setAttributes(attributes);
return rq;
}
});

public ExtendedListener() {
super(SERVICE.get());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* More advanced way to report a screenshot. Reports screenshot as a separate log entry in 'Test' method.
*/
@Listeners({ScreenshotOnFailureTestExtensionTest.ExtendedListener.class})
@Listeners({ ScreenshotOnFailureTestExtensionTest.ExtendedListener.class })
public class ScreenshotOnFailureTestExtensionTest {

private static WebDriver driver;
Expand Down

0 comments on commit 58f3e7f

Please sign in to comment.