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 WebElement
s should be private to the page they belong to.