Interface IGetsProxyWebDriver
- Namespace
- CSF.Extensions.WebDriver.Proxies
- Assembly
- CSF.Extensions.WebDriver.dll
An object which serves as a factory for proxy instances of OpenQA.Selenium.IWebDriver.
public interface IGetsProxyWebDriver
Remarks
Some of the functionality of CSF.Extensions.WebDriver
requires the replacement of the WebDriver
with a proxy object. The proxy is an object which implements all of the same interfaces as the original
WebDriver and behaves in the same way but is augmented with additional functionality.
This proxying process is presently implemented via Castle DynamicProxy: https://www.castleproject.org/projects/dynamicproxy/. This kind of augmentation of objects is typically performed via the decorator pattern: https://en.wikipedia.org/wiki/Decorator_pattern. That pattern is unsuitable here though because it is impossible to know the complete list of interfaces that the original WebDriver object implements until runtime. Without proxies this library would be forced to either:
- Assume that the WebDriver implements all of Selenium's possible interfaces, possibly claiming to implement some interfaces that the WebDriver does not support
- Or implement only a minimal set of interfaces: the lowest common denominator, which would result in a less useful WebDriver object
In addition, a new third-party WebDriver which implements as-yet-unknown interfaces would not be able to expose them from a traditional decorator.
A proxy, which is created at runtime, may use reflection to determine all of the interfaces implemented by the WebDriver and implement those same interfaces, whilst adding support for the extra interfaces that are provided by this library.
In practice, this has no effect upon the returned instance of OpenQA.Selenium.IWebDriver; any casting/safe-casting logic which
relies on Selenium or other interfaces will continue to work as it has done before. Logic such as the following is still
possible if webDriverProxy
is a proxy object.
if(webDriverProxy is IDevTools devToolsDriver)
{
var session = devToolsDriver.GetDevToolsSession();
// ...
}
What will not work with a proxy object is casting back to the original OpenQA.Selenium.IWebDriver implementation class type.
The following would be false if webDriverProxy
were a proxy object:
var amIChrome = webDriverProxy is ChromeDriver;
Using a functionality-providing object via its interfaces and avoiding reliance upon concrete classes is generally best practice across all of software development so well-written code should not be concerned with this limitation. To cope with the rare scenarios in which this limitation is troublesome, all WebDriver proxies are enhanced with the interface IHasUnproxiedWebDriver. This interface provides functionality to get the original 'unproxied' WebDriver instance. There is also an extension method for the OpenQA.Selenium.IWebDriver interface: Unproxy(IWebDriver) to provide convenient and reliable access to that functionality.
Methods
GetProxyWebDriver(IWebDriver, ProxyCreationOptions)
Gets a proxy object implementing OpenQA.Selenium.IWebDriver as well as all of the other interfaces implemented by the
webDriver
, enriching it with additional functionality as determined by the options
.
IWebDriver GetProxyWebDriver(IWebDriver webDriver, ProxyCreationOptions options)
Parameters
webDriver
IWebDriverThe original WebDriver instance to proxy.
options
ProxyCreationOptionsOptions related to the creation of this proxy.
Returns
- IWebDriver
A proxy OpenQA.Selenium.IWebDriver instance.
Remarks
See the remarks upon IGetsProxyWebDriver for more information.