Reputation: 9111
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());
}
a second question will be, how do you think about writing a static class for the Driver instead of a separate Driver for every page.
a third one will be how do you recommend to use waits? I am thinking about adding this method to my driver static class
public static void Wait(int seconds)
{
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(seconds));
}
any ideas will be highly appreciated
Upvotes: 1
Views: 528
Reputation: 2502
This is the concept I follow when I work on framework:
Hope that helps!
Upvotes: 3
Reputation: 1400
The Below code is just a representation Code and you may have to add few more lines eg Page Factory etc.
public class driverConfig{
Static WebDriver driver;
public Static WebDriver getDriver{
driver = new WebDriver();
//Navigate to the URL here
return driver;
}
}
public Class PageClass{
public WebDriver driver;
public PageClass(){
driver = driverConfig.getDriver();
}
}
Upvotes: 2