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

#567 fix #568

Merged
merged 1 commit into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
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
25 changes: 3 additions & 22 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class AppiumDriver<T extends WebElement>
private ExecuteMethod executeMethod;
private final String platformName;
private final String automationName;
private String currentContext;


/**
Expand All @@ -92,35 +91,21 @@ public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) {
locationContext = new RemoteLocationContext(executeMethod);
super.setErrorHandler(errorHandler);
this.remoteAddress = executor.getAddressOfRemoteServer();
final AppiumDriver<?> driver = this;

HasSessionDetails hasSessionDetails = new HasSessionDetails() {
@Override
public Response execute(String driverCommand, Map<String, ?> parameters) {
return driver.execute(driverCommand, parameters);
}

@Override
public Response execute(String driverCommand) {
return driver.execute(driverCommand);
}
};

Object capabilityPlatform1 = getCapabilities().getCapability(PLATFORM_NAME);
Object capabilityAutomation1 = getCapabilities().getCapability(AUTOMATION_NAME);

Object capabilityPlatform2 = capabilities.getCapability(PLATFORM_NAME);
Object capabilityAutomation2 = capabilities.getCapability(AUTOMATION_NAME);

platformName = ofNullable(ofNullable(hasSessionDetails.getPlatformName())
platformName = ofNullable(ofNullable(super.getPlatformName())
.orElse(capabilityPlatform1 != null ? String.valueOf(capabilityPlatform1) : null))
.orElse(capabilityPlatform2 != null ? String.valueOf(capabilityPlatform2) : null);
automationName = ofNullable(ofNullable(hasSessionDetails.getAutomationName())
automationName = ofNullable(ofNullable(super.getAutomationName())
.orElse(capabilityAutomation1 != null ? String.valueOf(capabilityAutomation1) : null))
.orElse(capabilityAutomation2 != null ? String.valueOf(capabilityAutomation2) : null);

this.setElementConverter(new JsonToMobileElementConverter(this, this));
currentContext = getContext();
}

public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
Expand Down Expand Up @@ -352,7 +337,6 @@ public void zoom(int x, int y) {
@Override public WebDriver context(String name) {
checkNotNull(name, "Must supply a context name");
execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name));
currentContext = name;
return this;
}

Expand Down Expand Up @@ -430,9 +414,6 @@ public URL getRemoteAddress() {
}

@Override public boolean isBrowser() {
if (super.isBrowser()) {
return true;
}
return !currentContext.toLowerCase().contains("NATIVE_APP".toLowerCase());
return !getContext().toLowerCase().contains("NATIVE_APP".toLowerCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,5 @@ default String getAutomationName() {
/**
* @return is focus on browser or on native content.
*/
default boolean isBrowser() {
Object browserName = getSessionDetail("browserName");
return browserName != null && !isBlank(String.valueOf(browserName));
}
boolean isBrowser();
}
14 changes: 9 additions & 5 deletions src/main/java/io/appium/java_client/internal/ElementMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.ImmutableMap;

import io.appium.java_client.HasSessionDetails;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.ios.IOSElement;
Expand Down Expand Up @@ -69,14 +70,17 @@ public Class<? extends RemoteWebElement> getElementClass() {
}

