SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
29.78k stars 8.02k forks source link

[🐛 Bug]: DriverManager installing wrong version of Edge WebDriver for Automating WebView2 #14042

Open MagdelineNg opened 1 month ago

MagdelineNg commented 1 month ago

What happened?

This is my C# script to initialise the EdgeDriver I am using to automate my WebView2 (I did not use EdgeDriverManager, since the driverManager had issues installing the edgeDriver compatible to the browser). Similar issue to https://github.com/SeleniumHQ/selenium/issues/12958

        using WebDriverManager;
        using WebDriverManager.DriverConfigs.Impl;

       public IWebDriver InitializeEdgeDriver()

        {            

            string webView2Args = Environment.GetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS");

            Console.WriteLine($"WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS: {webView2Args}");   //checked that remote-debugging-port flag is set in env variable 

            var edgeService = EdgeDriverService.CreateDefaultService();

            edgeService.Start();

            var edgeOptions = new EdgeOptions();

            edgeOptions.UseWebView = UseWebView;

            edgeOptions.DebuggerAddress = DebuggerAddress;

            foreach (var argument in IDSDKUIAutomationConfig.EdgeDriver.Arguments)

            {

                edgeOptions.AddArgument(argument);

            }

            return new EdgeDriver(@"C:\Users\admin\Desktop\msedgedriver.exe", edgeOptions);

            // tried using drivermanager but received error for incompatible edge driver and browser    
            //  new DriverManager().SetUpDriver(new EdgeConfig());
            // return new EdgeDriver(edgeOptions);

        }

I am able to run the same script locally without issues, but when I run it though a Jenkins pipeline job, I receive the error below in the logs:

Starting Microsoft Edge WebDriver 125.0.2535.67 (626a82df5fa7d0d88fbb990d069c0dd83672d0dc) on port 57920

To submit feedback, report a bug, or suggest new features, please visit https://github.com/MicrosoftEdge/EdgeWebDriver

Only local connections are allowed.

Please see https://aka.ms/WebDriverSecurity for suggestions on keeping Microsoft Edge WebDriver safe.

Microsoft Edge WebDriver was started successfully.

Starting Microsoft Edge WebDriver 124.0.2478.51 (e23a9b7d0a14cd45f7a5324b61f0c57561757bb3) on port 57932

To submit feedback, report a bug, or suggest new features, please visit https://github.com/MicrosoftEdge/EdgeWebDriver

Only local connections are allowed.

Please see https://aka.ms/WebDriverSecurity for suggestions on keeping Microsoft Edge WebDriver safe.

Microsoft Edge WebDriver was started successfully.

Unhandled exception. OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:57932/session timed out after 60 seconds.

---> System.Threading.Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.

---> System.TimeoutException: The operation was canceled.

---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.

---> System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request..

---> System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.

--- End of inner exception stack trace ---

at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)

at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)

at System.Net.Http.HttpConnection.InitialFillAsync(Boolean async)

at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)

--- End of inner exception stack trace ---

at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)

at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)

at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)

at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)

--- End of inner exception stack trace ---

--- End of inner exception stack trace ---

at System.Net.Http.HttpClient.HandleFailure(Exception e, Boolean telemetryStarted, HttpResponseMessage response, CancellationTokenSource cts, CancellationToken cancellationToken, CancellationTokenSource pendingRequestsCts)

at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)

at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)

at OpenQA.Selenium.Remote.HttpCommandExecutor.<>c__DisplayClass33_0.<b__0>d.MoveNext()

--- End of stack trace from previous location ---

at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)

--- End of inner exception stack trace ---

at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)

at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)

at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)

at OpenQA.Selenium.WebDriver.StartSession(ICapabilities capabilities)

at OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities)

at OpenQA.Selenium.Chromium.ChromiumDriver..ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)

at OpenQA.Selenium.Edge.EdgeDriver..ctor(EdgeDriverService service, EdgeOptions options, TimeSpan commandTimeout)

at OpenQA.Selenium.Edge.EdgeDriver..ctor(String edgeDriverDirectory, EdgeOptions options, TimeSpan commandTimeout)

at OpenQA.Selenium.Edge.EdgeDriver..ctor(String edgeDriverDirectory, EdgeOptions options)

at UIAutomation.AutomationActions.InitializeEdgeDriver() in E:\ui_tests\builds\scripts\win\

UIAutomation

\IMScreenAutomation\AutomationActions.cs:line 69

at IDSDKUIAutomation.IMScreenAutomation.

g__Test2|3_1() in

E:\ui_tests\builds\scripts\win\UIAutomation\IMScreenAutomation\IMScreenAutomation.cs:line 48

at IDSDKUIAutomation.IMScreenAutomation.Main(String[] args) in

E:\ui_tests\builds\scripts\win\UIAutomation\IMScreenAutomation\IMScreenAutomation.cs:line 27

From the logs, I believe the WebDriver was started correctly but timed out. Two web drivers are started, 124.0.2478.51 and 125.0.2535.67 on different ports. Why is that so?

How can we reproduce the issue?

public IWebDriver InitializeEdgeDriver()

       {            
           TerminateProcess("Widgets.exe");
           string webView2Args = Environment.GetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS");

           Console.WriteLine($"WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS: {webView2Args}");   //checked that remote-debugging-port flag is set in env variable 

           var edgeService = EdgeDriverService.CreateDefaultService();

           edgeService.Start();

           var edgeOptions = new EdgeOptions();

           edgeOptions.UseWebView = UseWebView;

           edgeOptions.DebuggerAddress = DebuggerAddress;

           foreach (var argument in IDSDKUIAutomationConfig.EdgeDriver.Arguments)

           {

               edgeOptions.AddArgument(argument);

           }

           return new EdgeDriver(@"C:\Users\admin\Desktop\msedgedriver.exe", edgeOptions);

       }

        public void TerminateProcess(string processName)
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    Arguments = $"/C taskkill /IM \"{processName}\" /F 2> NUL",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true
                }
            };

            process.Start();
            process.WaitForExit();
            Console.WriteLine($"Terminated {processName}");
        }

       public static void Main(string[] args){
            static void Test1()
            {
                IWebDriver edgeDriver = automationActions.InitializeEdgeDriver();
                edgeDriver.Quit();
            }
       }

Relevant log output

Shown above.

Operating System

Win11

Selenium version

4.20.0

What are the browser(s) and version(s) where you see this issue?

Not sure which Edge browser Selenium is detecting

What are the browser driver(s) and version(s) where you see this issue?

EdgeDriver

Are you using Selenium Grid?

No

github-actions[bot] commented 1 month ago

@MagdelineNg, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] commented 1 month ago

We need more information about this issue in order to troubleshoot.

Please turn on logging and re-run your code. Information on how to adjust logs for your language can be found in our Troubleshooting documentation.