Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ByAll was re-implemented. #680

Merged
merged 6 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,49 @@
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.function.Function;


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

private By[] bys;
Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Jul 28, 2017

Choose a reason for hiding this comment

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

I'd keep it as List<By> internally


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

Choose a reason for hiding this comment

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

I'd mark it with Nullable annotation

Copy link
Contributor

Choose a reason for hiding this comment

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

Or it would be even better to return an Optional value

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 input.findElement(by);
Copy link
Contributor

Choose a reason for hiding this comment

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

return Optional.of(input.findElement(by))

} catch (NoSuchElementException e) {
return null;
}
};
}

/**
* @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);
if (bys.length == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

better to use isEmpty()

throw new IllegalArgumentException("By array should not be empty");
}
this.bys = bys;
}

@Override
public WebElement findElement(SearchContext context) {
for (By by : bys) {
Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Jul 28, 2017

Choose a reason for hiding this comment

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

return bys.stream()
   .map(x -> getSearchingFunction(x).apply(context))
   .filter(Optional::isPresent)
   .findFirst()
   .orElseThrow(() -> new NoSuchElementException("Cannot locate an element using " + toString()));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mykola-mokhnach I don't think that we can use stream in this case, because it returns By type not WebElement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you know how to need to do it without double invoking searching function, show me please.

Copy link
Contributor

Choose a reason for hiding this comment

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

we can. Updated my comment above

WebElement element = getSearchingFunction(by).apply(context);
if (element != null) {
return element;
}
}
throw new NoSuchElementException("Cannot locate an element using " + toString());
}
}