Skip to content

Instantly share code, notes, and snippets.

@HoraceBury
Last active May 8, 2023 08:18
Show Gist options
  • Save HoraceBury/ac78eb235747af39f27e711d6f0c2de0 to your computer and use it in GitHub Desktop.
Save HoraceBury/ac78eb235747af39f27e711d6f0c2de0 to your computer and use it in GitHub Desktop.
Attempt to create multiple selenium edge browser instances in console app
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Selenium.WebDriver" Version="4.9.0" />
<PackageReference Include="WebDriverManager" Version="2.16.2" />
</ItemGroup>
</Project>
List<string> urls = new List<string>{"youtube.com","microsoft.com","twitter.com","news.bbc.co.uk","disney.com"};
List<Tester> testers = new List<Tester>();
var counter = 0;
var inputKey = ' ';
using (var service = Tester.GetService())
{
Console.WriteLine("Press:\n\tq = quit\n\t+ = new browser\n\t- = remove browser");
while (inputKey != 'q')
{
var key = Console.ReadKey();
inputKey = key.KeyChar;
Console.WriteLine();
switch (inputKey)
{
case '+':
var task = new Tester(counter++, service);
testers.Add(task);
await task.TestBrowser($"https://www.{urls.FirstOrDefault()}");
urls.RemoveAt(0);
break;
case '-':
testers[0].Quit = true;
testers.RemoveAt(0);
break;
case 'i':
foreach (var tester in testers)
Console.WriteLine($"Tester: {tester.ToString()}");
break;
}
Console.WriteLine($"\tTesters count: {testers.Count}");
}
}
Console.WriteLine("Ended");
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
// Requires msedgedriver.exe from 'https://msedgedriver.azureedge.net/' + $edgeVersion + '/edgedriver_win64.zip'
public class Tester
{
private static List<string> Options => new List<string> {
"no-sandbox",
"inprivate",
"disable-gpu",
"ignore-certificate-errors",
"no-default-browser-check",
"disable-web-security",
"allow-insecure-localhost",
"allow-running-insecure-content",
"acceptInsecureCerts=true",
"--log-level=3",
"--disable-logging",
"proxy-server='direct://'",
"proxy-bypass-list=*",
"disable-extensions",
"disable-infobars",
"remote-debugging-port=9222",
"--window-size=1920,1080",
};
private static readonly object padlock = new Object();
private int ProfileCounter { get; set; } = 0;
private EdgeDriverService? Service { get; set; }
private WebDriver? Driver { get; set; }
public bool Quit { get; set; } = false;
public Task? MyTask { get; set; }
public Tester(int counter, EdgeDriverService service)
{
ProfileCounter = counter;
Service = service;
Console.WriteLine($"Tester {counter} instantiated");
}
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");
});
}
public override string ToString() => $"Counter: {ProfileCounter} \tDriverID: {Driver?.SessionId}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment