Best JavaScript code snippet using appium-xcuitest-driver
utils.js
Source:utils.js
1import { fs, tempDir } from 'appium-support';2import path from 'path';3import { utils as iosUtils } from 'appium-ios-driver';4import { exec } from 'teen_process';5import xcode from 'appium-xcode';6import _ from 'lodash';7import log from './logger';8import pkgObj from '../../package.json'; // eslint-disable-line import/no-unresolved9const WDA_DERIVED_DATA_SEARCH_SUFFIX = 'Library/Developer/Xcode/DerivedData/WebDriverAgent-*';10const WDA_ATTACHMENTS_FOLDER_RELATIVE_PATH = 'Logs/Test/Attachments';11const DRIVER_VER = pkgObj.version;12async function detectUdid () {13 log.debug('Auto-detecting real device udid...');14 let cmd, args = [];15 try {16 cmd = await fs.which('idevice_id');17 args.push('-l');18 log.debug('Using idevice_id');19 } catch (err) {20 log.debug('Using udidetect');21 cmd = require.resolve('udidetect');22 }23 let udid;24 try {25 let {stdout} = await exec(cmd, args, {timeout: 3000});26 let udids = _.filter(stdout.split('\n'), Boolean);27 udid = _.last(udids);28 if (udids.length > 1) {29 log.warn(`Multiple devices found: ${udids.join(', ')}`);30 log.warn(`Choosing '${udid}'. If this is wrong, manually set with 'udid' desired capability`);31 }32 } catch (err) {33 log.errorAndThrow(`Error detecting udid: ${err.message}`);34 }35 if (!udid || udid.length <= 2) {36 throw new Error('Could not detect udid.');37 }38 log.debug(`Detected real device udid: '${udid}'`);39 return udid;40}41async function getAndCheckXcodeVersion () {42 let version;43 try {44 version = await xcode.getVersion(true);45 } catch (err) {46 log.debug(err);47 log.errorAndThrow(`Could not determine Xcode version: ${err.message}`);48 }49 if (!version.toolsVersion) {50 try {51 version.toolsVersion = await xcode.getCommandLineToolsVersion();52 } catch (ign) {}53 }54 // we do not support Xcodes < 7.3,55 if (version.versionFloat < 7.3) {56 log.errorAndThrow(`Xcode version '${version.versionString}'. Support for ` +57 `Xcode ${version.versionString} is not supported. ` +58 `Please upgrade to version 7.3 or higher`);59 }60 return version;61}62async function getAndCheckIosSdkVersion () {63 let versionNumber;64 try {65 versionNumber = await xcode.getMaxIOSSDK();66 } catch (err) {67 log.errorAndThrow(`Could not determine iOS SDK version: ${err.message}`);68 }69 return versionNumber;70}71async function killAppUsingAppName (udid, appName) {72 let psArgs = [`-c`, `ps -ax|grep -i "${appName}"|grep -i "${udid}"|grep -v grep|awk '{print "kill -9 " $1}'|sh`];73 try {74 await exec(`bash`, psArgs);75 } catch (err) {76 log.debug(`Error : ${err.message}`);77 }78}79async function adjustWDAAttachmentsPermissions (perms) {80 if (!process.env.HOME) {81 throw new Error('Need HOME env var to be set in order to adjust WDA attachments permission');82 }83 let derivedDataSearchMask = path.join(process.env.HOME, WDA_DERIVED_DATA_SEARCH_SUFFIX);84 let folders = await fs.glob(derivedDataSearchMask);85 let changesMade = false;86 for (let folder of folders) {87 log.debug(`Found WDA derived data folder: '${folder}'`);88 let attachmentsFolder = path.join(folder, WDA_ATTACHMENTS_FOLDER_RELATIVE_PATH);89 if (await fs.exists(attachmentsFolder)) {90 log.info(`Setting '${perms}' permissions to '${attachmentsFolder}' folder`);91 await fs.chmod(attachmentsFolder, perms);92 changesMade = true;93 }94 }95 if (!changesMade) {96 log.info('No WDA derived data folders have been found.');97 }98}99async function clearSystemFiles (wda) {100 // only want to clear the system files for the particular WDA xcode run101 if (!wda || !wda.derivedDataPath) {102 log.debug('No WebDriverAgent derived data available, so unable to clear system files');103 return;104 }105 let toDelete = [path.resolve(wda.derivedDataPath, 'Logs')];106 await iosUtils.clearLogs(toDelete);107}108async function generateXcodeConfigFile (orgId, signingId) {109 log.debug(`Generating xcode config file for orgId '${orgId}' and signingId ` +110 `'${signingId}'`);111 let contents = `DEVELOPMENT_TEAM = ${orgId}112CODE_SIGN_IDENTITY = ${signingId}113`;114 let xcconfigPath = await tempDir.path('appium-temp.xcconfig');115 log.debug(`Writing xcode config file to ${xcconfigPath}`);116 await fs.writeFile(xcconfigPath, contents, "utf8");117 return xcconfigPath;118}119async function checkAppPresent (app) {120 log.debug(`Checking whether app '${app}' is actually present on file system`);121 if (!(await fs.exists(app))) {122 log.errorAndThrow(`Could not find app at '${app}'`);123 }124 log.debug('App is present');125}126async function getDriverInfo () {127 let stat = await fs.stat(path.resolve(__dirname, '..'));128 let built = stat.mtime.getTime();129 let info = {130 built,131 version: DRIVER_VER,132 };133 return info;134}135export { detectUdid, getAndCheckXcodeVersion, getAndCheckIosSdkVersion,136 killAppUsingAppName, adjustWDAAttachmentsPermissions,137 generateXcodeConfigFile, checkAppPresent, getDriverInfo,...
driver-specs.js
Source:driver-specs.js
...49 .returns(B.resolve());50 sinon.mock(driver.helpers).expects('configureApp')51 .returns(app);52 await driver.createSession({app});53 await driver.checkAppPresent(); // should not error54 // configureApp is shared between the two,55 // so restore mock or the next test will fail56 driver.helpers.configureApp.restore();57 });58 it('should reject if app not present', async function () {59 let driver = new SelendroidDriver({}, false);60 let app = path.resolve('asdfasdf');61 sinon.mock(driver).expects('checkAppPresent')62 .returns(B.resolve());63 sinon.mock(driver).expects('startSelendroidSession')64 .returns(B.resolve());65 sinon.mock(driver.helpers).expects('configureApp')66 .returns(app);67 await driver.createSession({app});68 driver.checkAppPresent.restore();69 await driver.checkAppPresent().should.eventually.be.rejectedWith('Could not find');70 });71 });72 describe('proxying', function () {73 let driver;74 before(function () {75 driver = new SelendroidDriver({}, false);76 driver.sessionId = 'abc';77 });78 describe('#proxyActive', function () {79 it('should exist', function () {80 driver.proxyActive.should.be.an.instanceof(Function);81 });82 it('should return true', function () {83 driver.proxyActive('abc').should.be.true;...
driver.js
Source:driver.js
...28 // TODO handle otherSessionData for multiple sessions29 let sessionId;30 [sessionId] = await super.createSession(caps);31 // fail very early if the app doesn't actually exist32 await this.checkAppPresent();33 if (!this.opts.bundleId) {34 this.opts.bundleId = await extractBundleId(this.opts.app);35 }36 // start sim, or use running sim37 log.info('starting simlator (if not booted)');38 this.sim = await this.startSim();39 this.wda = new WebDriverAgent({40 udid: this.sim.udid,41 platformVersion: this.opts.platformVersion,42 host: this.opts.host,43 agentPath: this.opts.agentPath44 });45 await this.wda.launch(sessionId);46 log.info("Installing the app");...
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder()3 .withCapabilities({4 })5 .build();6driver.sleep(5000);7driver.quit();8var webdriver = require('selenium-webdriver');9var driver = new webdriver.Builder()10 .withCapabilities({11 })12 .build();13driver.executeScript('mobile: checkAppPresent', {bundleId: 'com.apple.Preferences'}).then(function(result) {14 console.log(result);15});16driver.quit();17var webdriver = require('selenium-webdriver');18var driver = new webdriver.Builder()19 .withCapabilities({20 })21 .build();22driver.executeScript('mobile: checkAppPresent', {bundleId: 'com.apple.Preferences'}).then(function(result) {23 if (result) {24 console.log('The application is present.');25 } else {26 console.log('The application is not present.');27 }28});
Using AI Code Generation
1const wd = require('wd');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4chai.use(chaiAsPromised);5const expect = chai.expect;6(async function() {7 try {8 await driver.init({
Using AI Code Generation
1const wdio = require('webdriverio');2const opts = {3 desiredCapabilities: {4 }5};6const client = wdio.remote(opts);7 .checkAppPresent()8 .then((isPresent) => {9 if (!isPresent) {10 console.log('App is not present on device');11 }12 })13 .catch((err) => {14 console.error(err);15 });
Using AI Code Generation
1const { remote } = require('webdriverio');2const opts = {3 capabilities: {4 }5};6async function checkAppPresent() {7 const client = await remote(opts);8 const isAppInstalled = await client.isAppInstalled('com.apple.mobilesafari');9 console.log('isAppInstalled', isAppInstalled);10 await client.deleteSession();11}12checkAppPresent();13const { remote } = require('webdriverio');14const opts = {15 capabilities: {
Using AI Code Generation
1const wdio = require('webdriverio');2const options = {3 desiredCapabilities: {4 }5};6const client = wdio.remote(options);7client.init()8 .then(() => client.checkAppPresent())9 .then((isPresent) => console.log('App is present: ' + isPresent))10 .catch((err) => console.log(err))11 .end();
Using AI Code Generation
1const wdio = require('webdriverio');2const opts = {3 capabilities: {4 }5};6const client = wdio.remote(opts);7 .init()8 .then(async () => {9 const isAppInstalled = await client.isAppInstalled('com.myapp.myapp');10 console.log(isAppInstalled);11 })12 .catch((e) => {13 console.log(e);14 })15 .finally(() => {16 client.deleteSession();17 });18[0-0] 2019-05-31T12:56:38.282Z INFO webdriver: DATA { capabilities:19 { alwaysMatch: { appium: [Object] },20 firstMatch: [ {} ] },21 { platformName: 'iOS',22 automationName: 'XCUITest' } }23[0-0] 2019-05-31T12:56:38.286Z INFO webdriver: COMMAND init()24[0-0] 2019-05-31T12:56:38.286Z INFO webdriver: DATA { capabilities:25 { alwaysMatch: { appium: [
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desiredCapabilities = {4};5driver.init(desiredCapabilities)6 .then(function () {7 return driver.checkAppPresent('com.apple.mobilesafari');8 })9 .then(function (isPresent) {10 console.log('App is present', isPresent);11 })12 .catch(function (err) {13 console.log('Error', err);14 })15 .fin(function () {16 driver.quit();17 });18var wd = require('wd');19var assert = require('assert');20var desiredCapabilities = {21};22driver.init(desiredCapabilities)23 .then(function () {24 return driver.checkAppPresent('com.apple.mobilesafari');25 })26 .then(function (isPresent) {27 console.log('App is present', isPresent);28 })29 .catch(function (err) {30 console.log('Error', err);31 })32 .fin(function () {33 driver.quit();34 });35var wd = require('wd');36var assert = require('assert');37var desiredCapabilities = {38};39driver.init(desiredCapabilities)40 .then(function () {41 return driver.checkAppPresent('com.apple.mobilesafari');42 })43 .then(function (isPresent) {44 console.log('App is present', isPresent);45 })46 .catch(function (err) {47 console.log('Error', err);48 })
Using AI Code Generation
1const wd = require('wd');2const { checkAppPresent } = require('appium-xcuitest-driver/lib/commands/general');3const driver = wd.promiseChainRemote('localhost', 4723);4const caps = {5};6(async function example() {7 await driver.init(caps);8 const isAppPresent = await checkAppPresent.call(driver, caps.app);9 await driver.quit();10})();11commands.checkAppPresent = async function (appPath) {12 const appExists = await fs.exists(appPath);13 return appExists;14};15commands.checkAppPresent = async function (appPath) {16 const appExists = await fs.exists(appPath);17 return appExists;18};19commands.checkAppPresent = async function (appPath) {20 const appExists = await fs.exists(appPath);21 return appExists;22};23commands.checkAppPresent = async function (appPath) {24 const appExists = await fs.exists(appPath);25 return appExists;26};27commands.checkAppPresent = async function (appPath) {28 const appExists = await fs.exists(appPath);29 return appExists;30};31commands.checkAppPresent = async function (
Using AI Code Generation
1const { checkAppPresent } = require('appium-xcuitest-driver');2const { fs } = require('appium-support');3const appPath = 'path/to/my.app';4(async () => {5 try {6 const isPresent = await checkAppPresent(appPath);7 console.log('App is present: ', isPresent);8 } catch (err) {9 console.log('Error: ', err);10 }11})();12const { checkAppPresent } = require('appium-xcuitest-driver');13const { fs } = require('appium-support');14const appPath = 'path/to/my.app';15(async () => {16 try {17 const isPresent = await checkAppPresent(appPath);18 console.log('App is present: ', isPresent);19 } catch (err) {20 console.log('Error: ', err);21 }22})();23const { checkAppPresent } = require('appium-xcuitest-driver');24const { fs } = require('appium-support');25const appPath = 'path/to/my.app';26(async () => {27 try {28 const isPresent = await checkAppPresent(appPath);29 console.log('App is present: ', isPresent);30 } catch (err) {31 console.log('Error: ', err);32 }33})();34const { checkAppPresent } = require('appium-xcuitest-driver');35const { fs } = require('appium-support');36const appPath = 'path/to/my.app';37(async () => {38 try {39 const isPresent = await checkAppPresent(appPath);40 console.log('App is present: ', isPresent);41 } catch (err) {42 console.log('Error: ', err);43 }44})();45const { checkAppPresent } = require('appium-xcuitest-driver');46const { fs } = require('appium-support');47const appPath = 'path/to/my.app';48(async () => {49 try {50 const isPresent = await checkAppPresent(appPath);51 console.log('App is present: ', isPresent);52 } catch (err) {
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desired = {4};5driver.init(desired).then(function() {6 return driver.checkAppPresent()7}).then(function(appPresent) {8 if (appPresent) {9 console.log("App is already installed.");10 } else {11 console.log("App is not installed. Installing app...");12 return driver.installApp()13 }14}).then(function() {15 return driver.quit()16});17Parameters: time (Number)18Parameters: strategy (String) – strategy to be used to hide the keyboard. Can be one of the following:
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!