Edge not starting after msedgedriver.exe is started on 'protected' server (C# / Selenium)

217 views Asked by At

I have a testautomation framework which is working fine on my own system. It uses drivermanager to fetch the right version of msedgedriver.exe and then is supposed to launch edge and starts testing with the scripts. This works fine on my own laptop.

This code is now on a server of a client of mine. The server has Windows Server 2019 installed and is behind a proxy.The user has local administrator rights. On this server, when I launch a test with VS 2022 msedgedriver.exe starts (taskmanager) but the test fails:

Message: 
OpenQA.Selenium.WebDriverException : Cannot start the driver service on http://localhost:53325/

Stack Trace: 
DriverService.Start()
DriverServiceCommandExecutor.Execute(Command commandToExecute)
WebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) 
WebDriver.StartSession(ICapabilities desiredCapabilities) 
WebDriver.ctor(ICommandExecutor executor, ICapabilities capabilities) 
ChromiumDriver.ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout) 
EdgeDriver.ctor(EdgeDriverService service, EdgeOptions options, TimeSpan commandTimeout) 
EdgeDriver.ctor(EdgeDriverService service, EdgeOptions options) BrowserFeature.OpenBrowser() line 109

The msedgedriver.exe is reachable through edge on http://localhost:53325/ (or any other port it starts) but Edge itself never starts.

The code used to start:

var edgeOptions = new EdgeOptions();
edgeOptions.Proxy = proxy;
edgeOptions.SetLoggingPreference(LogType.Driver, LogLevel.All);
edgeOptions.AddArguments("--no-sandbox");

var service = EdgeDriverService.CreateDefaultService();
service.UseVerboseLogging = true;
edgeOptions.AddUserProfilePreference("download.default_directory", Settings.DownloadFolder);
                   
Driver = new EdgeDriver(service, edgeOptions);

What we've already tried:

  1. Running VS 2022 in admin modus
  2. Adding HTTP_PROXY, HTTPS_PROXY and NO_PROXY to the system variables with HTTP_PROXY and HTTPS_PROXY filled with the proxy server and NO_PROXY filled with localhost and 127.0.0.1
  3. Checked whether msedgedriver is already running or is started multiple times. This is not the case.
  4. Added proxyserver in the code, even for the DriverManager. This should not be the issue since msedgedriver is downloaded correctly and DriverManager takes the HTTP_PROXY and HTTPS_PROXY values from the system.
  5. Dismissing the DriverManager and integrating the correct driver (and starting it) directly
  6. Point 5 with Chrome instead of edge which results in the exact same error
  7. Driver versioning combined with the version of the driver is correct
  8. Windows firewall has been killed in the hopes that that was blocking something
  9. Added the DeveloperToolsAvailability variable to the registry wity value "1" so that developertools is enabled for MSEdge

We are kind of clueless since the same code is working on my own system and on another server of my own. But it seems that at the client there is something configured that is blocking the start of the browser, whether it is Chrome or Edge. My take is that either the start of the browser is blocked, or the communication with the driver (which apparently is running since I can see it in edge and chrome (the localhost: is reachable).

But with everything we have already tried we are kind of in the dark.

I dove into this further and came across the following code which checks whether the start of the driver was succesfull (part of OpenQA.Selenium.DriverService):

       protected virtual bool IsInitialized
        {
            get
            {
                bool result = false;
                try
                {
                    HttpClient httpClient = new HttpClient();
                    try
                    {
                        httpClient.DefaultRequestHeaders.ConnectionClose = true;
                        httpClient.Timeout = TimeSpan.FromSeconds(5.0);
                        Uri serviceHealthUri = new Uri(ServiceUrl, new Uri(DriverCommand.Status, UriKind.Relative));
                        using HttpResponseMessage httpResponseMessage = Task.Run(async () => await httpClient.GetAsync(serviceHealthUri)).GetAwaiter().GetResult();
                        result = httpResponseMessage.StatusCode == HttpStatusCode.OK && httpResponseMessage.Content.Headers.ContentType!.MediaType!.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
                        return result;
                    }
                    finally
                    {
                        if (httpClient != null)
                        {
                            ((IDisposable)httpClient).Dispose();
                        }
                    }
                }
                catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException)
                {
                    Console.WriteLine(ex.Message);
                    return result;
                }
            }
        }

What i've done next is I fixated the portnumber, created a powershell script doing practically the same as the above. It it sais (even before the test fails with the error OpenQA.Selenium.WebDriverException : Cannot start the driver service on http://localhost:5588/) that it is running.

The powershell script that I am running simultaniously from the developer powershell in Visual Studio:

# Set the URL for the WebDriver service status check
$serviceUrl = "http://localhost:5588/status"

# Attempt to make an HTTP GET request to the service URL
try {
    $response = Invoke-WebRequest -Uri $serviceUrl -TimeoutSec 5

    # Check if the response is successful (status code 200)
    if ($response.StatusCode -eq 200) {
        # Check the content type (case-insensitive)
        $contentType = $response.Headers["Content-Type"]
        if ($contentType -eq "application/json" -or $contentType -ilike "application/json*") {
            Write-Host "WebDriver service is running and responsive."
        } else {
            Write-Host "Unexpected content type: $contentType"
        }
    } else {
        Write-Host "Error: $($response.StatusCode)"
    }
} catch {
    Write-Host "Error: $_"
}

echo $response.StatusCode

Which results in the following response:

WebDriver service is running and responsive.
200

Apparently the check which is being done from the code is not able to reach http://localhost:5588 ??

0

There are 0 answers