Created
November 12, 2020 23:27
-
-
Save shivanshu3/5ccb7839bed913a4181f2e8876b2004e to your computer and use it in GitHub Desktop.
Open N instances of a URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
using System; | |
using System.Linq; | |
using System.Threading; | |
namespace HelloCSharp | |
{ | |
class Program | |
{ | |
static IWebDriver OpenUrl(string driverDirectory, string url) | |
{ | |
IWebDriver driver = new ChromeDriver(driverDirectory); | |
var navigator = driver.Navigate(); | |
try | |
{ | |
navigator.GoToUrl(url); | |
} | |
catch (WebDriverException ex) | |
{ | |
Console.WriteLine($"Failed to load page: {ex.Message}"); | |
} | |
return driver; | |
} | |
static void DoClick(IWebDriver driver) | |
{ | |
var buttonElement = driver.FindElement(By.Id("foo")); | |
buttonElement.Click(); | |
} | |
static void DoClickWithRetries(IWebDriver driver) | |
{ | |
const int numRetries = 5; | |
for (int i = 0; i < numRetries; ++i) | |
{ | |
try | |
{ | |
DoClick(driver); | |
return; | |
} | |
catch (InvalidElementStateException ex) | |
{ | |
Thread.Sleep(1000); | |
} | |
} | |
} | |
static int Main(string[] args) | |
{ | |
if (args.Length != 3) | |
{ | |
Console.WriteLine("Usage: <exe> <driver_dir> <url> <num_instances>"); | |
return 1; | |
} | |
var driverDirectory = args[0]; | |
var url = args[1]; | |
var numInstances = int.Parse(args[2]); | |
Console.WriteLine($"Opening {numInstances} instances of the URL: {url}"); | |
Enumerable.Range(0, numInstances) | |
.AsParallel() | |
.ForAll(x => | |
{ | |
Console.WriteLine($"Thread #{x} running..."); | |
var driver = OpenUrl(driverDirectory, url); | |
DoClickWithRetries(driver); | |
}); | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment