问题
Main Question: How does one configure selenium for CEFsharp applications in C#?
If anyone is having issues with configuring selenium to CEFsharp applications please take a look at this post. I had a tough time and scoured the internet to make this happen and I want to share this information with others.
My Environment:
- VS 2017
- C# backend/middleware
- Angular frontend
- CEF to encapsulate application
- Application is exe
回答1:
Here is the solution:
Make sure your application uses RemoteDebugging (in main program file):
static void Main()
{
var cefSettings = new CefSettings
{
WindowlessRenderingEnabled = true,
MultiThreadedMessageLoop = true,
BrowserSubprocessPath = @"CefSharp.BrowserSubprocess.exe",
LogSeverity = LogSeverity.Error,
};
cefSettings.CefCommandLineArgs.Add("--disable-pinch", "1");
#if DEBUG
**cefSettings.RemoteDebuggingPort = port#;**
#endif
Cef.Initialize(cefSettings);
If using angular make sure to include (in clientshellwinforms section):
#if DEBUG
_webView.Load("http://localhost:portForAngular");
#endif
To connect to application on selenium use:
public void Main()
{
var chromeDriverService = ChromeDriverService.CreateDefaultService();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("remote-debugging-port=port#");
chromeOptions.DebuggerAddress = "localhost:port#";
driver = new ChromeDriver(chromeDriverService, chromeOptions);
}
Then you should be able to drive through using selenium.
Things to consider:
As long as the application has remote debugging enabled you can open it and then run the selenium functions.
Use chrome://inspect (instead or debugger port) to take full advantage of dev tools.
Nuget:
- Selenium.Chrome.WebDriver 2.34.0 (Old, but gold)
- Selenium.Support & Selenium.WebDriver 3.141.0
- NUnit/NUnit3TestAdapter 3.11/3.13
- DotNetSeleniumExtras.PageObjects 3.11.0
Good luck!
来源:https://stackoverflow.com/questions/55497969/how-to-configure-selenium-for-cef-winforms-applications-exe