Friday 27 April 2012

Navigation in Page Objects method returning different pages

Page Objects would now appear to be the standard way to approach test automation when using open source tools such as Selenium.

Of the accepted rules, this one can cause problems;





What happens if the login method returns you to different pages dependent on the page you have logged in from or if the login is unsuccessful?

Proposed solutions are to create different methods dependent on the return type;

public HomePage LoginFromHomePage(string email, string password)
{
     .......
     return new HomePage(driver);
}


public PurchaseFlowLoginFromProductPage(string email, string password)
{
     .......
     return new PurchaseFlowPage(driver);
}



public LoginPage UnsuccessfulLogin(string email, string password)
{
     .......
     return new LoginPage(driver);
}






I wasn't happy with this approach, especially if the AUT can return login to lots of different pages.

By using Generics (this example is C# but I assume can be done in Java too), I am able to create one method;

public T Login<T>(string email, string password) 
{
   .....


   return (T)Activator.CreateInstance(typeof(T), base.Driver);
}



I can then write tests like this;

HomePage page = page1.Login<HomePage>("sello", "password");


OR


PurchaseFlowPage page = productPage.Login< PurchaseFlowPage>("sello", "password");


OR


ErrorPage page = page1.Login<ErrorPage>("sello", "");


I think this is a much better way of appraoching page object navigation