UNPKG

ti-appium

Version:

An Appium wrapper to test Titanium applications

210 lines (163 loc) 7.24 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: src/appium.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: src/appium.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict'; const wd = require('wd'), chai = require('chai'), appium = require('appium'), output = require('./output.js'), webdriver = require('./webdriver.js'), chaiAsPromised = require('chai-as-promised'); /** * @class Appium_Helper * @desc * Functions for handling the Appium server and client sessions. */ class Appium_Helper { /** * Starts a WD session on the device, using the given capability requirements * as Appium configuration. * * @param {Object} capabilities - Desired capabilities for Appium to run with */ static async startClient(capabilities) { output.debug('Starting WebDriver Instance'); if (!capabilities.automationName) { switch (capabilities.platformName) { case 'iOS': capabilities.automationName = 'XCUITest'; break; case 'Android': capabilities.automationName = 'UiAutomator2'; break; default: capabilities.automationName = 'Appium'; break; } } if (!capabilities.deviceReadyTimeout &amp;&amp; capabilities.platformName === 'Android') { capabilities.deviceReadyTimeout = 60; } // Seems to be an issue from Xcode 11.4.1, simulators won't show as visible unless the state is 'on' if (!capabilities.simulatorPasteboardAutomaticSync &amp;&amp; capabilities.platformName === 'iOS') { capabilities.simulatorPasteboardAutomaticSync = 'on'; } // Sets the amount of time Appium waits before shutting down in the background if (!capabilities.newCommandTimeout) { capabilities.newCommandTimeout = (60 * 10); } // Enabling chai assertion style: https://www.npmjs.com/package/chai-as-promised#node chai.use(chaiAsPromised); chai.should(); // Enables chai assertion chaining chaiAsPromised.transferPromiseness = wd.transferPromiseness; // Establish the testing driver let driver = wd.promiseChainRemote({ host: 'localhost', port: 4723 }); // Make sure to include the custom commands defined in the WebDriver Helper webdriver.loadDriverCommands(driver, wd); global.driver = driver; global.webdriver = wd; await driver.init(capabilities); // Janky fix to patch an issue with syslog not reporting on iOS tests await driver.resetApp(); } /** * Stops the WD session, but first it closes and removes the app from the * device in an attempt to save storage space. * * @param {Boolean} softStop - Whether or not to remove the app on stopping */ static async stopClient(softStop = false) { output.debug('Stopping WebDriver Instance'); const driver = global.driver; if (driver) { const capabilities = await driver.sessionCapabilities(), platform = capabilities.platformName; output.debug('Closing the application'); await driver.closeApp(); if (!softStop) { if (platform === 'Android' || platform === 'iOS') { output.debug('Removing the app from device'); await driver.removeApp((platform === 'iOS') ? capabilities.CFBundleIdentifier : capabilities.desired.appPackage); } } output.debug('Exiting the WebDriver session'); await driver.quit(); delete global.driver; delete global.webdriver; } } /** * Launch an Appium server for the mobile testing, as it cannot use the * desktop session. * * @param {Object} opts - Optional arguments * @param {String} opts.hostname - The address of the Appium server to connect to * @param {Int} opts.port - The port of the server that the Appium server is running on */ static async runAppium({ hostname = 'localhost', port = 4723 } = {}) { output.debug(`Starting Appium Server On '${hostname}:${port}'`); // We only want to allow starting a server on the local machine const validAddresses = [ 'localhost', '0.0.0.0', '127.0.0.1' ]; if (validAddresses.includes(hostname)) { this.server = await appium.main({ host: hostname, port: port, loglevel: 'false' }); this.host = hostname; this.port = port; } else { throw Error('Connecting to an External Appium Server is Not Currently Supported'); } } /** * Tells the Appium server to shut down */ static async quitServ() { output.debug('Stopping Appium Server'); try { if (this.server) { output.debug('Found running Appium Instance'); await this.server.close(); output.debug('Clearing class variables'); delete this.server; delete this.host; delete this.port; } else { throw Error('Appium server not found!'); } } catch (e) { throw e; } } } module.exports = Appium_Helper; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="WebDriverCommands.html">WebDriverCommands</a></li></ul><h3>Classes</h3><ul><li><a href="Appc_Helper.html">Appc_Helper</a></li><li><a href="Appium_Helper.html">Appium_Helper</a></li><li><a href="Device_Helper.html">Device_Helper</a></li><li><a href="Mocha_Helper.html">Mocha_Helper</a></li><li><a href="Output_Helper.html">Output_Helper</a></li><li><a href="Webdriver_Helper.html">Webdriver_Helper</a></li></ul><h3>Global</h3><ul><li><a href="global.html#appcRun">appcRun</a></li><li><a href="global.html#appcSetup">appcSetup</a></li><li><a href="global.html#banner">banner</a></li><li><a href="global.html#buildApp">buildApp</a></li><li><a href="global.html#createAppPath">createAppPath</a></li><li><a href="global.html#debug">debug</a></li><li><a href="global.html#error">error</a></li><li><a href="global.html#finish">finish</a></li><li><a href="global.html#getCert">getCert</a></li><li><a href="global.html#getProfile">getProfile</a></li><li><a href="global.html#getSimState">getSimState</a></li><li><a href="global.html#info">info</a></li><li><a href="global.html#killEmulator">killEmulator</a></li><li><a href="global.html#killSimulator">killSimulator</a></li><li><a href="global.html#parseSDK">parseSDK</a></li><li><a href="global.html#skip">skip</a></li><li><a href="global.html#startAppium">startAppium</a></li><li><a href="global.html#startClient">startClient</a></li><li><a href="global.html#step">step</a></li><li><a href="global.html#stopAppium">stopAppium</a></li><li><a href="global.html#stopClient">stopClient</a></li><li><a href="global.html#test">test</a></li><li><a href="global.html#warn">warn</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Fri Aug 28 2020 10:09:07 GMT+0100 (British Summer Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>