flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
265 lines • 12.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTimeout = exports.setImplicitWait = exports.siriCommandAndResponse = exports.getSiriEffect = exports.sendSiriCommand = exports.sendAdbCommand = exports.setDevProperties = exports.appiumFindByUiAutomator = exports.appiumSessionDestroy = exports.appiumSessionCreate = exports.sendAppiumRequest = void 0;
const http_request_1 = require("../http/http-request");
const helpers_1 = require("../helpers");
const jpath_1 = require("../json/jpath");
const DEFAULT_APPIUM_PORT = 4723;
const getUrlPrefix = (scenario) => scenario.suite.baseUrl
? `${scenario.suite.baseUrl.protocol}//${scenario.suite.baseUrl.host}/wd/hub`
: `http://localhost:${DEFAULT_APPIUM_PORT}/wd/hub`;
const sendAppiumRequest = (scenario, path, opts) => __awaiter(void 0, void 0, void 0, function* () {
const urlPrefix = getUrlPrefix(scenario);
const req = new http_request_1.HttpRequest(Object.assign(Object.assign({}, opts), {
headers: { "Content-Type": "application/json" },
uri: `${urlPrefix}${path}`,
}));
const res = yield req.fetch();
const doc = new jpath_1.JsonDoc(JSON.parse(res.body));
return doc;
});
exports.sendAppiumRequest = sendAppiumRequest;
const getAppiumSession = (scenario) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const json = yield exports.sendAppiumRequest(scenario, "/sessions", {
method: "get",
});
return ((_a = json.jsonRoot.value[0]) === null || _a === void 0 ? void 0 : _a.id) || null;
});
const createAppiumSession = (scenario, opts = {}) => __awaiter(void 0, void 0, void 0, function* () {
const json = yield exports.sendAppiumRequest(scenario, "/session", {
method: "post",
data: {
capabilities: {
alwaysMatch: Object.assign({}, opts),
},
},
});
return json.jsonRoot.value.sessionId;
});
const appiumSessionCreate = (scenario, opts = {}) => {
return () => __awaiter(void 0, void 0, void 0, function* () {
scenario.set("capabilities", opts);
const newSessionId = yield createAppiumSession(scenario, opts);
if (opts.devProperties) {
yield exports.setDevProperties(newSessionId, scenario, opts.devProperties);
}
return scenario.set("sessionId", newSessionId);
});
};
exports.appiumSessionCreate = appiumSessionCreate;
const appiumSessionDestroy = (scenario, sessionId) => __awaiter(void 0, void 0, void 0, function* () {
return yield exports.sendAppiumRequest(scenario, `/session/${sessionId}`, {
method: "delete",
});
});
exports.appiumSessionDestroy = appiumSessionDestroy;
const appiumFindByUiAutomator = (response, selector, text, opts, parent) => __awaiter(void 0, void 0, void 0, function* () {
const usingValue = selector.split("/");
let UiSelector = "new UiSelector().";
switch (usingValue[0]) {
case "id":
const packageName = yield appiumGetPackageName(response);
UiSelector += `resourceId("${packageName}:id/${usingValue[1]}").textContains("${text}")`;
break;
case "class name":
UiSelector += `className("${usingValue[1]}").textContains("${text}")`;
break;
case "accessibility id":
UiSelector += `description("${usingValue[1]}").textContains("${text}")`;
break;
default:
UiSelector += `text("${text}")`;
}
const res = yield exports.sendAppiumRequest(response.scenario, `/session/${response.sessionId}/${parent ? "element/" + parent + "/" : ""}elements`, {
method: "post",
data: {
using: "-android uiautomator",
value: UiSelector,
},
});
const elements = res.jsonRoot.value.map((item, i) => helpers_1.wrapAsValue(response.context, item.ELEMENT, `${selector} with text "${text}" [${i}]`));
return (opts === null || opts === void 0 ? void 0 : opts.offset) || (opts === null || opts === void 0 ? void 0 : opts.limit)
? helpers_1.applyOffsetAndLimit(opts, elements)
: elements;
});
exports.appiumFindByUiAutomator = appiumFindByUiAutomator;
const appiumGetPackageName = (response) => __awaiter(void 0, void 0, void 0, function* () {
const res = yield exports.sendAppiumRequest(response.scenario, `/session/${response.sessionId}/appium/device/current_package`, {
method: "get",
});
return res.jsonRoot.value;
});
const getAppiumSessionCapabilities = (sessionId, scenario) => __awaiter(void 0, void 0, void 0, function* () {
const res = yield exports.sendAppiumRequest(scenario, `/session/${sessionId}`, {
method: "get",
});
return res.jsonRoot.value;
});
const setDevProperties = (sessionId, scenario, devProperties = {}) => __awaiter(void 0, void 0, void 0, function* () {
if (devProperties.network) {
const capabilities = yield getAppiumSessionCapabilities(sessionId, scenario);
const automationName = capabilities.automationName;
if (automationName.toLowerCase() === "uiautomator2" ||
automationName.toLowerCase() === "espresso") {
if (devProperties.network.wifi !== undefined) {
yield exports.sendAdbCommand(sessionId, scenario, `svc wifi ${devProperties.network.wifi ? "enable" : "disable"}`);
}
if (devProperties.network.mobileData !== undefined) {
yield exports.sendAdbCommand(sessionId, scenario, `svc data ${devProperties.network.mobileData ? "enable" : "disable"}`);
}
if (devProperties.network.locationServices !== undefined) {
yield exports.sendAdbCommand(sessionId, scenario, "settings", [
"put",
"secure",
"location_mode",
devProperties.network.locationServices ? 3 : 0,
]);
}
if (devProperties.network.airplaneMode === true) {
yield exports.sendAdbCommand(sessionId, scenario, "settings", [
"put",
"global",
"airplane_mode_on",
1,
]);
yield exports.sendAdbCommand(sessionId, scenario, "svc wifi disable");
yield exports.sendAdbCommand(sessionId, scenario, "svc data disable");
}
if (devProperties.network.airplaneMode === false) {
yield exports.sendAdbCommand(sessionId, scenario, "settings", [
"put",
"global",
"airplane_mode_on",
0,
]);
}
}
else if (capabilities.automationName.toLowerCase() === "xcuitest") {
if (devProperties.network.wifi !== undefined) {
yield exports.siriCommandAndResponse(sessionId, scenario, "Wi-Fi", devProperties.network.wifi);
}
if (devProperties.network.mobileData !== undefined) {
yield exports.siriCommandAndResponse(sessionId, scenario, "Cellular Data", devProperties.network.mobileData);
}
if (devProperties.network.locationServices !== undefined) {
if (devProperties.network.locationServices) {
yield exports.sendSiriCommand(sessionId, scenario, "Turn on location services");
const siriVal = yield exports.getSiriEffect(sessionId, scenario, "Location Services");
if (siriVal !== "On")
throw "Failed to set location services";
}
else {
yield exports.sendSiriCommand(sessionId, scenario, "Turn off location services");
let siriVal = yield exports.getSiriEffect(sessionId, scenario, "Location Services");
if (siriVal === "On") {
yield exports.sendSiriCommand(sessionId, scenario, "Yes");
yield helpers_1.delay(100);
}
siriVal = yield exports.getSiriEffect(sessionId, scenario, "Location Services");
if (siriVal !== "Off")
throw "Failed to set location services";
}
}
if (devProperties.network.airplaneMode !== undefined) {
yield exports.siriCommandAndResponse(sessionId, scenario, "Airplane Mode", devProperties.network.airplaneMode);
}
yield exports.sendSiriCommand(sessionId, scenario, "Close Siri");
yield helpers_1.delay(3500);
}
}
if (devProperties.location) {
yield exports.sendAppiumRequest(scenario, `/session/${sessionId}/location`, {
method: "post",
data: {
location: {
latitude: devProperties.location.latitude,
longitude: devProperties.location.longitude,
altitude: devProperties.location.altitude || 0,
},
},
});
}
});
exports.setDevProperties = setDevProperties;
const sendAdbCommand = (sessionId, scenario, command, args, timeout = 20000, includeStderr = false) => __awaiter(void 0, void 0, void 0, function* () {
var _b;
const res = yield exports.sendAppiumRequest(scenario, `/session/${sessionId}/execute`, {
method: "post",
data: {
script: "mobile: shell",
args: {
command: command,
args: args,
timeout: timeout,
includeStderr: includeStderr,
},
},
});
if ((_b = res.jsonRoot.value) === null || _b === void 0 ? void 0 : _b.error)
throw res.jsonRoot.value.error.message;
return res.jsonRoot.value;
});
exports.sendAdbCommand = sendAdbCommand;
const sendSiriCommand = (sessionId, scenario, command) => __awaiter(void 0, void 0, void 0, function* () {
yield exports.sendAppiumRequest(scenario, `/session/${sessionId}/execute`, {
method: "post",
data: {
script: "mobile: siriCommand",
args: {
text: command,
},
},
});
});
exports.sendSiriCommand = sendSiriCommand;
const getSiriEffect = (sessionId, scenario, setting) => __awaiter(void 0, void 0, void 0, function* () {
const prevTimeout = yield exports.getTimeout(sessionId, scenario);
yield exports.setImplicitWait(sessionId, scenario, 3000);
const elRes = yield exports.sendAppiumRequest(scenario, `/session/${sessionId}/element`, {
method: "post",
data: {
using: "accessibility id",
value: setting,
},
});
const textRes = yield exports.sendAppiumRequest(scenario, `/session/${sessionId}/element/${elRes.jsonRoot.value.ELEMENT}/text`, {
method: "get",
});
yield exports.setImplicitWait(sessionId, scenario, prevTimeout);
return textRes.jsonRoot.value;
});
exports.getSiriEffect = getSiriEffect;
const siriCommandAndResponse = (sessionId, scenario, setting, isSet) => __awaiter(void 0, void 0, void 0, function* () {
yield exports.sendSiriCommand(sessionId, scenario, `Turn ${isSet ? "on" : "off"} ${setting}`);
const siriVal = yield exports.getSiriEffect(sessionId, scenario, setting);
if (siriVal !== (isSet ? "On" : "Off"))
throw `Failed to set ${setting}`;
});
exports.siriCommandAndResponse = siriCommandAndResponse;
const setImplicitWait = (sessionId, scenario, ms) => __awaiter(void 0, void 0, void 0, function* () {
yield exports.sendAppiumRequest(scenario, `/session/${sessionId}/timeouts/implicit_wait`, {
method: "post",
data: {
ms: ms,
},
});
});
exports.setImplicitWait = setImplicitWait;
const getTimeout = (sessionId, scenario) => __awaiter(void 0, void 0, void 0, function* () {
const res = yield exports.sendAppiumRequest(scenario, `/session/${sessionId}/timeouts`, {
method: "get",
});
return res.jsonRoot.value.implicit;
});
exports.getTimeout = getTimeout;
//# sourceMappingURL=appium-helpers.js.map
;