I am writing a Selenium framework that should be really easy for a tester with relatively little Java knowledge to use and write tests for. In order to keep the framework as user friendly as possible, I've noticed that I am writing a lot of statics so that the tester doesn't have to instantiate the page objects and methods every time. This of course goes against the principles of OOP. Before I get in too deep with this I'd like to see if there are viable alternatives to my approach. I'm including below the login page class that instantiates a webdriver Singleton, has a username/password field, and a login button. There are 2 tests for this page that are run using TestNG. All code to follow:
LoginPage.java
public class LoginPage {
public static WebDriver driver;
private static String username_selector = "username";
private static String password_selector = "password";
private static String login_button_selector = "#loginbutton > input";
private static String fail_message_selector = "error";
public static void goTo(String environment_url){
driver = Driver.getDriver();
driver.get(environment_url);
}
public static void loginAs(String username, String password){
WebElement user = driver.findElement(By.name(username_selector));
user.clear();
user.sendKeys(username);
WebElement pass = driver.findElement(By.name(password_selector));
pass.clear();
pass.sendKeys(password);
WebElement loginBtn = driver.findElement(By.cssSelector(login_button_selector));
loginBtn.click();
}
public static boolean loginErrorDisplayed(){
WebElement failMessageContainer = driver.findElement(By.className(fail_message_selector));
if(failMessageContainer.isDisplayed()){
return true;
}
else return false;
}
public static String getLoginErrorMessage(){
WebElement failMessageContainer = driver.findElement(By.className(fail_message_selector));
String failMessage = failMessageContainer.getText();
return failMessage;
}
}
Driver.java
public class Driver {
public static WebDriver driver = null;
public static WebDriver getDriver(){
if(driver == null){
driver = new FirefoxDriver();
}
return driver;
}
public static void killDriver(){
driver.quit();
}
}
LoginTest.java
public class LoginTest {
@Test(priority = 0)
public void loginfail() {
LoginPage.goTo("http://127.0.0.1:8080/login");
LoginPage.loginAs("wrong username", "wrongpassword");
boolean didLoginFail = LoginPage.loginErrorDisplayed();
Assert.assertTrue(didLoginFail == true, "Bad login was successful");
if (didLoginFail){
LoginPage.getLoginErrorMessage();
}
}
@Test(priority = 1)
public void loginsuccess() {
LoginPage.loginAs("correct_username", "correctpass");
boolean didLoginFail = LoginPage.loginErrorDisplayed();
Assert.assertTrue(didLoginFail == false, "Valid Login was unsuccessful");
}
}