From 66f3706c90029c6f17b898307ca78cbc5708dd84 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 6 Nov 2023 13:24:07 +0100 Subject: [PATCH 1/3] chore: Introduce better constructor argument validation for the AppiumFieldDecorator class --- .../pagefactory/AppiumFieldDecorator.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java index 280cd02d1..078170a92 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java @@ -30,6 +30,7 @@ import org.openqa.selenium.support.pagefactory.ElementLocatorFactory; import org.openqa.selenium.support.pagefactory.FieldDecorator; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.lang.ref.WeakReference; import java.lang.reflect.Constructor; @@ -44,6 +45,7 @@ import java.util.List; import java.util.Map; +import static com.google.common.base.Preconditions.checkNotNull; import static io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy; import static io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility.unpackObjectFromSearchContext; import static io.appium.java_client.remote.options.SupportsAutomationNameOption.AUTOMATION_NAME_OPTION; @@ -81,8 +83,7 @@ public class AppiumFieldDecorator implements FieldDecorator { * @param duration is a desired duration of the waiting for an element presence. */ public AppiumFieldDecorator(SearchContext context, Duration duration) { - this.webDriverReference = unpackObjectFromSearchContext(context, WebDriver.class) - .map(WeakReference::new).orElse(null); + this.webDriverReference = requireWebDriverReference(context); this.platform = readStringCapability(context, CapabilityType.PLATFORM_NAME); this.automation = readStringCapability(context, AUTOMATION_NAME_OPTION); this.duration = duration; @@ -109,8 +110,7 @@ public AppiumFieldDecorator(SearchContext context) { */ AppiumFieldDecorator(WeakReference contextReference, Duration duration) { var cr = contextReference.get(); - this.webDriverReference = unpackObjectFromSearchContext(cr, WebDriver.class) - .map(WeakReference::new).orElse(null); + this.webDriverReference = requireWebDriverReference(cr); this.platform = readStringCapability(cr, CapabilityType.PLATFORM_NAME); this.automation = readStringCapability(cr, AUTOMATION_NAME_OPTION); this.duration = duration; @@ -123,6 +123,22 @@ contextReference, duration, new WidgetByBuilder(platform, automation) ); } + @Nonnull + private static WeakReference requireWebDriverReference(SearchContext searchContext) { + var wd = unpackObjectFromSearchContext( + checkNotNull(searchContext, "The provided search context cannot be null"), + WebDriver.class + ); + return wd.map(WeakReference::new) + .orElseThrow(() -> new IllegalArgumentException( + String.format( + "No driver implementing %s interface could be extracted from the %s instance. " + + "Is the provided search context valid?", + WebDriver.class, searchContext.getClass().getName() + ) + )); + } + @Nullable private String readStringCapability(SearchContext searchContext, String capName) { if (searchContext == null) { From d527b6bbf0eff8b4e7f0c6c4c0e6c3639ad2f80e Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 6 Nov 2023 13:26:54 +0100 Subject: [PATCH 2/3] use name --- .../io/appium/java_client/pagefactory/AppiumFieldDecorator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java index 078170a92..cad96313d 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java @@ -134,7 +134,7 @@ private static WeakReference requireWebDriverReference(SearchContext String.format( "No driver implementing %s interface could be extracted from the %s instance. " + "Is the provided search context valid?", - WebDriver.class, searchContext.getClass().getName() + WebDriver.class.getName(), searchContext.getClass().getName() ) )); } From 4792d6539f8415d1506f9fa9b4fe9abcf23abd36 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 6 Nov 2023 14:11:16 +0100 Subject: [PATCH 3/3] checkstyle --- .../appium/java_client/pagefactory/AppiumFieldDecorator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java index cad96313d..05fa41a42 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java @@ -132,8 +132,8 @@ private static WeakReference requireWebDriverReference(SearchContext return wd.map(WeakReference::new) .orElseThrow(() -> new IllegalArgumentException( String.format( - "No driver implementing %s interface could be extracted from the %s instance. " + - "Is the provided search context valid?", + "No driver implementing %s interface could be extracted from the %s instance. " + + "Is the provided search context valid?", WebDriver.class.getName(), searchContext.getClass().getName() ) ));