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

chore: Introduce better constructor argument validation for the AppiumFieldDecorator class #2070

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Changes from all commits
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 @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -109,8 +110,7 @@ public AppiumFieldDecorator(SearchContext context) {
*/
AppiumFieldDecorator(WeakReference<SearchContext> 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;
Expand All @@ -123,6 +123,22 @@ contextReference, duration, new WidgetByBuilder(platform, automation)
);
}

@Nonnull
private static WeakReference<WebDriver> 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.getName(), searchContext.getClass().getName()
)
));
}

@Nullable
private String readStringCapability(SearchContext searchContext, String capName) {
if (searchContext == null) {
Expand Down
Loading