Skip to content

Commit 15cacda

Browse files
committed
Allow playwright to connect over cdp for WebView2 automation
It should be possible now to connect over CDP using playwright to automation WebView2 applications. We are not supporting however launching a WebView2 application. If launch is required, it needs to be handled by using a custom script.
1 parent cda2c68 commit 15cacda

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

src/Pixel.Automation.Web.Playwright.Components/WebApplicationEntity.cs

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Pixel.Automation.Core.Interfaces;
55
using System.ComponentModel;
66
using System.ComponentModel.DataAnnotations;
7+
using System.Diagnostics;
78
using System.Runtime.Serialization;
89

910
namespace Pixel.Automation.Web.Playwright.Components;
@@ -51,7 +52,14 @@ public class WebApplicationEntity : ApplicationEntity
5152
[DataMember]
5253
[Display(Name = "Install Browser", GroupName = "Browser Management", Order = 50, Description = "Indicates if configured browser should be automatically installed")]
5354
public Argument AutoInstallBrowser { get; set; } = new InArgument<bool> { Mode = ArgumentMode.Default, CanChangeType = false, DefaultValue = true };
54-
55+
56+
/// <summary>
57+
/// Remote debugging port to use to connect over CDP. Use this for automation of a WebView2 application
58+
/// </summary>
59+
[DataMember]
60+
[Display(Name = "Remote Debugging Port", GroupName = "WebView2", Order = 20, Description = "Remote debugging port to use")]
61+
public Argument RemoteDebuggingPort { get; set; } = new InArgument<string>() { CanChangeType = false };
62+
5563
/// <summary>
5664
/// Get the TargetApplicationDetails and apply any over-rides to it
5765
/// </summary>
@@ -78,43 +86,55 @@ public override async Task LaunchAsync()
7886
return;
7987
}
8088

81-
Browsers preferredBrowser = await GetPreferredBrowser(webApplicationDetails);
82-
var browserLaunchOptions = await GetBrowserLaunchOptions(preferredBrowser);
83-
84-
await InstallBrowser(preferredBrowser, browserLaunchOptions);
85-
86-
webApplicationDetails.Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
87-
88-
switch (preferredBrowser)
89+
//For WebView2 process
90+
if(this.RemoteDebuggingPort.IsConfigured())
8991
{
90-
case Browsers.Chrome:
91-
case Browsers.Edge:
92-
webApplicationDetails.Browser = await webApplicationDetails.Playwright.Chromium.LaunchAsync(browserLaunchOptions);
93-
break;
94-
case Browsers.FireFox:
95-
webApplicationDetails.Browser = await webApplicationDetails.Playwright.Firefox.LaunchAsync(browserLaunchOptions);
96-
break;
97-
98-
case Browsers.WebKit:
99-
webApplicationDetails.Browser = await webApplicationDetails.Playwright.Webkit.LaunchAsync(browserLaunchOptions);
100-
break;
101-
default:
102-
throw new ArgumentException("Requested web driver type is not supported");
92+
var remoteDebuggingPort = await this.ArgumentProcessor.GetValueAsync<string>(this.RemoteDebuggingPort);
93+
webApplicationDetails.Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
94+
webApplicationDetails.Browser = await webApplicationDetails.Playwright.Chromium.ConnectOverCDPAsync($"http://localhost:{remoteDebuggingPort}");
95+
webApplicationDetails.ActiveContext = webApplicationDetails.Browser.Contexts[0];
96+
webApplicationDetails.ActivePage = webApplicationDetails.ActiveContext.Pages[0];
10397
}
98+
else
99+
{
100+
Browsers preferredBrowser = await GetPreferredBrowser(webApplicationDetails);
101+
var browserLaunchOptions = await GetBrowserLaunchOptions(preferredBrowser);
104102

105-
var browserContextOptions = await GetBrowserNewContextOptions();
106-
webApplicationDetails.ActiveContext = await webApplicationDetails.Browser.NewContextAsync(browserContextOptions);
107-
webApplicationDetails.ActivePage = await webApplicationDetails.ActiveContext.NewPageAsync();
103+
await InstallBrowser(preferredBrowser, browserLaunchOptions);
108104

109-
string goToUrl = webApplicationDetails.TargetUri.ToString();
110-
if (this.TargetUriOverride.IsConfigured())
111-
{
112-
goToUrl = await this.ArgumentProcessor.GetValueAsync<string>(this.TargetUriOverride);
113-
logger.Information($"TargetUri was over-ridden to {goToUrl} for application : {applicationDetails.ApplicationName}");
114-
}
115-
await webApplicationDetails.ActivePage.GotoAsync(goToUrl);
105+
webApplicationDetails.Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
106+
107+
switch (preferredBrowser)
108+
{
109+
case Browsers.Chrome:
110+
case Browsers.Edge:
111+
webApplicationDetails.Browser = await webApplicationDetails.Playwright.Chromium.LaunchAsync(browserLaunchOptions);
112+
break;
113+
case Browsers.FireFox:
114+
webApplicationDetails.Browser = await webApplicationDetails.Playwright.Firefox.LaunchAsync(browserLaunchOptions);
115+
break;
116+
117+
case Browsers.WebKit:
118+
webApplicationDetails.Browser = await webApplicationDetails.Playwright.Webkit.LaunchAsync(browserLaunchOptions);
119+
break;
120+
default:
121+
throw new ArgumentException("Requested web driver type is not supported");
122+
}
123+
124+
var browserContextOptions = await GetBrowserNewContextOptions();
125+
webApplicationDetails.ActiveContext = await webApplicationDetails.Browser.NewContextAsync(browserContextOptions);
126+
webApplicationDetails.ActivePage = await webApplicationDetails.ActiveContext.NewPageAsync();
127+
logger.Information("{browserToLaunch} has been launched.", preferredBrowser);\
116128

117-
logger.Information("{browserToLaunch} has been launched.", preferredBrowser);
129+
string goToUrl = webApplicationDetails.TargetUri.ToString();
130+
if (this.TargetUriOverride.IsConfigured())
131+
{
132+
goToUrl = await this.ArgumentProcessor.GetValueAsync<string>(this.TargetUriOverride);
133+
logger.Information($"TargetUri was over-ridden to {goToUrl} for application : {applicationDetails.ApplicationName}");
134+
}
135+
await webApplicationDetails.ActivePage.GotoAsync(goToUrl);
136+
logger.Information("Browser was navigted to {0}", goToUrl);
137+
}
118138
}
119139

120140
/// <summary>

0 commit comments

Comments
 (0)