Here is an example of some C# WebDriver code I'm using to model a registration page:
public class RegistrationPage : BasePage
{
private IWebDriver driver;
private String URL = "http://mydomain.com/registration.aspx";
// private String pageTitle = "Registration";
public RegistrationPage(IWebDriver driver) : base(driver)
{
this.driver = driver;
/*
// if we are not on the Registration page
if (!(driver.Title.ToString().Equals(pageTitle)))
{
throw new Exception(); // Need a more relevant exception
}*/
}
override public String URLproperty
{
get { return URL; }
set { URL = value; }
}
// CODE OMITTED ...
}
Here is a method from my BasePage, which RegistrationPage uses:
// Open the URL specified in the implementation of the class
public virtual void Open()
{
driver.Navigate().GoToUrl(this.URLproperty);
}
And finally here is an example of my test code:
[Test]
public void SubmitRegistrationWithBlankUserDetails()
{
RegistrationPage regPage = new RegistrationPage(driver);
regPage.Open();
regPage.ClickRegisterButton();
Assert.IsTrue(regPage.SearchForText("You must enter a first name"));
}
The issue I'm having is implementing the title "sanity check" in the constructor (currently commented out) for RegistrationPage. I've seen this used on the WebDriver wiki (here, code in Java I think: http://code.google.com/p/selenium/wiki/PageObjects), but I'm unsure how to go about it.
I see two possible scenarios when running my tests:
- My test's [SetUp] points the driver to mydomain.com. A link clicked by WebDriver (for example a link to my Registration page, above) passes the driver to the constructor of whatever page I am visiting. Job done, title sanity check can work.
- Let's say instead of clicking links to get to a certain page I want to start my test at the Registration page. I don't have a neat way of visiting that page without navgiating from the domain specified in [Setup]. My [SetUp] doesn't help (unless I navigate manually to the page) and creating a new PageObject of the Registration page doesn't take me anywhere unless I call the Open() method.
Should I simply create a new test plan for the second scenario, one where [Setup] points me to mydomain.com/registration.aspx? Am I misunderstanding something fundamental here?