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

fix: Update hashing and iteration logic of page object items #2067

Merged
merged 11 commits into from
Nov 7, 2023
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ tasks.register('uiAutomationTest', Test) {
includeTestsMatching 'io.appium.java_client.android.OpenNotificationsTest'
includeTestsMatching '*.AndroidAppStringsTest'
includeTestsMatching '*.pagefactory_tests.widget.tests.android.*'
includeTestsMatching '*.pagefactory_tests.widget.tests.AndroidPageObjectTest'
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it removed intentionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. The current pattern does not match anything. I've fixed it, but these tests still fail because of the CI slowness (I've verified them locally though). Maybe someone would find some time to make them work in the slow CI env 🤷

includeTestsMatching '*.AndroidPageObjectTest'
includeTestsMatching 'io.appium.java_client.service.local.StartingAppLocallyAndroidTest'
includeTestsMatching 'io.appium.java_client.service.local.ServerBuilderTest'
includeTestsMatching 'io.appium.java_client.service.local.ThreadSafetyTest'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ public ByChained(By[] bys) {
@Override
public WebElement findElement(SearchContext context) {
Function<SearchContext, WebElement> searchingFunction = null;

for (By by: bys) {
searchingFunction = Optional.ofNullable(searchingFunction != null
? searchingFunction.andThen(getSearchingFunction(by)) : null).orElse(getSearchingFunction(by));
searchingFunction = Optional.ofNullable(searchingFunction)
.map(sf -> sf.andThen(getSearchingFunction(by)))
.orElseGet(() -> getSearchingFunction(by));
}

FluentWait<SearchContext> waiting = new FluentWait<>(context);
requireNonNull(searchingFunction);

try {
requireNonNull(searchingFunction);
return waiting.until(searchingFunction);
return new FluentWait<>(context).until(searchingFunction);
} catch (TimeoutException e) {
throw new NoSuchElementException("Cannot locate an element using " + this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ protected abstract Object getObject(

@Override
public Object call(Object obj, Method method, Object[] args, Callable<?> original) throws Throwable {
if (locator == null || Object.class.equals(method.getDeclaringClass())) {
if (locator == null || Object.class == method.getDeclaringClass()) {
return original.call();
}

List<WebElement> realElements = new ArrayList<>(locator.findElements());
final var realElements = new ArrayList<>(locator.findElements());
return getObject(realElements, method, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrapsDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.pagefactory.ElementLocator;

import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.Callable;

public abstract class InterceptorOfASingleElement implements MethodCallListener {
Expand All @@ -42,6 +44,15 @@ public InterceptorOfASingleElement(

protected abstract Object getObject(WebElement element, Method method, Object[] args) throws Throwable;

private static boolean areElementsEqual(Object we1, Object we2) {
if (!(we1 instanceof RemoteWebElement) || !(we2 instanceof RemoteWebElement)) {
return false;
}

return we1 == we2
|| (Objects.equals(((RemoteWebElement) we1).getId(), ((RemoteWebElement) we2).getId()));
}

@Override
public Object call(Object obj, Method method, Object[] args, Callable<?> original) throws Throwable {
if (locator == null) {
Expand All @@ -52,7 +63,7 @@ public Object call(Object obj, Method method, Object[] args, Callable<?> origina
return locator.toString();
}

if (Object.class.equals(method.getDeclaringClass())) {
if (Object.class == method.getDeclaringClass()) {
return original.call();
}

Expand All @@ -62,6 +73,9 @@ public Object call(Object obj, Method method, Object[] args, Callable<?> origina
}

WebElement realElement = locator.findElement();
if ("equals".equals(method.getName()) && args.length == 1) {
return areElementsEqual(realElement, args[0]);
}
return getObject(realElement, method, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
* proxy object here.
*/
public final class ProxyFactory {
private static final Set<String> NON_PROXYABLE_METHODS = setWith(
setWithout(OBJECT_METHOD_NAMES, "toString"),
"iterator"
private static final Set<String> NON_PROXYABLE_METHODS = setWithout(
OBJECT_METHOD_NAMES, "toString", "equals", "hashCode"
);

@SafeVarargs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.pagefactory.HowToUseLocators;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
Expand All @@ -34,16 +35,19 @@
import org.openqa.selenium.support.PageFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ALL_POSSIBLE;
import static java.time.Duration.ofSeconds;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"})
public class AndroidPageObjectTest extends BaseAndroidTest {

private boolean populated = false;
Expand Down Expand Up @@ -149,6 +153,10 @@ public class AndroidPageObjectTest extends BaseAndroidTest {
@FindBy(id = "fakeId")
private List<WebElement> fakeElements;

@FindBy(className = "android.widget.TextView")
@CacheLookup
private List<WebElement> cachedViews;

@CacheLookup
@FindBy(className = "android.widget.TextView")
private WebElement cached;
Expand Down Expand Up @@ -343,8 +351,22 @@ public class AndroidPageObjectTest extends BaseAndroidTest {
assertNotEquals(ArrayList.class, fakeElements.getClass());
}

@Test public void checkCached() {
@Test public void checkCachedElements() {
assertEquals(((RemoteWebElement) cached).getId(), ((RemoteWebElement) cached).getId());
assertEquals(cached.hashCode(), cached.hashCode());
//noinspection SimplifiableAssertion,EqualsWithItself
assertTrue(cached.equals(cached));
}

@Test public void checkCachedLists() {
assertEquals(cachedViews.hashCode(), cachedViews.hashCode());
//noinspection SimplifiableAssertion,EqualsWithItself
assertTrue(cachedViews.equals(cachedViews));
}

@Test public void checkListHashing() {
assertFalse(cachedViews.isEmpty());
assertEquals(cachedViews.size(), new HashSet<>(cachedViews).size());
}

@Test
Expand All @@ -364,6 +386,7 @@ public void checkThatElementSearchingThrowsExpectedExceptionIfChainedLocatorIsIn
assertNotEquals(0, androidElementsViewFoundByMixedSearching.size());
}

@Disabled("FIXME")
@Test public void checkMixedElementSearching2() {
assertNotNull(androidElementViewFoundByMixedSearching2.getAttribute("text"));
}
Expand Down
Loading