5

I am building a test framework for my Website using Selenium, I actually want your ideas of good practices when using Page Object Model: let us say that I have a Welcome page that contains a header where logout button exists, and this header can be seen in most of my pages I am thinking that it is better to write a separate class for the header something like this:

public class Header
    {
    [FindsBy(How = How.Name, Using = "user_profile")]
    public IWebElement BtnUserProfile{ get; set; }

    [FindsBy(How = How.ClassName, Using = "logout_button")]
    public IWebElement BtnLogout { get; set; }

    public void Logout()
     {
        BtnLogout.Click();
     }
}

public class LoginPage
    {
    [FindsBy(How = How.Name, Using = "username")]
    public IWebElement TxtbxUserName { get; set; }

    [FindsBy(How = How.Name, Using = "password")]
    public IWebElement TxtbxPassword { get; set; }

    [FindsBy(How = How.ClassName, Using = "button")]
    public IWebElement BtnSignIn { get; set; }


    public string GoTO()
   {
       Driver.Navigate().GoToUrl(LoginURL);
   }
    public void Login(string username, string password)
     {
        TxtbxUserName.SendKeys(username);
        TxtbxPassword.SendKeys(password);
        BtnSignIn.Click();
     }
    public bool IsAt()
     {
         return Driver.Url == LoginURL;
     }
}

public class WelcomePage
   {
    [FindsBy(How = How.Name, Using = "welcome-message")]
    public IWebElement LblWelcomeMessage { get; set; }
}

My question is do you think it is better to include the Header as a property in the Welcome Page or should they be separated?

Let us take the code for Logout test method, for example.

Case 1:

public void LogoutTest()
{
    LoginPage loginpage= new LoginPage();
    loginpage.GoTo();
    loginpage.login("user","pass");
    Header header= new Header();
    header.Logout();
    Assert.IsTrue(loginpage.IsAt());
}

Case 2:

public void LogoutTest()
{
    LoginPage loginpage= new LoginPage();
    loginpage.GoTo();
    loginpage.login("user","pass");
    WelcomePage wlcmPage= new WelcomePage();
    WelcomePage.Logout();
    Assert.IsTrue(loginpage.IsAt());
}

The second question will be, how do you think about writing a static class for the Driver instead of a separate Driver for every page.

Any ideas will be highly appreciated.

2
  1. My question is do you think it is better to include the Header as a property in the Welcome Page or should they be separated?

    Yes, logically, a header is a property of the welcome page. You can implement a header as a property of a generic page, then inherit from this generic page.

  2. The second question will be, how do you think about writing a static class for the Driver instead of a separate Driver for every page.

    This is a good idea, logically, there is only one driver instance, all we are doing is to pass it different parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.