0

I am trying to build a utility to run multiple Edge windows via Selenium 4. While I can do this in a single tasked model (as seen here) doing it in a parallel model (as the aim is to run multiple headless browsers under xUnit) is failing. This is because every new driver appears to start a new, rather flaky, browser but sends its Navigate command to the first window.

The following code (also available here) is designed to be a console app which allows key inputs to create new browser windows and outputs this if 3 are started:

Creating Edge Driver Service
Microsoft Edge WebDriver was started successfully.
Created Edge Driver Service
Press:
        q = quit
       + = new browser
        - = remove browser
+
Tester 0 instantiated
        Testers count: 1
Starting 0 with https://www.youtube.com
Creating Edge Driver
Creating Edge options
Created Edge options
 
DevTools listening on ws://127.0.0.1:9222/devtools/browser/5e485cab-07e6-4d18-ae68-94537adfa423
Created Edge Driver
Started 0 with https://www.youtube.com
+
Tester 1 instantiated
        Testers count: 2
Starting 1 with https://www.microsoft.com
Creating Edge Driver
Creating Edge options
Created Edge options
 
DevTools listening on ws://[::1]:9222/devtools/browser/0df00805-09ee-4b7c-85ec-824fa47bae77
Created Edge Driver
Started 1 with https://www.microsoft.com
+
Tester 2 instantiated
        Testers count: 3
Starting 2 with https://www.twitter.com
Creating Edge Driver
Creating Edge options
Created Edge options
Created Edge Driver
Started 2 with https://www.twitter.com
-
        Testers count: 2
-
        Testers count: 1
-
        Testers count: 0
Tester 0 ended
Tester 1 ended
q
        Testers count: 0
Ended

I’m trying to figure out why every command goes to the first window to be opened and, secondly (but probably related), why the following windows are so unreliable (they don’t stay open long and disappear if the first is closed):

public static EdgeDriverService GetService()
{
    lock (padlock)
    {
        Console.WriteLine("Creating Edge Driver Service");
        
        var service = EdgeDriverService.CreateDefaultService(@".", @"msedgedriver.exe");
        
        service.Port = 4444;
        service.UseVerboseLogging = false;
        service.EnableVerboseLogging = false;
        service.HideCommandPromptWindow = true;
        service.SuppressInitialDiagnosticInformation = true;
        service.Start();

        Console.WriteLine("Created Edge Driver Service");

        return service;
    }
}

private EdgeOptions GetOptions()
{
    Console.WriteLine("Creating Edge options");

    var options = new EdgeOptions();
    options.LeaveBrowserRunning = false;
    options.AcceptInsecureCertificates = true;
    options.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";

    Options.ForEach(arg => { if (!String.IsNullOrEmpty(arg)) options.AddArgument(arg); });
    options.AddArgument($@"--user-data-dir={Directory.GetCurrentDirectory()}\prof-{ProfileCounter}");
    
    options.SetLoggingPreference(LogType.Driver, LogLevel.Off);

    Console.WriteLine("Created Edge options");

    return options;
}

private EdgeDriver GetEdgeDriver()
{
    lock (padlock)
    {
        Console.WriteLine("Creating Edge Driver");
        var driver = new EdgeDriver(Service, GetOptions());
        Console.WriteLine("Created Edge Driver");
        return driver;
    }
}

private RemoteWebDriver GetRemoteDriver(string url = "https://www.google.com")
{
    lock (padlock)
    {
        Console.WriteLine("Creating Remote Driver");
        var driver = new RemoteWebDriver(GetOptions());
        Console.WriteLine("Creatied Remote Driver");
        return driver;
    }
}

public async Task TestBrowser(string url = "https://www.google.com")
{
    MyTask = Task.Run(() => {
        Console.WriteLine($"Starting {ProfileCounter} with \"{url}\"");
        using (var driver = GetEdgeDriver())
        {
            Console.WriteLine($"Started {ProfileCounter} with \"{url}\"");
            Driver = driver;
            driver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 10);
            driver.Manage().Timeouts().PageLoad = new TimeSpan(0, 2, 0);
            driver.Manage().Timeouts().AsynchronousJavaScript = new TimeSpan(0, 0, 10);
            driver.Navigate().GoToUrl(url);
            while (!Quit)
            {
                Thread.Sleep(1000);
            }
            if (driver != null)
                driver.Quit();
        }
        Console.WriteLine($"Tester {ProfileCounter} ended");
    });
}
Matt W
  • 11,753
  • 25
  • 118
  • 215
  • What do you mean by parallel model? Do you mean several tasks to open Edge instances? Can you open them one by one? It seems works fine if I open Edge instances one by one. – Yu Zhou May 10 '23 at 09:18
  • Yes, one edge instance per task because each task has its own driver – Matt W May 10 '23 at 19:34

1 Answers1

0

I can reproduce your issue. I think that's because you defined "remote-debugging-port=9222" in Tester.cs, so the urls are all sent to the first port which is 9222. Just remove this line "remote-debugging-port=9222" then it can work well.

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22