IzzyTwinkly
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
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:
In here pageName is a "destination page"
maybe I should provide "WebElementInPageName" as well?
Then, I can call it in UnitTest.cs class like
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);
}
}
}
And the following is VacationPackagesPage.csnamespace 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);
}
}
}
my UnitTest1.cs class look like this:[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
HomePage hp = new HomePage(Repository.Driver);
hp.NavigateToVacationPackagePage();
}
}
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);
}
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);
orhp.NavigateTo(VacationPackagePage, WebElementInPageName);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER