Skip to content

Commit 5298e9c

Browse files
chore: Introduce better constructor argument validation for the AppiumFieldDecorator class (appium#2070)
1 parent 89046d5 commit 5298e9c

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
3131
import org.openqa.selenium.support.pagefactory.FieldDecorator;
3232

33+
import javax.annotation.Nonnull;
3334
import javax.annotation.Nullable;
3435
import java.lang.ref.WeakReference;
3536
import java.lang.reflect.Constructor;
@@ -44,6 +45,7 @@
4445
import java.util.List;
4546
import java.util.Map;
4647

48+
import static com.google.common.base.Preconditions.checkNotNull;
4749
import static io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy;
4850
import static io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility.unpackObjectFromSearchContext;
4951
import static io.appium.java_client.remote.options.SupportsAutomationNameOption.AUTOMATION_NAME_OPTION;
@@ -81,8 +83,7 @@ public class AppiumFieldDecorator implements FieldDecorator {
8183
* @param duration is a desired duration of the waiting for an element presence.
8284
*/
8385
public AppiumFieldDecorator(SearchContext context, Duration duration) {
84-
this.webDriverReference = unpackObjectFromSearchContext(context, WebDriver.class)
85-
.map(WeakReference::new).orElse(null);
86+
this.webDriverReference = requireWebDriverReference(context);
8687
this.platform = readStringCapability(context, CapabilityType.PLATFORM_NAME);
8788
this.automation = readStringCapability(context, AUTOMATION_NAME_OPTION);
8889
this.duration = duration;
@@ -109,8 +110,7 @@ public AppiumFieldDecorator(SearchContext context) {
109110
*/
110111
AppiumFieldDecorator(WeakReference<SearchContext> contextReference, Duration duration) {
111112
var cr = contextReference.get();
112-
this.webDriverReference = unpackObjectFromSearchContext(cr, WebDriver.class)
113-
.map(WeakReference::new).orElse(null);
113+
this.webDriverReference = requireWebDriverReference(cr);
114114
this.platform = readStringCapability(cr, CapabilityType.PLATFORM_NAME);
115115
this.automation = readStringCapability(cr, AUTOMATION_NAME_OPTION);
116116
this.duration = duration;
@@ -123,6 +123,22 @@ contextReference, duration, new WidgetByBuilder(platform, automation)
123123
);
124124
}
125125

126+
@Nonnull
127+
private static WeakReference<WebDriver> requireWebDriverReference(SearchContext searchContext) {
128+
var wd = unpackObjectFromSearchContext(
129+
checkNotNull(searchContext, "The provided search context cannot be null"),
130+
WebDriver.class
131+
);
132+
return wd.map(WeakReference::new)
133+
.orElseThrow(() -> new IllegalArgumentException(
134+
String.format(
135+
"No driver implementing %s interface could be extracted from the %s instance. "
136+
+ "Is the provided search context valid?",
137+
WebDriver.class.getName(), searchContext.getClass().getName()
138+
)
139+
));
140+
}
141+
126142
@Nullable
127143
private String readStringCapability(SearchContext searchContext, String capName) {
128144
if (searchContext == null) {

0 commit comments

Comments
 (0)