Are getters needed in POM PageFactory?

887 Views Asked by At

I've got a question. What is a difference between:

@FindBy(id= "submit-button")
WebElement submitButton;

public void submitClick() {
    submitButton.click();
}

and

@FindBy(id= "submit-button")
WebElement submitButton;


public WebElement submitButton() {
    return submitButton;
}

public void submitClick() {
    submitButton().click;
}

Is getter needed using PageFactory? If yes, why we should use it? It is not just handled by initelements method?

1

There are 1 best solutions below

0
On

Getters are used to expose private variables to other classes.

@FindBy(id="submit-button")
private WebElement submitButton;

If you declare submitButton as private (as you should, it's a class member as any other class variable you declare) it can be used only in the current class. If you use it only in the current class (as you should, the button should be part of this page object alone) there is no need for getter.