Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    }
}
like image 459
mleewing Avatar asked Nov 11 '14 17:11

mleewing


People also ask

Why static method is not necessary for object creation?

Object is needed for the member variables and methods but static is the application variable or function this one of the reason why object is not needed for the static.

Do I need to instantiate the class to use a static variable?

Static members belong to the class, not to any specific object of that class. Hence they can exist whether or not you instantiate any objects of that class. If they're private, you'll still need to instantiate an object since only objects of that class can access private data.

Why do we use static objects?

The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class.

What is the benefit of making a class static?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.


1 Answers

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.

like image 149
Aleksey Timohin Avatar answered Sep 21 '22 01:09

Aleksey Timohin