Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.appium.java_client.pagefactory.bys.ContentMappedBy;
import io.appium.java_client.pagefactory.bys.ContentType;
import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder;
import io.appium.java_client.pagefactory.bys.builder.ByAll;
import io.appium.java_client.pagefactory.bys.builder.ByChained;
import io.appium.java_client.pagefactory.bys.builder.HowToUseSelectors;
import org.openqa.selenium.By;
Expand All @@ -31,7 +32,6 @@
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;
import org.openqa.selenium.support.pagefactory.ByAll;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import org.openqa.selenium.By;
import org.openqa.selenium.support.pagefactory.AbstractAnnotations;
import org.openqa.selenium.support.pagefactory.ByAll;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.appium.java_client.pagefactory.bys.builder;

import static com.google.common.base.Preconditions.checkNotNull;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;


public class ByAll extends org.openqa.selenium.support.pagefactory.ByAll {

private final List<By> bys;

private Function<SearchContext, Optional<WebElement>> getSearchingFunction(By by) {
return input -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return (input) -> ... is more readable

try {
return Optional.of(input.findElement(by));
} catch (NoSuchElementException e) {
return Optional.empty();
}
};
}

/**
* @param bys is a set of {@link org.openqa.selenium.By} which forms the all possible searching.
*/
public ByAll(By[] bys) {
super(bys);
checkNotNull(bys);

this.bys = Arrays.asList(bys);
if (this.bys.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new IllegalArgumentException("By array should not be empty");
}
}

@Override
public WebElement findElement(SearchContext context) {
return bys.stream()
.map(by -> getSearchingFunction(by).apply(context))
.filter(Optional::isPresent)
.findFirst().map(Optional::get)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapping should be done before findFirst

.orElseThrow(() -> new NoSuchElementException("Cannot locate an element using " + toString()));
}
}