Trying to use the @FindBy
annotation my feeling is there is no perfect way to check the presence of an element.
There are similar discussions here or here, but I see only workarounds and nothing like
@FindBy (id = "abc")
private WebElement TestElement;
if (TestElement.isPresent) {...};
I found solutions with @FindBys
, but this way I have to implement an additional @FindBys
for every element I would check the presence within DOM? Not really nice.
Every alternative solution with ExpectedConditions.presenceOf...
or anything using FindElement
needs a locator as parameter instead of a WebElement, correct?
Sure I can build workarounds using e.g. ExpectedConditions.elementToBeClickable(WebElement)
within a try/catch and raise an org.junit.jupiter.api.Assertions.fail
, but this is not a real DOM check.
To me it seems I'm more flexible with classic By
definitions of Page Objects (e.g. private By searchInput = By.xpath("\\input[@class='input_search']");
) instead of using @FindBy
. Using @FindBy I always deal with the WebElement itself and there is no chance to check the presence before, right?
What are best pratice solutions to check the DOM presence of elements in context of page objects using @FindBy
? Or should I stay with By
to be safe.