Skip to content

Commit 3738903

Browse files
authored
Merge pull request #206 from Nfactor26/browser-plugin-enhancements
Browser plugin enhancements
2 parents 76820cb + fa9999a commit 3738903

File tree

6 files changed

+59
-51
lines changed

6 files changed

+59
-51
lines changed

src/Pixel.Automation.Web.Playwright.Components/Enums/Browsers.cs renamed to src/Pixel.Automation.Web.Common/Browsers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
namespace Pixel.Automation.Web.Playwright.Components;
2+
namespace Pixel.Automation.Web.Common;
33

44
public enum Browsers
55
{

src/Pixel.Automation.Web.Playwright.Components/Pixel.Automation.Web.Playwright.Components.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PackageReference Include="Serilog" Version="2.12.0">
2020
<IncludeAssets>compile</IncludeAssets>
2121
</PackageReference>
22-
<PackageReference Include="Microsoft.Playwright" Version="1.27.2" />
22+
<PackageReference Include="Microsoft.Playwright" Version="1.28.0" />
2323
</ItemGroup>
2424

2525
<ItemGroup>

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>

src/Pixel.Automation.Web.Selenium.Components/Enums/Browsers.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Pixel.Automation.Web.Selenium.Components/Pixel.Automation.Web.Selenium.Components.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0">
3535
<IncludeAssets>compile</IncludeAssets>
3636
</PackageReference>
37-
<PackageReference Include="Selenium.Support" Version="4.5.1" />
38-
<PackageReference Include="Selenium.WebDriver" Version="4.5.1" />
39-
<PackageReference Include="WebDriverManager" Version="2.16.0" />
37+
<PackageReference Include="Selenium.Support" Version="4.7.0" />
38+
<PackageReference Include="Selenium.WebDriver" Version="4.7.0" />
39+
<PackageReference Include="WebDriverManager" Version="2.16.2" />
4040
</ItemGroup>
4141

4242
<ItemGroup>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
using OpenQA.Selenium.Chrome;
33
using OpenQA.Selenium.Edge;
44
using OpenQA.Selenium.Firefox;
5-
using Pixel.Automation.Core;
65
using Pixel.Automation.Core.Arguments;
76
using Pixel.Automation.Core.Components;
87
using Pixel.Automation.Core.Interfaces;
9-
using Pixel.Automation.Web.Selenium.Components.Enums;
108
using System.ComponentModel;
119
using System.ComponentModel.DataAnnotations;
1210
using System.Diagnostics;
13-
using System.Runtime.InteropServices;
1411
using System.Runtime.Serialization;
1512

1613
namespace Pixel.Automation.Web.Selenium.Components;
@@ -115,7 +112,7 @@ public override async Task LaunchAsync()
115112
webApplicationDetails.WebDriver = new EdgeDriver(edgeDriverService, edgeDriverOptions);
116113
break;
117114
default:
118-
throw new ArgumentException("Requested web driver type is not supported");
115+
throw new ArgumentException($"Browser : '{preferredBrowser}' is not supported");
119116
}
120117

121118
logger.Information("{browserToLaunch} has been launched.", preferredBrowser);

0 commit comments

Comments
 (0)