-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic browser closure #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
using Bellatrix.Playwright.Controls.EventHandlers; | ||
using Bellatrix.Playwright.Proxy; | ||
using Bellatrix.Playwright.Services; | ||
using Bellatrix.Playwright.Services.Browser; | ||
using Bellatrix.Plugins; | ||
using Bellatrix.Utilities; | ||
|
||
|
@@ -35,6 +36,7 @@ public class App : IDisposable | |
public App() | ||
{ | ||
_apiClientService = GetNewApiClientService(); | ||
AddShutdownHook(); | ||
} | ||
|
||
public BrowserService Browser => ServicesCollection.Current.Resolve<BrowserService>(); | ||
|
@@ -159,4 +161,15 @@ public TPage GoTo<TPage>() | |
page.Open(); | ||
return page; | ||
} | ||
|
||
private void AddShutdownHook() | ||
{ | ||
var container = ServicesCollection.Current; | ||
var driver = container.Resolve<WrappedBrowser>(); | ||
AppDomain.CurrentDomain.ProcessExit += new EventHandler((sender, eventArgs) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): The ProcessExit event handler is never unregistered, which could lead to memory leaks Consider storing the EventHandler delegate as a field and removing it in the Dispose method using ProcessExit -= |
||
{ | ||
DisposeBrowserService.Dispose(driver, container); | ||
this.Dispose(); | ||
Comment on lines
+171
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Recommended Solution: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Calling Dispose in the shutdown hook could lead to double-dispose issues Consider removing this call since the driver cleanup is already handled, or add a guard against multiple disposes |
||
}); | ||
Comment on lines
+169
to
+173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error logging in the ProcessExit event handler. Tell me moreConsider adding error logging in the ProcessExit event handler. If an exception occurs during the disposal process, it would be beneficial to log it for debugging purposes. You could wrap the disposal calls in a try-catch block and log any exceptions that occur. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ public class App : IDisposable | |
public App() | ||
{ | ||
_apiClientService = GetNewApiClientService(); | ||
AddShutdownHook(); | ||
} | ||
Comment on lines
+41
to
42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initializing resources and registering shutdown hooks directly in the constructor can lead to issues with testability and separation of concerns. Consider initializing critical resources like Recommended Change: public App() { }
public void Initialize() {
_apiClientService = GetNewApiClientService();
AddShutdownHook();
} |
||
|
||
public BrowserService Browser => ServicesCollection.Current.Resolve<BrowserService>(); | ||
|
@@ -216,4 +217,15 @@ private string DetermineTestClassFullNameAttributes() | |
|
||
return fullClassName; | ||
} | ||
|
||
private void AddShutdownHook() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Shutdown logic is duplicated across Playwright and Web implementations Consider extracting the shutdown hook logic into a shared base class or utility to avoid duplication
|
||
{ | ||
var container = ServicesCollection.Current; | ||
var driver = container.Resolve<IWebDriver>(); | ||
AppDomain.CurrentDomain.ProcessExit += new EventHandler((sender, eventArgs) => | ||
{ | ||
DisposeDriverService.Dispose(driver, container); | ||
this.Dispose(); | ||
}); | ||
Comment on lines
+225
to
+229
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Recommended Change: public void Dispose() {
DisposeDriverService.Dispose(driver, ServicesCollection.Current);
base.Dispose();
} |
||
} | ||
Comment on lines
+221
to
+230
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve IWebDriver within the event handler in AddShutdownHook. Tell me moreThe current implementation of AddShutdownHook resolves the IWebDriver too early. Instead of resolving it when adding the hook, we should resolve it within the event handler to ensure we're disposing of the correct driver instance at shutdown. Consider modifying the method as follows: private void AddShutdownHook()
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler((sender, eventArgs) =>
{
var container = ServicesCollection.Current;
var driver = container.Resolve<IWebDriver>();
DisposeDriverService.Dispose(driver, container);
this.Dispose();
});
} This change will ensure that the correct IWebDriver instance is resolved and disposed of when the application is actually shutting down. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method
GoTo<TPage>()
lacks error handling for scenarios where the page cannot be resolved or fails to open. This can lead to unhandled exceptions which may crash the application.Recommended Solution:
Implement try-catch blocks around the page resolution and opening steps to handle potential exceptions gracefully and provide fallback or error logging mechanisms.