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

筅森魡賤 提交于 2019-11-30 05:02:41

问题


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;
    }
}

回答1:


I started to work with Selenium and Page Objects 2 months ago. And I'm also curious about the topic. I decided to go with classes and static methods about 1 month ago. But as a codebase grew up I'm started to think about switching to object instances. Main reason - objects can have state. In object I can check if I'm really working with the correct page. With classes I can only assume that current html matches the class (or bloat my code with asserts in every static method of page object). The other reason - autocomplete. Static methods encourage testers to use classes, not the class variables. So it's getting harder and harder to find correct class to call method from. With objects you'll have to declare a variable if you want to call any method. So you're limited with what you can call. With classes and static methods, you can call any method any time (and fail the test cause the expected html is not available eg). It starts to grow into a problem when you try to teach your team members to use your code. As long as you are the only test writer, it might be ok.




回答2:


As far as I can tell, using static all over the place gets rid of most of the benefits of object-oriented programming. For instance, inheritance and the ability to have multiple objects of the same class now won't work very well. But if it works the way you're doing it, I don't think it should be a problem.




回答3:


Using statics will mean you can't gain the benefits of using constructors to e.g. wait for the page to load or assert that the page has loaded.



来源:https://stackoverflow.com/questions/26870858/why-should-i-be-making-my-page-objects-instantiated-rather-than-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!