首页 > 解决方案 > Winform 中的 Selenium Webdriver 和嵌入式浏览器 - 如何连接 web_driver 实例?

问题描述

我需要将 Selenium Webdriver 的实例连接/创建到我的测试应用程序在 Winform 中打开的 Web 内容。

我的测试框架使用 CodedUI 打开 Winform 应用程序,进一步的测试步骤导致 Winform 应用程序打开另一个 Winform 窗口,其中嵌入了浏览器。我需要创建或绑定一个 Selenium 实例到这个嵌入式浏览器内容。它在嵌入区域中使用 WebViewer/Edge。

注意 - 如果有的话,还没有准备好迁移到 WinAppDriver。

标签: seleniumwebdriver

解决方案


解决了这个问题。我发现 Web 窗格是一个名为 WebView2 的对象。

在其中实现自动化需要以特殊方式启动应用程序:

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) 

您还需要:

最后在您的自动化代码中,当最终出现带有 WebView2 的窗口时,您需要运行此代码以将 selenium webdriver 绑定到 WebView2 窗格:

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)

推荐阅读