I am a relatively new QA Engineer that is working on learning Selenium (in Java), and I want to use page objects to model my pages.
Currently, as I do this, my page object classes are collections of static variables (by objects to determine page elements) and static methods (to get By objects and execute page functions). This seemed to me the easiest way, since my methods should not rely on instance variables, namely locators.
I just call these methods as I need them in my test code.
However, everything I read about page objects says about their creation and methods that return page objects. Everything seems to complicate the situation. For example, instead of having one method for logging in, I need two, one for the login to be completed successfully, and the other for failure.
I know this is the best practice, but I want to understand why. Thank.
Here is my page code, my tests call methods like 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){
driver.findElement(emailTxtB).sendKeys(email);
driver.findElement(passwordTxtB).sendKeys(password);
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;
}
}
source
share