Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Bill Torchia
Bill Torchia
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IzzyTwinkly

ASKER

Thanks!