Sunday 15 June 2014

java - Why should I be making my page objects instantiated rather than static? -



java - Why should I be making my page objects instantiated rather than static? -

i'm relatively new qa engineer working on learning selenium (in java) , want utilize page objects model pages.

currently, way i'm doing it, page object classes collections of static variables (by objects locating page elements) , static methods (for getting objects , performing page functions). seemed simplest way me methods don't need rely on instance variables, locators.

i phone call these methods need them in test code.

however, read page objects talks instantiating them , having methods homecoming page objects. seems makes more complicated. example, instead of having 1 method logging in, need two, 1 if login succeeds , 1 if fails.

i know seems accepted best practice, want understand why. thanks.

here pageobject code, tests phone call methods loginpage.login(username, password);

package pageobjects; import org.openqa.selenium.by; import org.openqa.selenium.webdriver; public class loginpage { private static emailtxtb = by.id("user_email"); private static passwordtxtb = by.id("user_password"); private static loginbutton = by.xpath("/html/body/div/div[2]/form/div[2]/div[2]/div/button"); private static signupbutton = by.xpath("/html/body/div/div[2]/form/div[2]/div[2]/div/a"); private static 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){ homecoming driver.findelement(valerrormessage).gettext(); } public getemailtxtb(){ homecoming emailtxtb; } public getpasswordtxtb(){ homecoming passwordtxtb; } public getloginbutton(){ homecoming loginbutton; } public getsignupbutton(){ homecoming signupbutton; } }

as far can tell, using static on place gets rid of of benefits of object-oriented programming. instance, inheritance , ability have multiple objects of same class won't work well. if works way you're doing it, don't think should problem.

java selenium selenium-webdriver pageobjects

No comments:

Post a Comment