nl.hsac.fitnesse.fixture.util.selenium.by.FirstElementBy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hsac-fitnesse-fixtures Show documentation
Show all versions of hsac-fitnesse-fixtures Show documentation
Fixtures to assist in testing via FitNesse
package nl.hsac.fitnesse.fixture.util.selenium.by;
import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import static nl.hsac.fitnesse.fixture.util.FirstNonNullHelper.firstNonNull;
/**
* By which returns the first non-null result of a sequence of nested By clauses.
* If a nested By returns mutliple elements it uses {@link BestMatchBy#findElement(By, SearchContext)} to select
* the element to use.
*/
public class FirstElementBy extends SingleElementOrNullBy {
private final List byList;
private Function postProcessor;
public FirstElementBy(Function postProcessor, By firstBy, By... bys) {
int size = 1;
if (bys != null) {
size += bys.length;
}
byList = new ArrayList<>(size);
byList.add(firstBy);
Collections.addAll(byList, bys);
setPostProcessor(postProcessor);
}
public FirstElementBy(By firstBy, By... bys) {
this(Function.identity(), firstBy, bys);
}
@Override
public WebElement findElement(SearchContext context) {
return firstNonNull(by -> postProcessor.apply(getWebElement(by, context)), byList);
}
public static WebElement getWebElement(By by, SearchContext context) {
WebElement byResult;
if (by instanceof SingleElementOrNullBy) {
byResult = by.findElement(context);
} else {
byResult = BestMatchBy.findElement(by, context);
}
return byResult;
}
/**
* @return Bys nested inside.
*/
public List getByList() {
return byList;
}
/**
* Sets post processor to apply to each function's/By's result.
* This function should return null when element should not be returned.
* @param postProcessor function to apply to each element found to determine whether it should be returned.
*/
public void setPostProcessor(Function postProcessor) {
this.postProcessor = postProcessor;
}
/**
* @return post processor in use.
*/
public Function getPostProcessor() {
return postProcessor;
}
@Override
public String toString() {
return getByName(getClass()) + byList;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy