Github user vladimir-kotikov commented on a diff in the pull request: https://github.com/apache/cordova-paramedic/pull/11#discussion_r69516761 --- Diff: lib/appium/AppiumRunner.js --- @@ -0,0 +1,491 @@ +#!/usr/bin/env node + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* jshint node: true */ + +"use strict"; + +var fs = require("fs"); +var path = require("path"); +var util = require("./helpers/util"); +var logger = require("../utils").logger; +var wd = require("wd"); +var wdHelper = require("./helpers/wdHelper"); +var screenshotHelper = require("./helpers/screenshotHelper"); +var kill = require("tree-kill"); +var child_process = require("child_process"); +var expectTelnet = require("expect-telnet"); +var shell = require("shelljs"); +var Jasmine = require("jasmine"); +var unorm = require("unorm"); +var elementTree = require("elementtree"); +var q = require("q"); +var Reporters = require("../Reporters"); + +var KILL_SIGNAL = "SIGINT"; +var SMALL_BUFFER_SIZE = 1024 * 1024; +var BIG_BUFFER_SIZE = 50 * 1024 * 1024; +var APPIUM_SERVER_PATH = getAppiumServerPath(); + +function AppiumRunner(options) { + this.options = options; + this.findTests(); + this.setGlobals(); +} + +function getAppiumServerPath() { + return path.resolve(process.cwd(), "cordova-paramedic/node_modules/appium/build/lib/main.js"); +} + +function getFullAppPath(appPath) { + var fullPath = appPath; + if (!path.isAbsolute(appPath)) { + fullPath = path.join(__dirname, "../..", appPath); + } + return fullPath; +} + +function getPackagePath(options) { + if (options.sauce) { + return options.sauceAppPath; + } + + var fullAppPath = getFullAppPath(options.appPath); + + switch (options.platform) { + case "android": + return path.join(fullAppPath, "/platforms/android/build/outputs/apk/android-debug.apk"); + case "ios": + var searchDir = options.device ? + path.join(getFullAppPath(options.appPath), "/platforms/ios/build/device/") : + path.join(getFullAppPath(options.appPath), "/platforms/ios/build/emulator/"); + var fileMask = options.device ? "*.ipa" : "*.app"; + var files = shell.ls(searchDir + fileMask); + logger.normal("paramedic-appium: Looking for app package in " + searchDir); + if (files && files.length > 0) { + logger.normal("paramedic-appium: Found app package: " + files[0]); + return files[0]; + } + throw new Error("Could not find the app package"); + } +} + +function getPluginDirs(appPath) { + return shell.ls(path.join(appPath, "/plugins/cordova-plugin-*")); +} + +function getConfigPath(appPath) { + return path.join(appPath, "config.xml"); +} + +function parseElementtreeSync(filename) { + var contents = fs.readFileSync(filename, util.DEFAULT_ENCODING); + if (!contents) { + throw new Error("The config file is empty: " + filename); + } + // Skip the Byte Order Mark (BOM) + contents = contents.substring(contents.indexOf("<")); + + return new elementTree.ElementTree(elementTree.XML(contents)); +} + +function addCspSource(appPath, directive, source) { + var cspInclFile = path.join(appPath, "www/csp-incl.js"); + var indexFile = path.join(appPath, "www/index.html"); + var cspFile = fs.existsSync(cspInclFile) ? cspInclFile : indexFile; + var cspContent = fs.readFileSync(cspFile, util.DEFAULT_ENCODING); + var cspTagOpening = "<meta http-equiv=\"Content-Security-Policy\" content=\""; + var cspRule = directive + " " + source; + var cspRuleReg = new RegExp(directive + "[^;\"]+" + source.replace("*", "\\*")); + + logger.normal("paramedic-appium: Adding CSP source \"" + source + "\" to directive \"" + directive + "\""); + + if (cspContent.match(cspRuleReg)) { + logger.normal("paramedic-appium: It's already there."); + } else if (util.contains(cspContent, directive)) { + // if the directive is there, just add the source to it + cspContent = cspContent.replace(directive, cspRule); + fs.writeFileSync(cspFile, cspContent, util.DEFAULT_ENCODING); + } else if (cspContent.match(/content=".*?default-src.+?"/)) { + // needed directive is not there but there is default-src directive + // creating needed directive and copying default-src sources to it + var defaultSrcReg = /(content=".*?default-src)(.+?);/; + cspContent = cspContent.replace(defaultSrcReg, "$1$2; " + cspRule + "$2;"); + fs.writeFileSync(cspFile, cspContent, util.DEFAULT_ENCODING); + } else if (util.contains(cspContent, cspTagOpening)) { + // needed directive is not there and there is no default-src directive + // but the CSP tag is till present + // just adding needed directive to a start of CSP tag content + cspContent = cspContent.replace(cspTagOpening, cspTagOpening + directive + " " + source + "; "); + fs.writeFileSync(cspFile, cspContent, util.DEFAULT_ENCODING); + } else { + // no CSP tag, skipping + logger.normal("paramedic-appium: WARNING: No CSP tag found."); + } +} + +function setPreference(appPath, preference, value) { + var configFile = getConfigPath(appPath); + var xml = parseElementtreeSync(configFile); + var pref = xml.find("preference[@name=\"" + preference + "\"]"); + + logger.normal("paramedic-appium: Setting \"" + preference + "\" preference to \"" + value + "\""); + + if (!pref) { + pref = new elementTree.Element("preference"); + pref.attrib.name = preference; + xml.getroot().append(pref); + } + pref.attrib.value = value; + + // write the changes + fs.writeFileSync(configFile, xml.write({indent: 4}), util.DEFAULT_ENCODING); +} + +function permitAccess(appPath, origin) { + var configFile = getConfigPath(appPath); + var xml = parseElementtreeSync(configFile); + var rule = xml.find("access[@origin=\"" + origin + "\"]"); + + logger.normal("paramedic-appium: Adding a whitelist 'access' rule for origin: " + origin); + + if (rule) { + logger.normal("paramedic-appium: It is already in place"); + } else { + rule = new elementTree.Element("access"); + rule.attrib.origin = origin; + xml.getroot().append(rule); + fs.writeFileSync(configFile, xml.write({indent: 4}), util.DEFAULT_ENCODING); + } +} + +function runCommand(command, appPath) { + if (appPath) { + shell.pushd(appPath); + } + shell.exec(command); + if (appPath) { + shell.popd(); + } +} + +function isFailFastError(error) { + if (error && error.message) { + return error.message.indexOf("Could not find a connected") > -1 || + error.message.indexOf("Bad app") > -1; + } + return false; +} + +function killProcess(procObj, killSignal, callback) { + if (procObj && procObj.alive) { + procObj.alive = false; + setTimeout(function () { + kill(procObj.process.pid, killSignal, callback); + }, 1000); + } else { + callback(); + } +} + +function installAppiumServer(success, fail) { + logger.normal("paramedic-appium: Installing Appium server..."); + shell.pushd(path.join(__dirname, "../..")); + shell.exec("npm install appium", { async: true, silent: !global.VERBOSE }, function (code, stdout) { + if (code !== 0) { + fail("Couldn't install Appium server:\n" + stdout); + } else { + success(); + } + }); + shell.popd(); +} + +function addCallback(reporter, callback) { --- End diff -- It might be better to place this function into 'Reporters.js' (a static method?)
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org For additional commands, e-mail: dev-h...@cordova.apache.org