Is it good to do the field (WebElements) public in the implementation of PageObject?

The fact that some elements of the operation are really simple: click, enter a value, etc.
How bad/good practice to make fields public PageObject?
April 4th 20 at 00:34
1 answer
April 4th 20 at 00:36
It's your own kitchen like decide with colleagues and do it. )
But hold the opinion that in the PageObject should not hold the field and logic.
Here you have a button to throw item in your bag, make a field in PO
By pay = By.id("pay")
(not important By or WebElement or some wrapper). Now a few tests you use it
@Test
publuc void UserShoudBeAdblePay(){
...
driver.findElement(pay).click();
}

And then the application evolves and the button turns into a drop-down where two pukka to throw into your cart and purchase immediately, now you go and shovel all the code adding click to open a drop-down.
But if you will be building a logic in the PO, then the rule will need in 1 place
public void putInBasket() {
driver.findElement(By.id("pay")).click();
 }


public void putInBasket() {
driver.findElement(By.id("drop-down")).click();
driver.findElement(By.id("pay")).click();
 }


Another thing that you may not PageObject tedious if you have UI tests that test that you cannot validate the lower-level tests, there is a chance that the PO in General unnecessary )
I'm a little lost in the part about the fields and logic. Classic PO implies that we are in class initialization, we find the desired elements using @FindBy annotation, well prescribed methods of work with them. You propose to do without these fields? Just methods? Just PO as a service, right? If not difficult, explain, what caused this choice? - Irwin.Effertz commented on April 4th 20 at 00:39
Well, you talked about public fields, then most likely want to manipulate directly in the test with the given fields (well, I see no other reason). I've explained why I think this is a bad approach.
To do or not to do the field a matter of taste, I often just see the declared fields that are used 1 time in the object, for me it's overkill. Generally push in variables everything strange approach. ))
That for me is much more convenient
public class LoginPage {
 public LoginPage setUsername(String username) {
driver.findElement(By.id("username")).sendKeys(username);
 return this; 
}
}

what is
public class LoginPage {
 By usernameLocator = By.id("username");

 public LoginPage setUsername(String username) {
driver.findElement(usernameLocator).sendKeys(username);
 return this; 
}
}

or is it
public class LoginPage {
 @FindBy(id = "username")
 WebElement usernameLocator

 public LoginPage setUsername(String username) {
usernameLocator.sendKeys(username);
 return this; 
}
}

Yes if the item is used many times, it is logical to pull it out into a variable) - aileen_Wolf commented on April 4th 20 at 00:42
@aileen_Wolfand all! Understand the meaning! Overall, I agree! - Irwin.Effertz commented on April 4th 20 at 00:45

Find more questions by tags JavaSelenium