/**
* @param platform platform name.
* @param automation automation name.
* @param hasSessionDetails something that implements {@link io.appium.java_client.HasSessionDetails}.
* @return subclass of {@link io.appium.java_client.MobileElement} that convenient to current session details.
*/
public static Class<? extends RemoteWebElement> getElementClass(String platform, String automation) {
public static Class<? extends RemoteWebElement> getElementClass(HasSessionDetails hasSessionDetails) {
if (hasSessionDetails == null) {
return RemoteWebElement.class;
}
ElementMap element = Optional.ofNullable(mobileElementMap.get(String
.valueOf(automation).toLowerCase().trim()))
.orElse(mobileElementMap.get(String.valueOf(platform).toLowerCase().trim()));
.valueOf(hasSessionDetails.getAutomationName()).toLowerCase().trim()))
.orElse(mobileElementMap
.get(String.valueOf(hasSessionDetails.getPlatformName()).toLowerCase().trim()));
if (element == null) {
return RemoteWebElement.class;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ public Object apply(Object result) {

protected RemoteWebElement newMobileElement() {
Class<? extends RemoteWebElement> target;
if (hasSessionDetails.isBrowser()) {
target = getElementClass(null, null);
} else {
target = getElementClass(hasSessionDetails.getPlatformName(),
hasSessionDetails.getAutomationName());
}

target = getElementClass(hasSessionDetails);
try {
Constructor<? extends RemoteWebElement> constructor = target.getDeclaredConstructor();
constructor.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,8 @@ public class AppiumFieldDecorator implements FieldDecorator {
private final String platform;
private final String automation;
private final TimeOutDuration timeOutDuration;
private final HasSessionDetails hasSessionDetails;

private static String extractSessionData(WebDriver driver, Supplier<String> dataSupplier) {
if (driver == null) {
return null;
}

if (!(driver instanceof HasSessionDetails)) {
return null;
}

return String.valueOf(dataSupplier.get());
}

public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut,
TimeUnit timeUnit) {
Expand All @@ -100,10 +90,17 @@ public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut,
*/
public AppiumFieldDecorator(SearchContext context, TimeOutDuration timeOutDuration) {
this.originalDriver = unpackWebDriverFromSearchContext(context);
platform = extractSessionData(originalDriver, () ->
HasSessionDetails.class.cast(originalDriver).getPlatformName());
automation = extractSessionData(originalDriver, () ->
HasSessionDetails.class.cast(originalDriver).getAutomationName());
if (originalDriver == null
|| !HasSessionDetails.class.isAssignableFrom(originalDriver.getClass())) {
hasSessionDetails = null;
platform = null;
automation = null;
} else {
hasSessionDetails = HasSessionDetails.class.cast(originalDriver);
platform = hasSessionDetails.getPlatformName();
automation = hasSessionDetails.getAutomationName();
}

this.timeOutDuration = timeOutDuration;

defaultElementFieldDecoracor = new DefaultFieldDecorator(
Expand Down Expand Up @@ -221,6 +218,6 @@ private Object decorateWidget(Field field) {

private WebElement proxyForAnElement(ElementLocator locator) {
ElementInterceptor elementInterceptor = new ElementInterceptor(locator, originalDriver);
return getEnhancedProxy(getElementClass(platform, automation), elementInterceptor);
return getEnhancedProxy(getElementClass(hasSessionDetails), elementInterceptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.appium.java_client.remote.MobilePlatform;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebElement;

import java.io.File;
import java.util.function.Supplier;
Expand Down Expand Up @@ -49,7 +48,7 @@ public class AndroidElementGeneratingTest extends BaseElementGenerationTest {
}, (by, aClass) -> {
driver.context("WEBVIEW_io.appium.android.apis");
return commonPredicate.test(by, aClass);
}, tagName("a"), RemoteWebElement.class));
}, tagName("a"), AndroidElement.class));
}

@Test public void whenAndroidBrowserIsLaunched() {
Expand All @@ -65,6 +64,6 @@ public class AndroidElementGeneratingTest extends BaseElementGenerationTest {
}, (by, aClass) -> {
driver.get("https://www.google.com");
return commonPredicate.test(by, aClass);
}, className("gsfi"), RemoteWebElement.class));
}, className("gsfi"), AndroidElement.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebElement;

import java.io.File;
import java.util.function.Function;
Expand Down Expand Up @@ -89,15 +88,15 @@ public void whenIOSNativeAppIsLaunched() {
}
});
return commonPredicate.test(by, aClass);
}, className("gsfi"), RemoteWebElement.class));
}, className("gsfi"), IOSElement.class));
}

@Test public void whenIOSBrowserIsLaunched() {
assertTrue(check(serverBrowserCapabilitiesSupplier,
clientBrowserCapabilitiesSupplier, (by, aClass) -> {
driver.get("https://www.google.com");
return commonPredicate.test(by, aClass);
}, className("gsfi"), RemoteWebElement.class));
}, className("gsfi"), IOSElement.class));
}

@Test
Expand All @@ -117,6 +116,6 @@ public void whenIOSNativeAppIsLaunched2() {
}, clientBrowserCapabilitiesSupplier, (by, aClass) -> {
driver.get("https://www.google.com");
return commonPredicate.test(by, aClass);
}, className("gsfi"), RemoteWebElement.class));
}, className("gsfi"), IOSElement.class));
}
}