0

Why do we declare webelement as Private while creating Pageobjects after FindBy in Selenium Webdriver? what if we declare as Public instead of Private? Can any one answer this question.

@FindBy(id = "uniqName_34_0")
private WebElement username;
  • 2
    Possible duplicate of [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – Guy Mar 20 '17 at 18:40
  • 1
    Do you know the difference between `public` and `private` keywords? I'm not sure exactly what you are asking. Please clarify. – JeffC Mar 20 '17 at 18:54

1 Answers1

0

In your example, should any class other than HomePage be able to work directly with it's username WebElement?

I would argue that you shouldn't have the public accessor method either, especially if you're using the Page Object Pattern.

Your test scripts shouldn't interact directly with WebElements, instead they interact with PageObjects that are responsible for knowing how to perform functions on it's page. So your HomePage might have a method that looks like this:

public HomePage enterUsername(string user)
{
    username.sendKeys(user);
    return this;
}

That way your test script doesn't care how to interact with the page, it's only worried about needing to enter a username. To protect yourself from using WebElements in your test script, don't expose them through public fields or accessor methods.

You might consider marking them protected as needed if you have some inheritance in your PageObject architecture, but other than that your WebElements should be private to the page they belong to.

mrfreester
  • 1,981
  • 2
  • 17
  • 36