I\'m looking for a way to reuse one NUnit test suite without duplicating the entire suite for each browser. It seems like I would need a new fixture for each browser. Can I send
I recommend an important variation on Lucifer's solution. Have the attribute supply a factory, not a driver, to Fact/Theory methods. Why? Well, when you see all those browser windows pop-up (n x m, where n = number of tests, m = browsers per test), you'll decide you only want them created when the test is actually going to run.
So with updates from prior solution, including some name changes...
using OpenQA.Selenium;
using SeleniumPageObjectsPatternExample.Attributes;
using SeleniumPageObjectsPatternExample.PageObjects;
using Xunit;
using Xunit.Extensions;
namespace SeleniumHelpers
{
public class HomepageTests
{
[Theory]
[WebDriver(DriverType.Firefox)]
[WebDriver(DriverType.GoogleChrome)]
public void HomepageLinksToBlogPage(WebDriverFactory factory)
{
// arrange
IWebDriver webDriver = factory.Create(); // Browser starts here.
var expected = "some expected value";
// act
var homepage = new HomePage(webDriver, true);
// assert
Assert.True(homepage.BlogLink.Displayed);
Assert.Equal(expected, homepage.Header.Text);
// Optional cleanup (better wrapped in try/finally for entire method).
webDriver.Quit();
webDriver.Dispose();
}
}
}
Updated DataAttribute... (Note, I corrected the signature of the GetData() method to work with the latest xUnit.)
using System.Reflection;
using OpenQA.Selenium;
using Xunit.Extensions;
namespace SeleniumHelpers
{
public class WebDriverAttribute : DataAttribute
{
private WebDriverFactory WebDriverFactory { get; set; }
public WebDriverAttribute(Type browser)
{
this.WebDriver = WebDriverFactory.Create(browser);
}
public override IEnumerable
The new WebDriverFactory
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
namespace SeleniumHelpers
{
public enum DriverType
{
Firefox,
GoogleChrome
}
public class WebDriverFactory
{
private DriverType _driverType;
public WebDriverFactory(DriverType driverType)
{
_driverType = driverType;
}
public static IWebDriver Create(Type browser)
{
IWebDriver webDriver;
switch (browser)
{
case Type.Firefox:
webDriver = new FirefoxDriver();
break;
case Type.GoogleChrome:
webDriver = new ChromeDriver();
break;
default:
webDriver = new ChromeDriver();
break;
}
webDriver.Manage().Window.Maximize();
return webDriver;
}
}
}