Skip to content

RemoteDriverAttribute doesn't support shared drivers. #7

@martijnburgers

Description

@martijnburgers

The RemoteDriverAttribute doesn't respect the Shared property. It will ignore it by always creating a new driver.

In the code:

public override IEnumerable<IWebDriver> GetDrivers(MethodInfo testMethod)
{
    yield return CreateWebDriver(testMethod, Capability, Hub);
}

Unfortunately sub classing it and implementing the methods as follows doesn't work because of the cache implementation. In my scenario I have created a specific attribute for a Chrome remote driver.

public class CustomChromeRemoteDriverAttribute : RemoteDriverAttribute
{
    public CustomChromeRemoteDriverAttribute() : base(RemoteWebDriverCapability.Chrome)
    {
    }

    public override void ReturnDriver(MethodInfo testMethod, IWebDriver driver)
    {
        this.ReturnDriver(testMethod, driver as RemoteWebDriver);
    }

    public override IEnumerable<IWebDriver> GetDrivers(MethodInfo testMethod)
    {        
        yield return GetOrCreateWebDriver(testMethod, () => CreateRemoteWebDriver(testMethod, Capability, Hub));            
    }

    public RemoteWebDriver CreateRemoteWebDriver(MethodInfo testMethod, RemoteWebDriverCapability capability, string hub)
    {
        var capabilities = CustomDesiredCapabilities.Chrome();

        if (string.IsNullOrEmpty(hub))
        {
            var attr = ReflectionHelper.GetAttribute<RemoteWebDriverHubAddressAttribute>(testMethod);

            if (attr != null)
            {
                hub = attr.Hub;
            }
        }

        return CreateWebDriverInstance(testMethod, hub, capabilities);
    }

    public static RemoteWebDriver CreateWebDriverInstance(MethodInfo testMethod, string hub, ICapabilities capabilities)
    {
        var driver = string.IsNullOrEmpty(hub) ? new RemoteWebDriver(capabilities) : new RemoteWebDriver(new Uri(hub), capabilities, TimeSpan.FromMinutes(60));

        InitializeDriver(testMethod, driver);

        return driver;
    }
}

The solution to get it to work with the current version of SeleniumFixture is to subclass RemoteWebDriver as well and change the CustomChromeRemoteDriverAttribute to

public class CustomChromeRemoteDriverAttribute : RemoteDriverAttribute
{
    public CustomChromeRemoteDriverAttribute() : base(RemoteWebDriverCapability.Chrome)
    {
    }

    public override void ReturnDriver(MethodInfo testMethod, IWebDriver driver)
    {
        this.ReturnDriver(testMethod, driver as CustomChromeRemoteDriver);
    }

    public override IEnumerable<IWebDriver> GetDrivers(MethodInfo testMethod)
    {
        yield return GetOrCreateWebDriver(testMethod, () => CreateRemoteWebDriver(testMethod, Capability, Hub));            
    }

    public CustomChromeRemoteDriver CreateRemoteWebDriver(MethodInfo testMethod, RemoteWebDriverCapability capability, string hub)
    {
        var capabilities = CustomDesiredCapabilities.Chrome();

        if (string.IsNullOrEmpty(hub))
        {
            var attr = ReflectionHelper.GetAttribute<RemoteWebDriverHubAddressAttribute>(testMethod);

            if (attr != null)
            {
                hub = attr.Hub;
            }
        }

        return CreateWebDriverInstance(testMethod, hub, capabilities);
    }

    public static CustomChromeRemoteDriver CreateWebDriverInstance(MethodInfo testMethod, string hub, ICapabilities capabilities)
    {
        var driver = string.IsNullOrEmpty(hub) ? new CustomChromeRemoteDriver(capabilities) : new CustomChromeRemoteDriver(new Uri(hub), capabilities, TimeSpan.FromMinutes(60));

        InitializeDriver(testMethod, driver);

        return driver;
    }
}

The shared instance cache key should not only be the driver type because this will not work in case of multiple remote webdrivers with different capabilities.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions