Added Webview2 to Temp Location and Playwright Custom Relative path i…#4215
Conversation
WalkthroughThe changes introduce Microsoft WebView2 integration into the AI-generated preview wizard page by adding initialization logic and error handling for the control. A new property for storing a temporary user data folder path is added to the base wizard class. Additionally, type corrections are made in a method handling regex matches for XPath locator generation. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Wizard
participant AIGeneratedPreviewWizardPage
participant WebView2
User->>Wizard: Start wizard
Wizard->>AIGeneratedPreviewWizardPage: Trigger Init event
AIGeneratedPreviewWizardPage->>AIGeneratedPreviewWizardPage: InitializeWebView()
AIGeneratedPreviewWizardPage->>WebView2: Create environment with user temp folder
WebView2-->>AIGeneratedPreviewWizardPage: Environment initialized
AIGeneratedPreviewWizardPage-->>Wizard: Initialization complete
Suggested reviewers
Poem
Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note ⚡️ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure ✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 2
🔭 Outside diff range comments (1)
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs (1)
147-150: 💡 Verification agent🧩 Analysis chain
Verify WebView2 initialization completion
The code sets the WebView source right after initialization, but there's no verification that the initialization has actually completed. Since
InitializeWebView()is async, it might not have finished when this line executes.Consider adding a way to ensure the WebView2 control is fully initialized before trying to navigate to content:
🏁 Script executed:
#!/bin/bash # Look for any checks for WebView2 initialization status before navigation rg -A 3 "MyWebView.Source" --glob "*.cs" # Check for any await patterns with WebView2 initialization across the codebase rg -A 5 "EnsureCoreWebView2Async" --glob "*.cs"Length of output: 1663
🏁 Script executed:
#!/bin/bash sed -n '1,300p' Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.csLength of output: 11414
Ensure WebView2 is fully initialized before setting
SourceThe current
async void InitializeWebView()is fire-and-forget, soGenerateHtmlAsyncmay callMyWebView.Source = …beforeEnsureCoreWebView2Asynchas completed. This can lead to intermittent navigation failures.Suggested fixes:
- Change initialization to return a
Taskand await it:
- In
AIGeneratedPreviewWizardPage.xaml.cs, rename and update:- public async void InitializeWebView() + public async Task InitializeWebViewAsync()- In the
WizardEvent(EventType.Init)handler:case EventType.Init: mWizard = (AddPOMFromScreenshotWizard)WizardEventArgs.Wizard;InitializeWebView();
await InitializeWebViewAsync(); ApiSettings = LoadApiSettings(); …
- Or guard the navigation in
GenerateHtmlAsyncby awaiting initialization if needed:// before navigating: if (MyWebView.CoreWebView2 == null) await MyWebView.EnsureCoreWebView2Async(); MyWebView.Source = new Uri(mWizard.HtmlFilePath);Both approaches ensure the WebView2 control is ready before attempting to load the HTML.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (3)
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs(2 hunks)Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/BasePOMWizard.cs(1 hunks)Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLearner.cs(1 hunks)
🔇 Additional comments (4)
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLearner.cs (1)
690-699: Good type correction for regex matchesThe changes correctly type the regex match collection and iteration, ensuring proper handling of match objects.
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs (3)
25-26: Appropriate imports for WebView2 integrationThe added imports for WebView2 components are necessary for the new functionality.
54-72: WebView2 initialization with error handlingGood implementation of the WebView2 initialization with proper error handling. This addresses the PR goal of adding WebView2 to a temporary location.
Some considerations:
- The temp folder path could be customized or configurable for different environments
- You might want to add cleanup logic somewhere (perhaps in a Dispose method) to remove temporary files when no longer needed
81-81: Good integration point for WebView2 initializationCalling InitializeWebView() during the Init event ensures the WebView2 environment is prepared before it's needed.
There was a problem hiding this comment.
Actionable comments posted: 4
♻️ Duplicate comments (1)
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs (1)
54-72: 🧹 Nitpick (assertive)WebView2 initialization logic looks good, but the comment needs correction
The implementation for WebView2 initialization with a temporary user data folder is well structured with proper error handling. However, there's a comment inconsistency.
- // Define the path to the local application data folder + // Define the path to the system temp folder mWizard.userTempDataFolderPath = Path.Combine(Path.GetTempPath(), "GingerWebView2");
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs(2 hunks)
🔇 Additional comments (1)
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs (1)
25-26: Appropriate WebView2 dependencies addedGood addition of the necessary WebView2 namespaces for integrating the Edge-based web view control.
| { | ||
| case EventType.Init: | ||
| mWizard = (AddPOMFromScreenshotWizard)WizardEventArgs.Wizard; | ||
| InitializeWebView(); |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Properly handle WebView2 initialization
Calling the async method without awaiting it may lead to race conditions. Consider either awaiting the result or handling the initialization completion differently.
- InitializeWebView();
+ // Option 1: For simplest implementation
+ await InitializeWebView();
+
+ // Option 2: For non-async context
+ _ = InitializeWebView().ContinueWith(t =>
+ {
+ if (t.IsFaulted)
+ {
+ Application.Current.Dispatcher.Invoke(() =>
+ Reporter.ToUser(eUserMsgKey.StaticErrorMessage, "Failed to initialize web preview"));
+ }
+ });Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs
at line 81, the async method InitializeWebView is called without awaiting it,
which can cause race conditions. Modify the code to await the InitializeWebView
call properly or implement a continuation to handle completion, ensuring the
WebView2 initialization finishes before proceeding.
| public async void InitializeWebView() | ||
| { | ||
| try | ||
| { | ||
| // Define the path to the local application data folder | ||
| mWizard.userTempDataFolderPath = Path.Combine(Path.GetTempPath(), "GingerWebView2"); | ||
|
|
||
| // Create the directory if it doesn't exist | ||
| Directory.CreateDirectory(mWizard.userTempDataFolderPath); | ||
|
|
||
| // Initialize WebView2 with the custom user data folder | ||
| var environment = await CoreWebView2Environment.CreateAsync(null, mWizard.userTempDataFolderPath); | ||
| await MyWebView.EnsureCoreWebView2Async(environment); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Reporter.ToLog(eLogLevel.ERROR, "Failed to load preview",ex); | ||
| } | ||
| } |
There was a problem hiding this comment.
🧹 Nitpick (assertive)
Consider using async Task instead of async void
The async void pattern should generally be reserved for event handlers. For better error propagation and testability, consider changing to async Task:
- public async void InitializeWebView()
+ public async Task InitializeWebView()Then update the caller to await this task or handle it properly.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| public async void InitializeWebView() | |
| { | |
| try | |
| { | |
| // Define the path to the local application data folder | |
| mWizard.userTempDataFolderPath = Path.Combine(Path.GetTempPath(), "GingerWebView2"); | |
| // Create the directory if it doesn't exist | |
| Directory.CreateDirectory(mWizard.userTempDataFolderPath); | |
| // Initialize WebView2 with the custom user data folder | |
| var environment = await CoreWebView2Environment.CreateAsync(null, mWizard.userTempDataFolderPath); | |
| await MyWebView.EnsureCoreWebView2Async(environment); | |
| } | |
| catch (Exception ex) | |
| { | |
| Reporter.ToLog(eLogLevel.ERROR, "Failed to load preview",ex); | |
| } | |
| } | |
| public async Task InitializeWebView() | |
| { | |
| try | |
| { | |
| // Define the path to the local application data folder | |
| mWizard.userTempDataFolderPath = Path.Combine(Path.GetTempPath(), "GingerWebView2"); | |
| // Create the directory if it doesn't exist | |
| Directory.CreateDirectory(mWizard.userTempDataFolderPath); | |
| // Initialize WebView2 with the custom user data folder | |
| var environment = await CoreWebView2Environment.CreateAsync(null, mWizard.userTempDataFolderPath); | |
| await MyWebView.EnsureCoreWebView2Async(environment); | |
| } | |
| catch (Exception ex) | |
| { | |
| Reporter.ToLog(eLogLevel.ERROR, "Failed to load preview", ex); | |
| } | |
| } |
🤖 Prompt for AI Agents
In
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs
between lines 54 and 72, the method InitializeWebView is declared as async void,
which should be avoided except for event handlers. Change the method signature
to async Task to allow proper error propagation and awaiting. Then update all
callers of InitializeWebView to await the returned Task or handle it
appropriately to ensure exceptions are caught and the asynchronous operation
completes correctly.
🧹 Nitpick (assertive)
Add cleanup mechanism for temporary WebView2 data folder
The code creates a temporary folder for WebView2 but doesn't include logic to clean it up when no longer needed. Consider adding a cleanup method to remove these temporary files.
// Add a new method to clean up temporary WebView2 data
+ public void CleanupWebViewData()
+ {
+ try
+ {
+ if (!string.IsNullOrEmpty(mWizard.userTempDataFolderPath) && Directory.Exists(mWizard.userTempDataFolderPath))
+ {
+ Directory.Delete(mWizard.userTempDataFolderPath, true);
+ }
+ }
+ catch (Exception ex)
+ {
+ Reporter.ToLog(eLogLevel.ERROR, "Failed to cleanup WebView2 data", ex);
+ }
+ }And call this method during wizard cleanup or application exit.
🤖 Prompt for AI Agents
In
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs
around lines 54 to 72, the InitializeWebView method creates a temporary folder
for WebView2 user data but lacks a cleanup mechanism. Add a new method to delete
the mWizard.userTempDataFolderPath directory and its contents safely, and ensure
this cleanup method is called during the wizard's disposal or when the
application exits to prevent leftover temporary files.
🧹 Nitpick (assertive)
Consider adding WebView2 runtime availability check
The code assumes WebView2 runtime is available on the user's system. Consider adding a check to verify WebView2 availability and provide guidance if it's not installed.
public async void InitializeWebView()
{
try
{
+ // Check if WebView2 is available
+ try
+ {
+ var version = CoreWebView2Environment.GetAvailableCoreWebView2Version();
+ if (string.IsNullOrEmpty(version))
+ {
+ Reporter.ToUser(eUserMsgKey.StaticWarningMessage, "WebView2 Runtime not found. Some features may not work properly. Please install WebView2 Runtime.");
+ return;
+ }
+ }
+ catch
+ {
+ Reporter.ToUser(eUserMsgKey.StaticWarningMessage, "WebView2 Runtime not found. Some features may not work properly. Please install WebView2 Runtime.");
+ return;
+ }
+
// Define the path to the local application data folder
mWizard.userTempDataFolderPath = Path.Combine(Path.GetTempPath(), "GingerWebView2");📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| public async void InitializeWebView() | |
| { | |
| try | |
| { | |
| // Define the path to the local application data folder | |
| mWizard.userTempDataFolderPath = Path.Combine(Path.GetTempPath(), "GingerWebView2"); | |
| // Create the directory if it doesn't exist | |
| Directory.CreateDirectory(mWizard.userTempDataFolderPath); | |
| // Initialize WebView2 with the custom user data folder | |
| var environment = await CoreWebView2Environment.CreateAsync(null, mWizard.userTempDataFolderPath); | |
| await MyWebView.EnsureCoreWebView2Async(environment); | |
| } | |
| catch (Exception ex) | |
| { | |
| Reporter.ToLog(eLogLevel.ERROR, "Failed to load preview",ex); | |
| } | |
| } | |
| public async void InitializeWebView() | |
| { | |
| try | |
| { | |
| // Check if WebView2 runtime is available | |
| try | |
| { | |
| var version = CoreWebView2Environment.GetAvailableCoreWebView2Version(); | |
| if (string.IsNullOrEmpty(version)) | |
| { | |
| Reporter.ToUser(eUserMsgKey.StaticWarningMessage, | |
| "WebView2 Runtime not found. Some features may not work properly. Please install WebView2 Runtime."); | |
| return; | |
| } | |
| } | |
| catch | |
| { | |
| Reporter.ToUser(eUserMsgKey.StaticWarningMessage, | |
| "WebView2 Runtime not found. Some features may not work properly. Please install WebView2 Runtime."); | |
| return; | |
| } | |
| // Define the path to the local application data folder | |
| mWizard.userTempDataFolderPath = Path.Combine(Path.GetTempPath(), "GingerWebView2"); | |
| // Create the directory if it doesn't exist | |
| Directory.CreateDirectory(mWizard.userTempDataFolderPath); | |
| // Initialize WebView2 with the custom user data folder | |
| var environment = await CoreWebView2Environment.CreateAsync(null, mWizard.userTempDataFolderPath); | |
| await MyWebView.EnsureCoreWebView2Async(environment); | |
| } | |
| catch (Exception ex) | |
| { | |
| Reporter.ToLog(eLogLevel.ERROR, "Failed to load preview", ex); | |
| } | |
| } |
🤖 Prompt for AI Agents
In
Ginger/Ginger/ApplicationModelsLib/POMModels/POMWizardLib/LearnWizard/AIGeneratedPreviewWizardPage.xaml.cs
around lines 54 to 72, the InitializeWebView method assumes the WebView2 runtime
is installed. Add a check before initializing WebView2 to verify if the runtime
is available on the user's system. If not available, provide a user-friendly
message or guidance on how to install the WebView2 runtime to prevent runtime
errors and improve user experience.
…ssue Fixed
Thank you for your contribution.
Before submitting this PR, please make sure:
Summary by CodeRabbit