I'm a relatively new QA Engineer working on learning Selenium (in Java) and I want to use page objects to model my pages.
Currently, the way I'm doing it, my page object classes are collections of static variables (By objects for locating page elements) and static methods (for getting the By objects and performing page functions). This seemed like the simplest way to me as my methods don't need to rely on any instance variables, just the locators.
I just call these methods as I need them in my test code.
However, everything I read about page objects talks about instantiating them and having methods return page objects. This seems like it makes everything more complicated. For example, instead of having one method for logging in, I need two, one for if the login succeeds and one for if it fails.
I know it seems to be the accepted best practice, but I want to understand why. Thanks.
Here is my pageObject code, my tests call methods as LoginPage.login(username, password);
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LogInPage {
private static By emailTxtB = By.id("user_email");
private static By passwordTxtB = By.id("user_password");
private static By logInButton =
By.xpath("/html/body/div/div[2]/form/div[2]/div[2]/div/button");
private static By signUpButton = By.xpath("/html/body/div/div[2]/form/div[2]/div[2]/div/a");
private static By valErrorMessage = By.id("flash_alert");
public static void logIn(WebDriver driver, String email, String password){
//Fill out form
driver.findElement(emailTxtB).sendKeys(email);
driver.findElement(passwordTxtB).sendKeys(password);
//Submit form
driver.findElement(logInButton).click();
}
public static void goToSignUp(WebDriver driver){
driver.findElement(signUpButton).click();
}
public static String getValErrorMessage(WebDriver driver){
return driver.findElement(valErrorMessage).getText();
}
public By getEmailTxtB(){
return emailTxtB;
}
public By getPasswordTxtB(){
return passwordTxtB;
}
public By getLogInButton(){
return logInButton;
}
public By getSignUpButton(){
return signUpButton;
}
}