selenium

blocking urls in selenium

Sometimes we want to block specific URLs from loading, for example Google Analytics so we don’t artificially inflate our statistics.

readonly string[] _blockUrls = new [] {
    "www.google-analytics.com"
};

public ChromeDriver GetDriver() 
{
    var options = new ChromeOptions();
    var rules = string.Join(',', _blockUrls.Select(r => $"MAP {r} 127.0.0.1"));
    options.AddArgument($"--host-resolver-rules={rules}");
    return new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
}

selenium packages for dotnet core

Selenium.WebDriver Selenium.WebDriver.ChromeDriver Selenium.Chrome.WebDriver - for moving chromedriver.exe into bin DotNetSeleniumExtras.WaitHelpers - for ExpectedConditions

killing long-running processes

When ChromeDriver fails to gracefully terminate, it can hold the build agent from completing the build until it times out (~2 hours). Use this to murder the process on Assembly Cleanup.

[TestClass]
public class Teardown
{
    [AssemblyCleanup]
    static public void AssemblyCleanup()
    {
        foreach (var process in Process.GetProcessesByName("chromedriver"))
        {
            process.Kill();
        }
    }
}

Selenium PageFactory deprecated for .NET Core

See alternate implementation here.

posts