Skip to content

Commit

Permalink
Added fix to run safari app with lesser SDK versions than the default…
Browse files Browse the repository at this point in the history
… SDK version, if lower SDK version is requested in IOSCapabilities.
  • Loading branch information
kumaravel-jayakumar authored and Doug Simmons committed Feb 18, 2015
1 parent 2c88918 commit 3f0a5b9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 24 deletions.
27 changes: 25 additions & 2 deletions server/src/main/java/org/uiautomation/ios/IOSServerManager.java
Expand Up @@ -50,6 +50,7 @@ public class IOSServerManager {
private State state = State.stopped;
private Map<String, ServerSideSession.StopCause> reasonByOpaqueKey = new ConcurrentHashMap<>();

private static final String SAFARI_BUNDLE_NAME = "Safari";

public void waitForState(State expected) {
while (getState() != expected) {
Expand Down Expand Up @@ -212,15 +213,23 @@ public IOSRunningApplication findAndCreateInstanceMatchingApplication(
+ ".\n Available apps: "
+ getSupportedApplications());
}
// if more than one matches it returns the last in the list (highest version for MobileSafari)

// If more than one matches it returns the last in the list (highest version for MobileSafari
// if not requested with a particular version of SDK)
APPIOSApplication app = matchingApps.get(matchingApps.size() - 1);
AppleLanguage lang = AppleLanguage.create(desiredCapabilities.getLanguage());
return app.createInstance(lang);
}

public List<APPIOSApplication> findAllMatchingApplications(IOSCapabilities desiredCapabilities) {
List<APPIOSApplication> matchingApps = new ArrayList<>();

if (isSafariRequestedWithSDKVersion(desiredCapabilities)) {
if (log.isLoggable(Level.INFO)) {
log.log(Level.INFO, "Safari application requested for SDK version: " + desiredCapabilities.getSDKVersion());
}
matchingApps.add(getSafariForSdkVersion(desiredCapabilities.getSDKVersion()));
return matchingApps;
}
for (APPIOSApplication app : getApplicationStore().getApplications()) {
IOSCapabilities appCapabilities = app.getCapabilities();
if (APPIOSApplication.canRun(desiredCapabilities, appCapabilities)) {
Expand All @@ -230,6 +239,20 @@ public List<APPIOSApplication> findAllMatchingApplications(IOSCapabilities desir
return matchingApps;
}

private boolean isSafariRequestedWithSDKVersion(IOSCapabilities desiredCapabilities) {
return desiredCapabilities.getSDKVersion() != null && !desiredCapabilities.getSDKVersion().isEmpty()
&& SAFARI_BUNDLE_NAME.equalsIgnoreCase(desiredCapabilities.getBundleName());
}

private APPIOSApplication getSafariForSdkVersion(String sdkVersion) {
for (APPIOSApplication application : getApplicationStore().getApplications()) {
if (application.isSafari() && sdkVersion.equalsIgnoreCase(application.getSafariSDKVersion())) {
return application;
}
}
throw new WebDriverException("Cannot find Safari for SDK version: " + sdkVersion);
}

public Device findAndReserveMatchingDevice(IOSCapabilities desiredCapabilities) {
List<Device> devices = getDeviceStore().getDevices();
for (Device device : devices) {
Expand Down
Expand Up @@ -14,24 +14,10 @@

package org.uiautomation.ios.application;

import com.google.common.collect.ImmutableList;
import com.dd.plist.BinaryPropertyListWriter;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSNumber;
import com.dd.plist.NSObject;
import com.dd.plist.PropertyListParser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriverException;
import org.uiautomation.ios.IOSCapabilities;
import org.uiautomation.ios.communication.device.DeviceType;
import org.uiautomation.ios.utils.IOSVersion;
import org.uiautomation.ios.utils.PlistFileUtils;
import static org.uiautomation.ios.IOSCapabilities.BUNDLE_ICONS;
import static org.uiautomation.ios.IOSCapabilities.DEVICE_FAMILLY;
import static org.uiautomation.ios.IOSCapabilities.ICON;
import static org.uiautomation.ios.IOSCapabilities.MAGIC_PREFIX;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -47,11 +33,27 @@
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.uiautomation.ios.IOSCapabilities.BUNDLE_ICONS;
import static org.uiautomation.ios.IOSCapabilities.DEVICE_FAMILLY;
import static org.uiautomation.ios.IOSCapabilities.ICON;
import static org.uiautomation.ios.IOSCapabilities.MAGIC_PREFIX;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriverException;
import org.uiautomation.ios.IOSCapabilities;
import org.uiautomation.ios.communication.device.DeviceType;
import org.uiautomation.ios.utils.IOSVersion;
import org.uiautomation.ios.utils.PlistFileUtils;

import com.dd.plist.BinaryPropertyListWriter;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSNumber;
import com.dd.plist.NSObject;
import com.dd.plist.PropertyListParser;
import com.google.common.collect.ImmutableList;

public class APPIOSApplication {

Expand All @@ -62,6 +64,12 @@ public class APPIOSApplication {
private final ImmutableList<LanguageDictionary> dictionaries;
private final ImmutableList<AppleLanguage> languages;

// Pattern to match '7.1' in the text '/Users/unclejohn/.ios-driver/safariCopies/safari-7.1.app'
// or '8.1' in the text
// '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk/Applications/MobileSafari.app'
private static final Pattern SAFARI_SDK_VERSION = Pattern
.compile("\\S+safari-([\\d\\.]+)\\.app|\\S+iPhoneSimulator([\\d\\.]+).sdk\\S+/MobileSafari.app");

/**
* @param pathToApp
* @throws WebDriverException
Expand Down Expand Up @@ -604,4 +612,30 @@ public static APPIOSApplication createFrom(File app) {
return null;
}
}

/**
* Returns the Safari SDK version from the location of the application, will throw {@link WebDriverException} if
* called on a Non Safari application.
*
* @return Safari SDK version
*/
public String getSafariSDKVersion() {
validateSafariSDKVersionMethodCall();
Matcher sdkVersionMatcher = SAFARI_SDK_VERSION.matcher(app.getAbsolutePath());
if (sdkVersionMatcher.matches()) {
return sdkVersionMatcher.group(1) != null ? sdkVersionMatcher.group(1) : sdkVersionMatcher.group(2);
} else {
throw new WebDriverException("Cannot identify the version of Safari from application: " + app.getAbsolutePath());
}
}

private void validateSafariSDKVersionMethodCall() {
if (!isSafari()) {
throw new UnsupportedOperationException("SDK version cannot be determined for non-safari app");
}
if (!app.exists()) {
throw new WebDriverException("Application: " + app.getAbsolutePath()
+ " does not exist in the specified location");
}
}
}

0 comments on commit 3f0a5b9

Please sign in to comment.