I am designing a test application (Using NUnit) that will have to navigate (through Selenium Webdriver API) through webpages. I’d like to modelize the site structure using enums to be able to tell methods where to navigate (especially NUnit methods) via this enum.
this.NavigateTo(Page.HomePage);
This is a strong requirement (to have an enum type somewhere) mostly because NUnit cannot be passed non-primitive types. For instance, this syntax is impossible to NUnit because Page has to be primitive or enum:
static class Page{
public static Page HomePage;
public static Page UserAccountPage;
}
/* ... later ... */
[TestCase(Page.HomePage)]
void TestMethod(Page p) { ...
I’d also like to use the same enum-or-the-like to make basic inheritance, like
interface IPage{}
interface IPageNavigatable : Page {}
interface IPageUserRelated : Page {}
interface IWildcardPage : Page {}
enum Page{
HomePage : IPageNavigatable,
UserAccountPage : IPageNavigatable IPageUserRelated,
ErrorPage : /* none */,
AnyPage : IWildcardPage
}
And then, I would be able to make very fun methods like
bool IsCurrentPage(IWildcardPage p);
void LoginThenNavigate(String user, String pw, IPageUserRelated p);
Page WhereAmI(); /* and use this value with IsAssignableFrom */
I know C# forbids enum inheritance (I still don’t understand this choice but this is not my question now) and I’ve read a few workarounds, including a class that recodes from scratch the enum mechanism; and none looks satisfactory.
The best Idea I have is using a complex class with an inner enum and a few public booleans. Not very satisfctory…
public class Page{
enum Pages { /* you know them */}
public Page.Pages InnerPage;
public bool isWildcard, isUserRelated, ...;
public Page(Page.Pages p){
this.InnerPage = p;
switch (p){
case ...:
case ...:
this.isWildcard = true;
break;
/* ... */
}
}
}
/* NUnit calls */
[TestCase(Page.Pages.HomePage)]
void TestMethod(Page.Pages p) { ...
Also, in a perfect world, I’d love to “link” my enum-or-so typed values with Strings or URIs, so that I could write something like:
enum Page{
HomePage : IPageNavigatable = "/index.php",
UserAccountPage : IPageNavigatable IPageUserRelated = "/user.php",
ErrorPage : /* none */ = (String) null,
AnyPage : IWildcardPage = (String) null
}
/* ... */
Page p;
Selenium.Webdriver drv;
drv.Navigate().GoToUrl(new Url(p.ToString()));
This is a tough problem to me. Anyone can help?
EDIT: A clue, as Chris points out below, is that NUnit (through attributes) can take an argument of type Object. Using this, there is probably a way to implement nearly-no-code objects with my inheritance structure, and recode part of the enum mechanism (and link to the URL strings).
Not my dream solution but might work…
Are you sure that they must be primitives (by the way, we call those Value Types or structs in the .net world)? This page seems to imply that the reason that you are limited is because of the inherent limitations in .NET attributes, which in turn are described here:
This suggests to me that the following would work: