Selenium Webdriver and Embedded Browser in a Winform - How to connect web_driver instance?

Viewed 630

I need to connect/create an instance of Selenium Webdriver to web content that my application under test opens in a Winform.

My test framework opens the Winform application with CodedUI, and further test steps cause the Winform application to open another Winform window that has a browser embedded in it. I need to create or tie a Selenium instance to this embedded browser content. It's using WebViewer/Edge in the embedded area.

Note - Not yet ready to move to WinAppDriver, if ever.

1 Answers

Resolved this. I found out that the web pane is an object called a WebView2.

Automating inside it requires the application to be launched in a special way:

Dim AppPath As String = "[full path to your app and its name].exe"

Dim edgeOptions As New EdgeOptions

edgeOptions.UseChromium = True
edgeOptions.UseWebView = True

edgeOptions.DebuggerAddress = "localhost:9222"

Dim AppStartInfo = New ProcessStartInfo(AppPath)
         
AppStartInfo.UseShellExecute = False
AppStartInfo.EnvironmentVariables("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS") = "--remote-debugging-port=9222"

Diagnostics.Process.Start(AppStartInfo) 

You also need:

And finally in your automation code, when the window with WebView2 in it finally appears, you need to run this code to tie selenium webdriver to the WebView2 pane:

Dim driver As String = "[path to:]msedgedriver.exe"

Dim edgeOptions As New EdgeOptions

edgeOptions.UseChromium = True
edgeOptions.UseWebView = True

edgeOptions.DebuggerAddress = "localhost:9222"

Dim WebDriver = New EdgeDriver(EdgeDriverService.CreateDefaultService(".", driver), edgeOptions)
Related