MicrosoftEdge / EdgeWebDriver

Feedback and discussions about WebDriver for Microsoft Edge
MIT License
55 stars 7 forks source link

Not able to communicate with EdgeDriver on a protected server, it starts, but tests say it did not start #128

Open TricTracT opened 9 months ago

TricTracT commented 9 months ago

We 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
  10. Minimized the code to putting msedgedriver.exe locally and added it into the PATH variables and just use this as code:
    
    using OpenQA.Selenium;
    using OpenQA.Selenium.Edge;
    using Xunit;

namespace SeleniumTestProject { public class BlankSeleniumTest {

    private IWebDriver _driver;

    public BlankSeleniumTest()
    {
        _driver = new EdgeDriver();
    }

    [Fact]
    public void Test1()
    {
        _driver.Navigate().GoToUrl("https://URL/");
    }
}

}


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.
It feels as as if there might be something in GPO rights that is allowing us to start the msedgedriver.exe (or chromedriver.exe for the same matter) but that it is preventing us to communicate with it from the test itself.
We cannot find any documentation stating what might be the issue here.

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 ??

AB#48961740
bwalderman commented 9 months ago

Hi @TricTracT , it sounds like you are able to reach the Edge WebDriver server when launching it manually and then connecting to it through a simple script.

From a quick glance at the Selenium source code, it looks like the problem you are seeing, "Cannot start the driver service on ###", is logged when the driver process never even starts:

https://github.com/SeleniumHQ/selenium/blob/5a97adf9864a346fdd8914cdb1b601c05dd837ac/dotnet/src/webdriver/DriverService.cs#L290

Some policy may be preventing Selenium from launching the driver process on the affected server.

TricTracT commented 9 months ago

Hi @bwalderman,

Thanks for your reply. The thing is, selenium is actually launching the webdriver and from visual studio it is reachable through a powershell script (by using the developer powershell console) and by using the webbrowser. I am using the same check as selenium does in the driverservice. So somehow selenium is not able to reach the service on localhost, but the powershell script (which is also ran from visual studio) is able to reach it.

So what would be the difference in policies that might affect selenium, but does not affect powershell?

So what the webdriver does is this check:

Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri(DriverCommand.Status, UriKind.Relative));
                        using (var response = Task.Run(async () => await httpClient.GetAsync(serviceHealthUri)).GetAwaiter().GetResult())
                        {
                            // Checking the response from the 'status' end point. Note that we are simply checking
                            // that the HTTP status returned is a 200 status, and that the resposne has the correct
                            // Content-Type header. A more sophisticated check would parse the JSON response and
                            // validate its values. At the moment we do not do this more sophisticated check.
                            isInitialized = response.StatusCode == HttpStatusCode.OK && response.Content.Headers.ContentType.MediaType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
}

That HTTP 200 check I've build in powershell and when I execute it at the moment the test is running (and I see the webdriver is started) it gets an HTTP 200 back. Whilst the test in VS crashes moments later (+/- 5 seconds) with the error that it cannot start the driver service. But it did start :-(