1616package io .qameta .allure .awaitility ;
1717
1818import io .qameta .allure .Allure ;
19+ import io .qameta .allure .AllureExternalKey ;
1920import io .qameta .allure .AllureLifecycle ;
21+ import io .qameta .allure .AttachmentOptions ;
2022import io .qameta .allure .model .Status ;
2123import io .qameta .allure .model .StepResult ;
2224import org .awaitility .Awaitility ;
2729import org .awaitility .core .StartEvaluationEvent ;
2830import org .awaitility .core .TimeoutEvent ;
2931
32+ import java .io .ByteArrayInputStream ;
3033import java .io .PrintWriter ;
3134import java .io .StringWriter ;
3235import java .nio .charset .StandardCharsets ;
33- import java .util .UUID ;
3436import java .util .concurrent .TimeUnit ;
3537
3638import static java .util .concurrent .TimeUnit .MILLISECONDS ;
@@ -74,7 +76,7 @@ public class AllureAwaitilityListener implements ConditionEvaluationListener<Obj
7476 private final String onTimeoutStepTextPattern ;
7577 private final String onExceptionStepTextPattern ;
7678
77- private String currentConditionStepUUID ;
79+ private AllureExternalKey currentConditionStepKey ;
7880
7981 private static final InheritableThreadLocal <AllureLifecycle > LIFECYCLE = new InheritableThreadLocal <AllureLifecycle >() {
8082 @ Override
@@ -134,17 +136,22 @@ public AllureAwaitilityListener setLogIgnoredExceptions(final boolean logging) {
134136 */
135137 @ Override
136138 public void beforeEvaluation (final StartEvaluationEvent <Object > startEvaluationEvent ) {
137- currentConditionStepUUID = UUID .randomUUID ().toString ();
138- final String nameWoAlias = String .format (onStartStepTextPattern , startEvaluationEvent .getDescription ());
139- final String nameWithAlias = String .format (onStartStepTextPattern , startEvaluationEvent .getAlias ());
140- final String stepName = startEvaluationEvent .getAlias () != null ? nameWithAlias : nameWoAlias ;
141- getLifecycle ().startStep (
142- currentConditionStepUUID ,
143- new StepResult ()
144- .setName (stepName )
145- .setDescription ("Awaitility condition started" )
146- .setStatus (Status .FAILED )
147- );
139+ currentConditionStepKey = null ;
140+ getLifecycle ().getCurrentExecutableKey ().ifPresent (parent -> {
141+ final String nameWoAlias = String .format (onStartStepTextPattern , startEvaluationEvent .getDescription ());
142+ final String nameWithAlias = String .format (onStartStepTextPattern , startEvaluationEvent .getAlias ());
143+ final String stepName = startEvaluationEvent .getAlias () != null ? nameWithAlias : nameWoAlias ;
144+ final AllureExternalKey conditionStepKey = AllureExternalKey .random (AllureAwaitilityListener .class );
145+ currentConditionStepKey = conditionStepKey ;
146+ getLifecycle ().startStep (
147+ parent ,
148+ conditionStepKey ,
149+ new StepResult ()
150+ .setName (stepName )
151+ .setDescription ("Awaitility condition started" )
152+ .setStatus (Status .FAILED )
153+ );
154+ });
148155 }
149156
150157 /**
@@ -154,19 +161,20 @@ public void beforeEvaluation(final StartEvaluationEvent<Object> startEvaluationE
154161 */
155162 @ Override
156163 public void onTimeout (final TimeoutEvent timeoutEvent ) {
157- getLifecycle ().updateStep (awaitilityCondition -> {
158- final String currentTimeoutStepUUID = UUID .randomUUID ().toString ();
159- getLifecycle ().startStep (
160- currentConditionStepUUID ,
161- currentTimeoutStepUUID ,
162- new StepResult ()
163- .setName (String .format (onTimeoutStepTextPattern , timeoutEvent .getDescription ()))
164- .setDescription ("Awaitility condition timeout" )
165- .setStatus (Status .BROKEN )
166- );
167- getLifecycle ().stopStep (currentTimeoutStepUUID );
168- });
169- getLifecycle ().stopStep (currentConditionStepUUID );
164+ if (currentConditionStepKey == null ) {
165+ return ;
166+ }
167+ final AllureExternalKey timeoutStepKey = AllureExternalKey .random (AllureAwaitilityListener .class );
168+ getLifecycle ().startStep (
169+ currentConditionStepKey ,
170+ timeoutStepKey ,
171+ new StepResult ()
172+ .setName (String .format (onTimeoutStepTextPattern , timeoutEvent .getDescription ()))
173+ .setDescription ("Awaitility condition timeout" )
174+ .setStatus (Status .BROKEN )
175+ );
176+ getLifecycle ().stopStep (timeoutStepKey );
177+ getLifecycle ().stopStep (currentConditionStepKey );
170178 }
171179
172180 /**
@@ -191,22 +199,25 @@ public void conditionEvaluated(final EvaluatedCondition<Object> condition) {
191199 new TemporalDuration (condition .getPollInterval ())
192200 );
193201
194- getLifecycle ().updateStep (awaitilityCondition -> {
195- final String lastAwaitStepUUID = UUID .randomUUID ().toString ();
196- getLifecycle ().startStep (
197- currentConditionStepUUID ,
198- lastAwaitStepUUID ,
199- new StepResult ()
200- .setName (message )
201- .setDescription ("Awaitility condition satisfied or not, but awaiting still in progress" )
202- .setStatus (Status .PASSED )
202+ if (currentConditionStepKey == null ) {
203+ return ;
204+ }
205+ final AllureExternalKey lastAwaitStepKey = AllureExternalKey .random (AllureAwaitilityListener .class );
206+ getLifecycle ().startStep (
207+ currentConditionStepKey ,
208+ lastAwaitStepKey ,
209+ new StepResult ()
210+ .setName (message )
211+ .setDescription ("Awaitility condition satisfied or not, but awaiting still in progress" )
212+ .setStatus (Status .PASSED )
213+ );
214+ getLifecycle ().stopStep (lastAwaitStepKey );
215+ if (condition .isSatisfied ()) {
216+ getLifecycle ().updateStep (
217+ currentConditionStepKey , awaitilityCondition -> awaitilityCondition .setStatus (Status .PASSED )
203218 );
204- getLifecycle ().stopStep (lastAwaitStepUUID );
205- if (condition .isSatisfied ()) {
206- awaitilityCondition .setStatus (Status .PASSED );
207- getLifecycle ().stopStep (currentConditionStepUUID );
208- }
209- });
219+ getLifecycle ().stopStep (currentConditionStepKey );
220+ }
210221 }
211222
212223 /**
@@ -224,29 +235,30 @@ public void conditionEvaluated(final EvaluatedCondition<Object> condition) {
224235 */
225236 @ Override
226237 public void exceptionIgnored (final IgnoredException ignoredException ) {
227- if (logIgnoredExceptions ) {
228- getLifecycle ().updateStep (awaitilityCondition -> {
229- final String currentExceptionIgnoredStepUUID = UUID .randomUUID ().toString ();
230- final String message = String .format (
231- onExceptionStepTextPattern , ignoredException .getThrowable ().getMessage ()
232- );
233- final StringWriter stringWriter = new StringWriter ();
234- ignoredException .getThrowable ().printStackTrace (new PrintWriter (stringWriter ));
235- final String stackTrace = stringWriter .toString ();
236- getLifecycle ().startStep (
237- currentConditionStepUUID ,
238- currentExceptionIgnoredStepUUID ,
239- new StepResult ()
240- .setName (message )
241- .setDescription ("Exception occurred and ignored, but awaiting still in progress" )
242- .setStatus (Status .SKIPPED )
243- );
244- getLifecycle ().addAttachment (
245- ignoredException .getThrowable ().getMessage (), "text/plain" , ".txt" ,
246- stackTrace .getBytes (StandardCharsets .UTF_8 )
247- );
248- getLifecycle ().stopStep (currentExceptionIgnoredStepUUID );
249- });
238+ if (logIgnoredExceptions && currentConditionStepKey != null ) {
239+ final AllureExternalKey exceptionIgnoredStepKey = AllureExternalKey .random (AllureAwaitilityListener .class );
240+ final String message = String .format (
241+ onExceptionStepTextPattern , ignoredException .getThrowable ().getMessage ()
242+ );
243+ final StringWriter stringWriter = new StringWriter ();
244+ ignoredException .getThrowable ().printStackTrace (new PrintWriter (stringWriter ));
245+ final String stackTrace = stringWriter .toString ();
246+ getLifecycle ().startStep (
247+ currentConditionStepKey ,
248+ exceptionIgnoredStepKey ,
249+ new StepResult ()
250+ .setName (message )
251+ .setDescription ("Exception occurred and ignored, but awaiting still in progress" )
252+ .setStatus (Status .SKIPPED )
253+ );
254+ getLifecycle ().addAttachment (
255+ exceptionIgnoredStepKey ,
256+ ignoredException .getThrowable ().getMessage (),
257+ "text/plain" ,
258+ new ByteArrayInputStream (stackTrace .getBytes (StandardCharsets .UTF_8 )),
259+ AttachmentOptions .empty ()
260+ );
261+ getLifecycle ().stopStep (exceptionIgnoredStepKey );
250262 }
251263 }
252264
0 commit comments