mirror of
https://github.com/appium/appium.git
synced 2026-04-28 06:20:31 -05:00
Merge pull request #2104 from Jonahss/ios_uiautomationLocStrat
Ios uiautomation loc strat
This commit is contained in:
@@ -33,10 +33,10 @@ import com.android.uiautomator.core.UiSelector;
|
||||
|
||||
/**
|
||||
* This handler is used to find elements in the Android UI.
|
||||
*
|
||||
*
|
||||
* Based on which {@link Strategy}, {@link UiSelector}, and optionally the
|
||||
* contextId, the element Id or Ids are returned to the user.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class Find extends CommandHandler {
|
||||
// These variables are expected to persist across executions.
|
||||
@@ -61,11 +61,11 @@ public class Find extends CommandHandler {
|
||||
|
||||
/*
|
||||
* @param command The {@link AndroidCommand} used for this handler.
|
||||
*
|
||||
*
|
||||
* @return {@link AndroidCommandResult}
|
||||
*
|
||||
*
|
||||
* @throws JSONException
|
||||
*
|
||||
*
|
||||
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
|
||||
* bootstrap.AndroidCommand)
|
||||
*/
|
||||
@@ -75,8 +75,13 @@ public class Find extends CommandHandler {
|
||||
final Hashtable<String, Object> params = command.params();
|
||||
|
||||
// only makes sense on a device
|
||||
final Strategy strategy = Strategy.fromString((String) params
|
||||
.get("strategy"));
|
||||
final Strategy strategy;
|
||||
try {
|
||||
strategy = Strategy.fromString((String) params
|
||||
.get("strategy"));
|
||||
} catch (final InvalidStrategyException e) {
|
||||
return new AndroidCommandResult(WDStatus.UNKNOWN_COMMAND, e.getMessage());
|
||||
}
|
||||
final String contextId = (String) params.get("context");
|
||||
|
||||
if (strategy == Strategy.DYNAMIC) {
|
||||
@@ -280,12 +285,12 @@ public class Find extends CommandHandler {
|
||||
/**
|
||||
* Get the element from the {@link AndroidElementsHash} and return the element
|
||||
* id using JSON.
|
||||
*
|
||||
*
|
||||
* @param sel
|
||||
* A UiSelector that targets the element to fetch.
|
||||
* @param contextId
|
||||
* The Id of the element used for the context.
|
||||
*
|
||||
*
|
||||
* @return JSONObject
|
||||
* @throws JSONException
|
||||
* @throws ElementNotFoundException
|
||||
@@ -301,12 +306,12 @@ public class Find extends CommandHandler {
|
||||
/**
|
||||
* Get an array of elements from the {@link AndroidElementsHash} and return
|
||||
* the element's ids using JSON.
|
||||
*
|
||||
*
|
||||
* @param sel
|
||||
* A UiSelector that targets the element to fetch.
|
||||
* @param contextId
|
||||
* The Id of the element used for the context.
|
||||
*
|
||||
*
|
||||
* @return JSONObject
|
||||
* @throws JSONException
|
||||
* @throws UiObjectNotFoundException
|
||||
@@ -326,7 +331,7 @@ public class Find extends CommandHandler {
|
||||
/**
|
||||
* Create and return a UiSelector based on the strategy, text, and how many
|
||||
* you want returned.
|
||||
*
|
||||
*
|
||||
* @param strategy
|
||||
* The {@link Strategy} used to search for the element.
|
||||
* @param text
|
||||
@@ -406,7 +411,7 @@ public class Find extends CommandHandler {
|
||||
|
||||
/**
|
||||
* Create and return a UiSelector based on Xpath attributes.
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
* The Xpath path.
|
||||
* @param attr
|
||||
@@ -415,7 +420,7 @@ public class Find extends CommandHandler {
|
||||
* Any constraint.
|
||||
* @param substr
|
||||
* Any substr.
|
||||
*
|
||||
*
|
||||
* @return UiSelector
|
||||
* @throws AndroidCommandException
|
||||
*/
|
||||
|
||||
+6
-3
@@ -1,8 +1,10 @@
|
||||
package io.appium.android.bootstrap.selector;
|
||||
|
||||
import io.appium.android.bootstrap.exceptions.InvalidStrategyException;
|
||||
|
||||
/**
|
||||
* An emumeration of possible strategies.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public enum Strategy {
|
||||
CLASS_NAME(0, "class name"), CSS_SELECTOR(1, "css selector"), ID(2, "id"), NAME(
|
||||
@@ -10,7 +12,7 @@ public enum Strategy {
|
||||
"partial link text"), TAG_NAME(6, "tag name"), XPATH(7, "xpath"), DYNAMIC(
|
||||
8, "dynamic");
|
||||
|
||||
public static Strategy fromString(final String text) {
|
||||
public static Strategy fromString(final String text) throws InvalidStrategyException {
|
||||
if (text != null) {
|
||||
for (final Strategy s : Strategy.values()) {
|
||||
if (text.equalsIgnoreCase(s.strategyName)) {
|
||||
@@ -18,7 +20,8 @@ public enum Strategy {
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
throw new InvalidStrategyException("Locator strategy '" + text +
|
||||
"' is not supported on Android");
|
||||
}
|
||||
|
||||
private final int strategyCode;
|
||||
|
||||
@@ -214,6 +214,11 @@ exports.checkValidLocStrat = function (strat, includeWeb, cb) {
|
||||
'partial link text'
|
||||
]);
|
||||
}
|
||||
if (!includeWeb) {
|
||||
validStrats = validStrats.concat([
|
||||
'-ios_uiautomation'
|
||||
]);
|
||||
}
|
||||
|
||||
if (!_.contains(validStrats, strat)) {
|
||||
logger.info("Invalid locator strategy: " + strat);
|
||||
|
||||
@@ -48,32 +48,41 @@ iOSController.findUIElementOrElements = function (strategy, selector, ctx, many,
|
||||
var ext = many ? 's' : '';
|
||||
|
||||
var command = "";
|
||||
if (strategy === "name") {
|
||||
command = ["au.getElement", ext, "ByName('", selector, "'", ctx, ")"].join('');
|
||||
} else if (strategy === "xpath") {
|
||||
command = ["au.getElement", ext, "ByXpath('", selector, "'", ctx, ")"].join('');
|
||||
} else if (strategy === "id") {
|
||||
selector = selector.replace(/'/g, "\\\\'"); // must escape single quotes
|
||||
command = ["var exact = au.mainApp().getFirstWithPredicateWeighted(\"name == '", selector,
|
||||
"' || label == '", selector, "' || value == '", selector, "'\");"].join('');
|
||||
command += ["exact && exact.status == 0 ? exact : au.mainApp().getFirstWith",
|
||||
"PredicateWeighted(\"name contains[c] '", selector, "' || label contains[c] '",
|
||||
selector, "' || value contains[c] '", selector, "'\");"].join('');
|
||||
} else {
|
||||
command = ["au.getElement", ext, "ByType('", selector, "'", ctx, ")"].join('');
|
||||
switch (strategy) {
|
||||
case "name":
|
||||
command = ["au.getElement", ext, "ByName('", selector, "'", ctx, ")"].join('');
|
||||
break;
|
||||
case "xpath":
|
||||
command = ["au.getElement", ext, "ByXpath('", selector, "'", ctx, ")"].join('');
|
||||
break;
|
||||
case "id":
|
||||
selector = selector.replace(/'/g, "\\\\'"); // must escape single quotes
|
||||
command = ["var exact = au.mainApp().getFirstWithPredicateWeighted(\"name == '", selector,
|
||||
"' || label == '", selector, "' || value == '", selector, "'\");"].join('');
|
||||
command += ["exact && exact.status == 0 ? exact : au.mainApp().getFirstWith",
|
||||
"PredicateWeighted(\"name contains[c] '", selector, "' || label contains[c] '",
|
||||
selector, "' || value contains[c] '", selector, "'\");"].join('');
|
||||
break;
|
||||
case "-ios_uiautomation":
|
||||
command = ["au.getElement", ext, "ByUIAutomation('", selector, "'", ctx, ")"].join('');
|
||||
break;
|
||||
default:
|
||||
command = ["au.getElement", ext, "ByType('", selector, "'", ctx, ")"].join('');
|
||||
}
|
||||
|
||||
this.proxy(command, function (err, res) {
|
||||
this.handleFindCb(err, res, many, findCb);
|
||||
}.bind(this));
|
||||
}.bind(this);
|
||||
|
||||
|
||||
if (_.contains(this.supportedStrategies, strategy)) {
|
||||
this.waitForCondition(this.implicitWaitMs, doFind, cb);
|
||||
} else {
|
||||
cb(null, {
|
||||
status: status.codes.UnknownError.code
|
||||
, value: "Sorry, we don't support the '" + strategy + "' locator " +
|
||||
"strategy yet"
|
||||
"strategy for ios yet"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -89,7 +89,7 @@ IOS.prototype.init = function () {
|
||||
this.curWebCoords = null;
|
||||
this.onPageChangeCb = null;
|
||||
this.dontDeleteSimApps = false;
|
||||
this.supportedStrategies = ["name", "tag name", "xpath", "id"];
|
||||
this.supportedStrategies = ["name", "tag name", "xpath", "id", "-ios_uiautomation"];
|
||||
this.localizableStrings = {};
|
||||
this.keepAppToRetainPrefs = false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user