3030import org .openqa .selenium .support .pagefactory .ElementLocatorFactory ;
3131import org .openqa .selenium .support .pagefactory .FieldDecorator ;
3232
33+ import javax .annotation .Nonnull ;
3334import javax .annotation .Nullable ;
3435import java .lang .ref .WeakReference ;
3536import java .lang .reflect .Constructor ;
4445import java .util .List ;
4546import java .util .Map ;
4647
48+ import static com .google .common .base .Preconditions .checkNotNull ;
4749import static io .appium .java_client .pagefactory .utils .ProxyFactory .getEnhancedProxy ;
4850import static io .appium .java_client .pagefactory .utils .WebDriverUnpackUtility .unpackObjectFromSearchContext ;
4951import 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