Skip to content

Advanced Browser Capabilities and Options

Jakub Raczek edited this page Apr 25, 2019 · 5 revisions

In some scenarios, you may require more control over the browser capabilities or browser options you set. You can achieve this by using the CapabilitiesSet event or DriverOptionsSet against the DriverContext.

This event fires after all the capabilities or options have bee read from the configuration file, but before the driver is created. It can be useful for creating runtime dependent capabilities or options.

Method CapabilitiesSet was removed in framework 3.1.8, please use DriverOptionsSet instead.

Browser Capabilities

// SpecFlow example

[Binding]
public class ProjectTestBase : TestBase
{
    private readonly ScenarioContext scenarioContext;
    private readonly DriverContext driverContext = new DriverContext();

    /// <summary>
    /// Initializes a new instance of the <see cref="ProjectTestBase"/> class.
    /// </summary>
    /// <param name="scenarioContext"> Scenario Context </param>
    public ProjectTestBase(ScenarioContext scenarioContext)
    {
        if (scenarioContext == null)
        {
            throw new ArgumentNullException("scenarioContext");
        }
        this.scenarioContext = scenarioContext;
        this.driverContext.CapabilitiesSet += DriverContext_CapabilitiesSet; ;
    }
    
    private void DriverContext_CapabilitiesSet(object sender, CapabilitiesSetEventArgs args)
    {
        if (args == null || args.Capabilities == null)
        {
            throw new ArgumentNullException();
        }

        // Check for an existing capability
        if (args.Capabilities.GetCapability("testcapability") != null)
        {
            // Capability Exists
            .... Do something ....
        }
        else
        {
            // Add the capability
            args.Capabilities.SetCapability("testcapability", ..some value..);
        }
    }
}
// NUnit example
public class ProjectTestBase : TestBase
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        private readonly DriverContext
            driverContext = new DriverContext();

        public ProjectTestBase()
        {
            this.driverContext.CapabilitiesSet += this.DriverContext_CapabilitiesSet;
        }

        private void DriverContext_CapabilitiesSet(object sender, CapabilitiesSetEventArgs args)
       {
        if (args == null || args.Capabilities == null)
        {
            throw new ArgumentNullException();
        }

        // Check for an existing capability
        if (args.Capabilities.GetCapability("testcapability") != null)
        {
            // Capability Exists
            .... Do something ....
        }
        else
        {
            // Add the capability
            args.Capabilities.SetCapability("testcapability", ..some value..);
        }
    }
}

Browser Options

// NUnit example
    public class ProjectTestBase : TestBase
    {
        private readonly DriverContext driverContext = new DriverContext();

        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        public ProjectTestBase()
        {
            this.driverContext.DriverOptionsSet += this.DriverContext_DriverOptionsSet;
        }

private void DriverContext_DriverOptionsSet(object sender, DriverOptionsSetEventArgs args)
        {
            if (args == null || args.DriverOptions == null)
            {
                throw new ArgumentNullException();
            }

            args.DriverOptions.SetLoggingPreference("performance", OpenQA.Selenium.LogLevel.All);
            args.DriverOptions.SetLoggingPreference(LogType.Browser, OpenQA.Selenium.LogLevel.All);
        }

Example can be found here

Clone this wiki locally