Skip to content

CreateFactoryFromConfiguration

Craig Fowler edited this page Feb 1, 2018 · 1 revision

Creating a web driver factory from configuration

One of the key benefits of WebDriverExtras is the ability to bootstrap a web driver factory from nothing but a configuration file, optionally supplemented with environment variables.

This is particularly useful when performing Continuous Integration (CI) which includes cross browser testing. Using this mechanism, a single set of test code may make use of WebDriverExtras in order to get the web driver for it's tests, with no mention our dependency upon which actual browser is to be used.

When supplementing configuration with environment variables it becomes very easy for a CI environment to control the web drivers created on a per-build basis with no code changes at all.

Usage

A helper class GetWebDriverFactory is available to assist in the creation of a web driver in this manner. The example below will read the application configuration and instantiate an appropriate web driver factory instance as well as populating its options. From there it is trivially easy to create the web driver itself.

var factory = GetWebDriverFactory.FromConfiguration();
var webDriver = factory.CreateWebDriver();

Configuration format

Here is an example of the configuration format. This would be placed within your App.config file.

<configuration>
  <configSections>
    <section name="WebDriverFactory" type="CSF.WebDriverExtras.Config.WebDriverFactoryConfigurationSection, CSF.WebDriverExtras" />
  </configSections>
  
  <WebDriverFactory AssemblyQualifiedTypeName="CSF.WebDriverExtras.Factories.ChromeDriverFactory, CSF.WebDriverExtras"
                    EnvironmentVariableSupportEnabled="True">
    <FactoryOptions>
      <!--
          Options here correspond to public settable properties
          on the corresponding 'factory options' type
      -->
      <Property Name="CommandTimeoutSeconds" Value="5" />
    </FactoryOptions>
  </WebDriverFactory>
</configuration>

This configuration indicates that when the GetWebDriverFactory.FromConfiguration() method is used, the factory returned should be an instance of the ChromeDriverFactory. The FactoryOptions collection accepts any number of name/value pairs; each is used to set a public property upon the options type which corresponds to the chosen factory. In the case of the Chrome driver factory, it makes use of LocalChromeOptions, which exposes the 'command timeout seconds' property.

You might also notice the EnvironmentVariableSupportEnabled configuration attribute - we will discuss that below.

Using environment variables

In addition to the XML configuration shown above, the FactoryOptions collection may be supplemented with environment variables. This provides a powerful mechanism by which a CI environment may pass information from its own build configuration into the environment and subsequently into the web driver factory. Experienced testers may use this to create a code-one, test-many-times build matrix whereby CI systems may build and test for a variety of browser configurations with no code changes required.

In order to make use of environment variables, the configuration attribute EnvironmentVariableSupportEnabled must be present and set to True (the default is False). There is an optional attribute named EnvironmentVariablePrefix (set at the same level) which controls a prefix used for environment variables which control the web driver; it defaults to WebDriver_.

When environment variable support is enabled, after configuration values in the FactoryOptions section are used to populate the web driver factory options instance, environment variables which match the configured prefix are also used to do the same (the prefix is stripped in order to get the correct option property name).

Example

Let us assume that the application configuration contains the following, and that environment variable support is enabled with its default prefix.

<Property Name="WebDriverExecutablePath" Value="C:\GoogleChrome.exe" />

The following environment variables are present:

WebDriver_WebDriverExecutablePath="C:\Browsers\Google\Chrome.exe"
WebDriver_CommandTimeoutSeconds="20"
WebDriverPort="8081"

Once the web driver is created from a factory which was built from this configuration, it would use a web driver executable path of "C:\Browsers\Google\Chrome.exe" and it would have a command timeout of 20 seconds. The WebDriverPort setting in the environment would be ignored, because it does not begin with the correct prefix.

Remember that when environment support is enabled, environment variables override the values in the configuration file.

Clone this wiki locally