1515import org .testng .annotations .BeforeSuite ;
1616
1717import java .lang .reflect .Method ;
18- import java .util .Objects ;
1918
2019@ Slf4j
2120public abstract class WebBaseTest {
@@ -38,34 +37,47 @@ public void init(Method method) {
3837 log .info ("Browser has been set." );
3938 WebTestListeners .setPage (page .get ());
4039 if (isChromiumBrowser ()) {
41- CdpUtils .initializeCdpSession (page .get ());
42- CdpUtils .enableCdpSession ();
43- CdpUtils .sendCommand (CdpCommands .NETWORK_ENABLE .getDescription ());
44- CdpUtils .logErrorResponses ();
45- CdpUtils .sendCommand (CdpCommands .CONSOLE_ENABLE .getDescription ());
46- CdpUtils .logConsoleErrors ();
47- CdpUtils .sendCommand (CdpCommands .RUNTIME_ENABLE .getDescription ());
48- CdpUtils .logUncaughtJavascriptErrors ();
49- CdpUtils .logUncaughtConsoleErrors ();
50- CdpUtils .sendCommand (CdpCommands .PAGE_ENABLE .getDescription ());
51- CdpUtils .logPageLoadCompletion ();
52- CdpUtils .logPageNavigatedWithinDocument ();
40+ initializeCdpSession (page .get ());
5341 }
42+ trackPerformanceMetrics (method );
43+ }
44+
45+ private void initializeCdpSession (Page page ) {
46+ CdpUtils .initializeCdpSession (page );
47+ CdpUtils .enableCdpSession ();
48+ CdpUtils .sendCommand (CdpCommands .NETWORK_ENABLE .getDescription ());
49+ CdpUtils .logErrorResponses ();
50+ CdpUtils .sendCommand (CdpCommands .CONSOLE_ENABLE .getDescription ());
51+ CdpUtils .logConsoleErrors ();
52+ CdpUtils .sendCommand (CdpCommands .RUNTIME_ENABLE .getDescription ());
53+ CdpUtils .logUncaughtJavascriptErrors ();
54+ CdpUtils .logUncaughtConsoleErrors ();
55+ CdpUtils .sendCommand (CdpCommands .PAGE_ENABLE .getDescription ());
56+ CdpUtils .logPageLoadCompletion ();
57+ CdpUtils .logPageNavigatedWithinDocument ();
58+ }
59+
60+ private void trackPerformanceMetrics (Method method ) {
5461 PerformanceUtils .evaluatePageLoadTime (page .get (), method .getName ());
5562 PerformanceUtils .evaluateDomContentLoadTime (page .get (), method .getName ());
5663 }
5764
5865 @ AfterMethod (alwaysRun = true )
5966 public void destroy () {
60- if (isChromiumBrowser ()) {
61- CdpUtils .destroyCdpSession ();
67+ try {
68+ if (isChromiumBrowser ()) {
69+ CdpUtils .destroyCdpSession ();
70+ }
71+ browserManager .destroyBrowserPage (page .get ());
72+ } catch (Exception e ) {
73+ log .error ("Cleanup failed: " , e );
74+ } finally {
75+ page .remove ();
76+ if (PLAYWRIGHT .get () != null ) {
77+ PLAYWRIGHT .get ().close ();
78+ PLAYWRIGHT .remove ();
79+ }
6280 }
63- browserManager .destroyBrowserPage (page .get ());
64- page .remove ();
65- Objects .requireNonNull (PLAYWRIGHT .get (), "Playwright is null!" );
66- PLAYWRIGHT .get ().close ();
67- PLAYWRIGHT .remove ();
68- log .info ("Browser has been destroyed." );
6981 }
7082
7183 private boolean isChromiumBrowser () {
0 commit comments