How can I create a generalized Navigation method in Selenium with Page Object Model?

Hi,

I have a somewhat challenging problem.
I just created a simple page object model in Selenium with C#. It has the following structure.


This is HomePage.cs
namespace CostcoTravel_UITest.Pages
{
    public class HomePage: BasePage
    {
        private By VacationPackageMenu = By.Id("nav_vacation_packages");
        private By CruisesMenu = By.Id("nav_cruises");
        private By RentalCarsMenu = By.Id("nav_rental_cars");
        private By HotelsMenu = By.Id("nav_hotels");
        public HomePage(IWebDriver _driver) : base(_driver) { }

        public CruisesPage NavigateToCruisesPage()
        {
            Repository.Driver.FindElement(CruisesMenu);
            return new CruisesPage(_driver);
        }
        public VacationPackagesPage NavigateToVacationPackagePage()
        {
            Repository.Driver.FindElement(VacationPackageMenu);
            return new VacationPackagesPage(_driver);
        }
        public RentalCarsPage NavigateToRentalCarsPage()
        {
            Repository.Driver.FindElement(RentalCarsMenu);
            return new RentalCarsPage(_driver);
        }
        public HotelsPage NavigateToHotelsPage()
        {
            Repository.Driver.FindElement(HotelsMenu);
            return new HotelsPage(_driver);
        }
    }
}

Open in new window

And the following is VacationPackagesPage.cs
namespace CostcoTravel_UITest.Pages
{
    public class VacationPackagesPage: BasePage
    {
        private By HomeMenu = By.Id("nav_home");
        public VacationPackagesPage(IWebDriver _driver) : base(_driver) { }

        public HomePage NavigateToHomePage()
        {
            Repository.Driver.FindElement(HomeMenu);
            return new HomePage(_driver);
        }
    }
}

Open in new window

my UnitTest1.cs class look like this:
[TestClass]
    public class UnitTest1
    {               
        [TestMethod]
        public void TestMethod1()
        {            
            HomePage hp = new HomePage(Repository.Driver);
            hp.NavigateToVacationPackagePage();
        }
    }

Open in new window

I know that this is typical way to create Navigation methods in each class like this.
But With this way, I will have too many  Navigation methods.
so, I would like to create a generalized Navigation method in BasePage class and all other classes can use it.
so in UnitTest1.cs class, I can call it by providing page name.
I would look like this:
public [pageName] NavigateTo(pageName){
       repository.Driver.FindElement(WebElementInPageName);
 return new [PageName](_driver);
}

Open in new window

 
In here pageName is a "destination page"
maybe I should provide "WebElementInPageName" as well?

Then, I can call it in UnitTest.cs class like
HomePage hp = new HomePage(Repository.Driver);
 hp.NavigateTo(VacationPackagePage);

Open in new window

or
hp.NavigateTo(VacationPackagePage, WebElementInPageName);

Open in new window

IzzyTwinklyAsked:
Who is Participating?
 
Bill TorchiaConnect With a Mentor Quality AssuranceCommented:
They way that I have handled this is to have the base page object take in parmaters that are then called in the "NavigateTo" function. So in Java (what I work in) it would look like this:

Base page:
public abstract class BasePage
{
  protected WebDriver m_Driver;
  protected String m_BaseUrl = "www.testsite.com";
  protected String m_Path = "";

  public BasePage(WebDriver driver, String baseUrl, String path)
  {
     m_Driver = driver;
     m_baseUrl = baseUrl;
     m_Path = path;
  }

  public BasePage get()
  {
     String url = m_baseUrl + m_Path;
     m_Driver.get(url);
     return this;
  }
}

Open in new window


Every page inherits the base page. So a sample page would look like this:
public class RealPage extends BasePage
{
  public RealPage (WebDriver driver)
  {
     super(driver, "/RealPage.html");
  }
}

Open in new window



Then in the test code I do this:

RealPage realPage = new RealPage(driver);

realPage.get();

Open in new window

2
 
IzzyTwinklyAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.