4
4
using Pixel . Automation . Core . Interfaces ;
5
5
using System . ComponentModel ;
6
6
using System . ComponentModel . DataAnnotations ;
7
+ using System . Diagnostics ;
7
8
using System . Runtime . Serialization ;
8
9
9
10
namespace Pixel . Automation . Web . Playwright . Components ;
@@ -51,7 +52,14 @@ public class WebApplicationEntity : ApplicationEntity
51
52
[ DataMember ]
52
53
[ Display ( Name = "Install Browser" , GroupName = "Browser Management" , Order = 50 , Description = "Indicates if configured browser should be automatically installed" ) ]
53
54
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
+
55
63
/// <summary>
56
64
/// Get the TargetApplicationDetails and apply any over-rides to it
57
65
/// </summary>
@@ -78,43 +86,55 @@ public override async Task LaunchAsync()
78
86
return ;
79
87
}
80
88
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 ( ) )
89
91
{
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 ] ;
103
97
}
98
+ else
99
+ {
100
+ Browsers preferredBrowser = await GetPreferredBrowser ( webApplicationDetails ) ;
101
+ var browserLaunchOptions = await GetBrowserLaunchOptions ( preferredBrowser ) ;
104
102
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 ) ;
108
104
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 ) ;
116
128
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
+ }
118
138
}
119
139
120
140
/// <summary>
0 commit comments