1

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.

Rainer
  • 1,067
  • 2
  • 14
  • 29
  • I do not know why the ```ExpectedConditions.presenceOf...``` would not work. You need a locator for the element you are checking anyways. I know I am missing something but the question is not clear. – pdrersin Feb 18 '20 at 14:50

1 Answers1

0

This may not be the best solution but you can use findBy and return a list:

List<WebElement> TestElement = driver.findElements(By.id(...));

and check the size of the list elelement each time (in a while loop) to see if it is not empty.

pdrersin
  • 311
  • 3
  • 10
  • sure, but for this again i need the ID. My Question was, can I work with FindBy, hence without ID's or xpath as locator anywhere. – Rainer Feb 18 '20 at 15:14
  • without any locator what are you supposed to find? – pdrersin Feb 18 '20 at 15:32
  • .. main question is: can I work without locators (except the one within FindBy declaration) and without redundant code in terms of checking the presence of elements. – Rainer Feb 18 '20 at 15:50