3
3
import com .zebrunner .agent .core .config .ConfigurationHolder ;
4
4
import com .zebrunner .agent .core .registrar .TestSessionRegistrar ;
5
5
import com .zebrunner .agent .core .registrar .descriptor .SessionStartDescriptor ;
6
+ import lombok .Data ;
7
+ import lombok .NoArgsConstructor ;
8
+ import lombok .RequiredArgsConstructor ;
6
9
import lombok .extern .slf4j .Slf4j ;
7
10
import net .bytebuddy .implementation .bind .annotation .Argument ;
8
11
import net .bytebuddy .implementation .bind .annotation .RuntimeType ;
21
24
import java .net .URISyntaxException ;
22
25
import java .net .URL ;
23
26
import java .util .Arrays ;
27
+ import java .util .function .Consumer ;
24
28
25
29
@ Slf4j
26
30
public class StartSessionInterceptor {
27
31
28
32
private static final TestSessionRegistrar REGISTRAR = TestSessionRegistrar .getInstance ();
29
33
private static final CapabilitiesCustomizerChain CAPABILITIES_CUSTOMIZER_CHAIN = CapabilitiesCustomizerChain .getInstance ();
34
+ private static final ThreadLocal <Consumer <SessionRegisterDescriptor >> SESSION_REGISTER_DESCRIPTOR_CONSUMER = new ThreadLocal <>();
35
+
36
+ public static void setSessionRegisterConsumer (Consumer <SessionRegisterDescriptor > consumer ) {
37
+ SESSION_REGISTER_DESCRIPTOR_CONSUMER .set (consumer );
38
+ }
30
39
31
40
@ RuntimeType
32
41
public static void onSessionStart (@ This RemoteWebDriver driver ,
@@ -37,7 +46,9 @@ public static void onSessionStart(@This RemoteWebDriver driver,
37
46
capabilities = customizeCapabilities (methodInvocationProxy , capabilities );
38
47
}
39
48
40
- SessionStartDescriptor startDescriptor = SessionStartDescriptor .initiatedWith (capabilities .asMap ());
49
+ SessionRegisterDescriptor sessionRegisterDescriptor = new SessionRegisterDescriptor ();
50
+ sessionRegisterDescriptor .setSessionStartDescriptor (SessionStartDescriptor .initiatedWith (capabilities .asMap ()));
51
+
41
52
try {
42
53
methodInvocationProxy .run ();
43
54
@@ -61,14 +72,24 @@ public static void onSessionStart(@This RemoteWebDriver driver,
61
72
driverCapabilities = (Capabilities ) capabilitiesField .get (driver );
62
73
}
63
74
64
- startDescriptor .successfullyStartedWith (sessionId , driverCapabilities .asMap ());
75
+ sessionRegisterDescriptor .getSessionStartDescriptor ()
76
+ .successfullyStartedWith (sessionId , driverCapabilities .asMap ());
65
77
} catch (Exception e ) {
78
+ sessionRegisterDescriptor .setException (e );
66
79
StringWriter errorMessageStringWriter = new StringWriter ();
67
80
e .printStackTrace (new PrintWriter (errorMessageStringWriter ));
68
- startDescriptor .failedToStart (errorMessageStringWriter .toString ());
81
+ sessionRegisterDescriptor .getSessionStartDescriptor ()
82
+ .failedToStart (errorMessageStringWriter .toString ());
69
83
throw e ;
70
84
} finally {
71
- REGISTRAR .registerStart (startDescriptor );
85
+ if (SESSION_REGISTER_DESCRIPTOR_CONSUMER .get () == null ) {
86
+ REGISTRAR .registerStart (sessionRegisterDescriptor .getSessionStartDescriptor ());
87
+ } else {
88
+ Consumer <SessionRegisterDescriptor > consumer = SESSION_REGISTER_DESCRIPTOR_CONSUMER .get ();
89
+ SESSION_REGISTER_DESCRIPTOR_CONSUMER .remove ();
90
+ sessionRegisterDescriptor .setTestSessionRegistrar (REGISTRAR );
91
+ consumer .accept (sessionRegisterDescriptor );
92
+ }
72
93
}
73
94
}
74
95
@@ -157,4 +178,12 @@ private static Capabilities customizeCapabilities(Runnable methodInvocationProxy
157
178
return capabilities ;
158
179
}
159
180
181
+ @ Data
182
+ @ NoArgsConstructor
183
+ @ RequiredArgsConstructor
184
+ public static class SessionRegisterDescriptor {
185
+ private TestSessionRegistrar testSessionRegistrar ;
186
+ private SessionStartDescriptor sessionStartDescriptor ;
187
+ private Exception exception ;
188
+ }
160
189
}
0 commit comments