0
votes

I'm using @FindBy annotations to locate WebElements. But I want to use these WebElements in methods as parameters. I had success with this only if WebElement was found on page. But if I want to wait for it I must use By (with same locator) instead.

E.g.

private static final String SEARCHFIELD_LOC = "#search input[placeholder]";
@FindBy(css = SEARCHFIELD_LOC)
public WebElement searchField;
public By searchField_by = new By.ByCssSelector(SEARCHFIELD_LOC);

That way I can use this WebElement as By object in some method, like

public boolean isElementDisplayedWithWait(final By by) { .... }

So for each element I have 4 lines of code. I'm to lazy and searching for a way to simplify that.

2

2 Answers

1
votes

I found totally another solution. I will store my locators as strings.

public String searchField = ".slide.slide_search input[placeholder]"

So only 1 line of code per locator. And I hope supporting only CSS locators will be enough. I will NOT use @FindBy for getting WebElements. Instead, I will get WebElements using special method that will wait for element and will log what's going on. I will implement getting WebElement like this:

WebElement we = wait.until(ExpectedConditions.visibilityOfElementLocated(new By.ByCssSelector(locator)));

where wait is

Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
                .withTimeout(timeout, TimeUnit.SECONDS)
                .pollingEvery(1, TimeUnit.SECONDS)
                .ignoring( StaleElementReferenceException.class ) ;
0
votes

The alternate of having wait/assertions/verification methods with webelement itself is available with extended webelement in QAF formerly ISFW. For example:

@FindBy(css = SEARCHFIELD_LOC)
public WebElement searchField;

searchField.waitForPresent();
searchField.assertPresent(); //auto wait
searchField.verifyPresent(); //auto wait

Reference:

  1. QAF Selenium testing-frameworks
  2. ISFW FAQ