How to use this.findNativeElementOrElements method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

web.js

Source:web.js Github

copy

Full Screen

...32 const implicitWaitMs = this.implicitWaitMs;33 // try to see if there has been scrolling34 try {35 this.setImplicitWait(0);36 await this.findNativeElementOrElements('accessibility id', 'ReloadButton', false);37 // reload button found, which means scrolling has not happened38 } catch (err) {39 // no reload button, which indicates scrolling has happened40 try {41 const el = await this.findNativeElementOrElements('accessibility id', 'URL', false);42 offset -= await this.getElementHeightMemoized('URLBar', el);43 } catch (ign) {44 // no URL elements found, so continue45 }46 // when scrolling has happened, there is a tick more offset needed47 offset += EXTRA_WEB_COORD_SCROLL_OFFSET;48 } finally {49 // return implicit wait to what it was50 this.setImplicitWait(implicitWaitMs);51 }52 // extra offset necessary (where do these come from? they just work)53 offset += await getSafariIsIphone(this.opts.sessionId, this) ?54 IPHONE_WEB_COORD_OFFSET :55 IPAD_WEB_COORD_OFFSET;56 log.debug(`Extra translated web coordinates offset: ${offset}`);57 return offset;58};59extensions.getExtraNativeWebTapOffset = async function () {60 let offset = 0;61 // keep track of implicit wait, and set locally to 062 const implicitWaitMs = this.implicitWaitMs;63 try {64 this.setImplicitWait(0);65 // first try to get tab offset66 try {67 const el = await this.findNativeElementOrElements('-ios predicate string', `name LIKE '*, Tab' AND visible = 1`, false);68 offset += await this.getElementHeightMemoized('TabBar', el);69 } catch (ign) {70 // no element found, so no tabs and no need to deal with offset71 }72 // next try to see if there is an Smart App Banner73 try {74 await this.findNativeElementOrElements('accessibility id', 'Close app download offer', false);75 offset += await getSafariIsIphone(this.opts.sessionId, this) ?76 IPHONE_WEB_COORD_SMART_APP_BANNER_OFFSET :77 IPAD_WEB_COORD_SMART_APP_BANNER_OFFSET;78 } catch (ign) {79 // no smart app banner found, so continue80 }81 } finally {82 // return implicit wait to what it was83 this.setImplicitWait(implicitWaitMs);84 }85 log.debug(`Additional native web tap offset computed: ${offset}`);86 return offset;87};88extensions.nativeWebTap = async function (el) {89 let atomsElement = this.useAtomsElement(el);90 let {x, y} = await this.executeAtom('get_top_left_coordinates', [atomsElement]);91 let {width, height} = await this.executeAtom('get_size', [atomsElement]);92 x = x + (width / 2);93 y = y + (height / 2);94 this.curWebCoords = {x, y};95 await this.clickWebCoords();96};97extensions.clickCoords = async function (coords) {98 let {x, y} = coords;99 // tap on absolute coordinates100 await this.proxyCommand('/wda/tap/nil', 'POST', {x, y});101};102extensions.translateWebCoords = async function (coords) {103 log.debug(`Translating coordinates (${JSON.stringify(coords)}) to web coordinates`);104 // add static offset for safari in landscape mode105 let yOffset = this.opts.curOrientation === 'LANDSCAPE' ? this.landscapeWebCoordsOffset : 0;106 // add extra offset for possible extra things in the top of the page107 yOffset += await this.getExtraNativeWebTapOffset();108 coords.y += await this.getExtraTranslateWebCoordsOffset();109 // absolutize web coords110 let webview = await retryInterval(5, 100, async () => {111 const implicitWaitMs = this.implicitWaitMs;112 try {113 this.setImplicitWait(0);114 return await this.findNativeElementOrElements('-ios predicate string', `type = 'XCUIElementTypeWebView' AND visible = 1`, false);115 } finally {116 this.setImplicitWait(implicitWaitMs);117 }118 });119 webview = util.unwrapElement(webview);120 let rect = await this.proxyCommand(`/element/${webview}/rect`, 'GET');121 let wvPos = {x: rect.x, y: rect.y};122 let realDims = {w: rect.width, h: rect.height};123 let cmd = '(function () { return {w: document.documentElement.clientWidth, h: document.documentElement.clientHeight}; })()';124 let wvDims = await this.remote.execute(cmd);125 // TODO: investigate where these come from. They appear to be constants in my tests126 let urlBarHeight = 64;127 wvPos.y += urlBarHeight;128 let realDimensionHeight = 108;...

Full Screen

Full Screen

alert.js

Source:alert.js Github

copy

Full Screen

...52 let endpoint = `/alert/dismiss`;53 return await this.proxyCommand(endpoint, method);54};55helpers.getAlert = async function () {56 let possibleAlert = await this.findNativeElementOrElements('class name', 'XCUIElementTypeScrollView', true);57 if (possibleAlert.length !== 1) throw new errors.NoAlertOpenError();58 let possibleAlertButtons = await this.findNativeElementOrElements('class name', 'XCUIElementTypeButton', true, possibleAlert[0].ELEMENT);59 if (possibleAlertButtons.length < 1 || possibleAlertButtons.length > 2) throw new errors.NoAlertOpenError();60 let assertButtonName = async (button, expectedName) => {61 button = button.ELEMENT ? button.ELEMENT : button;62 let name = await this.proxyCommand(`/element/${button}/attribute/name`, 'GET');63 if (name.toLowerCase() !== expectedName) throw new errors.NoAlertOpenError();64 };65 let alert = possibleAlert[0];66 if (possibleAlertButtons.length === 1) {67 // make sure the button is 'Close'68 let closeButton = possibleAlertButtons[0];69 await assertButtonName(closeButton, 'close');70 alert.close = async () => {71 await this.proxyCommand(`/element/${closeButton.ELEMENT}/click`, 'POST');72 };73 } else {74 // ensure the buttons are 'Cancel' and 'OK'75 let cancelButton = possibleAlertButtons[0];76 await assertButtonName(cancelButton, 'cancel');77 let okButton = possibleAlertButtons[1];78 await assertButtonName(okButton, 'ok');79 alert.cancel = async () => {80 await this.proxyCommand(`/element/${cancelButton.ELEMENT}/click`, 'POST');81 };82 alert.ok = async () => {83 await this.proxyCommand(`/element/${okButton.ELEMENT}/click`, 'POST');84 };85 }86 alert.getText = async () => {87 let textView = await this.findNativeElementOrElements('class name', 'XCUIElementTypeTextView', false, alert.ELEMENT);88 return await this.proxyCommand(`/element/${textView.ELEMENT}/attribute/value`, 'GET');89 };90 alert.setText = async (value) => {91 try {92 let textView = await this.findNativeElementOrElements('class name', 'XCUIElementTypeTextField', false, alert.ELEMENT);93 await this.proxyCommand(`/element/${textView.ELEMENT}/value `, 'POST', {value});94 } catch (err) {95 if (isErrorType(err, errors.NoSuchElementError)) {96 throw new Error('Tried to set text of an alert that was not a prompt');97 }98 throw err;99 }100 };101 return alert;102};103Object.assign(extensions, commands, helpers);104export { commands, helpers };...

Full Screen

Full Screen

general.js

Source:general.js Github

copy

Full Screen

...42};43commands.hideKeyboard = async function (strategy, ...possibleKeys) {44 let keyboard;45 try {46 keyboard = await this.findNativeElementOrElements('class name', 'XCUIElementTypeKeyboard', false);47 } catch (err) {48 // no keyboard found49 log.debug('No keyboard found. Unable to hide.');50 return;51 }52 possibleKeys.pop(); // last parameter is the session id53 possibleKeys = possibleKeys.filter((element) => !!element); // get rid of undefined elements54 if (possibleKeys.length) {55 for (let key of possibleKeys) {56 let el = _.last(await this.findNativeElementOrElements('accessibility id', key, true, keyboard));57 if (el) {58 log.debug(`Attempting to hide keyboard by pressing '${key}' key.`);59 await this.nativeClick(el);60 return;61 }62 }63 } else {64 // find the keyboard, and hit the last Button65 log.debug('Finding keyboard and clicking final button to close');66 let buttons = await this.findNativeElementOrElements('class name', 'XCUIElementTypeButton', true, keyboard);67 await this.nativeClick(_.last(buttons));68 }69};70commands.getDeviceTime = iosCommands.general.getDeviceTime;71commands.getStrings = iosCommands.general.getStrings;72commands.removeApp = async function (bundleId) {73 if (this.isRealDevice()) {74 await this.opts.device.remove(bundleId);75 } else {76 await this.opts.device.removeApp(bundleId);77 }78};79commands.launchApp = iosCommands.general.launchApp;80commands.closeApp = iosCommands.general.closeApp;...

Full Screen

Full Screen

navigation.js

Source:navigation.js Github

copy

Full Screen

...10 }11};12helpers.nativeBack = async function () {13 try {14 let navBar = await this.findNativeElementOrElements('class name', 'XCUIElementTypeNavigationBar', false);15 let buttons = await this.findNativeElementOrElements('class name', 'XCUIElementTypeButton', true, navBar);16 let backButton = _.filter(buttons, (value) => value.label === 'Back')[0];17 log.debug(`Found navigation bar 'back' button. Clicking.`);18 await this.nativeClick(backButton);19 } catch (err) {20 log.error('Unable to find navigation bar and back button.');21 }22};23commands.forward = async function () {24 if (!this.isWebContext()) {25 }26 await this.mobileWebNav('forward');27};28commands.closeWindow = async function () {29 if (!this.isWebContext()) {...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { findNativeElementOrElements } = require('appium-xcuitest-driver/lib/commands/web');2const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/web');3const { findNativeElementOrElements } = require('appium-xcuitest-driver/lib/commands/web');4const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/web');5const { findNativeElementOrElements } = require('appium-xcuitest-driver/lib/commands/web');6const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/web');7const { findNativeElementOrElements } = require('appium-xcuitest-driver/lib/commands/web');8const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/web');9const { findNativeElementOrElements } = require('appium-xcuitest-driver/lib/commands/web');10const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/web');11const { findNativeElementOrElements } = require('appium-xcuitest-driver/lib/commands/web');12const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/web');13const { findNativeElementOrElements } = require('appium-xcuitest-driver/lib/commands/web');14const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/web');

Full Screen

Using AI Code Generation

copy

Full Screen

1const { AppiumDriver, XCUITestDriver } = require('appium-xcuitest-driver');2const { XCUITestDriverExtension } = require('appium-xcuitest-driver/lib/commands/element');3class CustomXCUITestDriver extends XCUITestDriver {4 async findNativeElementOrElements(strategy, selector, mult) {5 let command = mult ? 'findElements' : 'findElement';6 return await this.proxyCommand(`/element${mult ? 's' : ''}`, 'POST', { using: strategy, value: selector });7 }8}9const appiumDriver = new AppiumDriver();10const xcuiTestDriver = new CustomXCUITestDriver(appiumDriver, null, null, null, null, null);11const xcuiTestDriverExtension = new XCUITestDriverExtension(xcuiTestDriver);12const { AppiumDriver, XCUITestDriver } = require('appium-xcuitest-driver');13const { XCUITestDriverExtension } = require('appium-xcuitest-driver/lib/commands/element');14class CustomXCUITestDriver extends XCUITestDriver {15 async findNativeElementOrElements(strategy, selector, mult) {16 let command = mult ? 'findElements' : 'findElement';17 return await this.proxyCommand(`/element${mult ? 's' : ''}`, 'POST', { using: strategy, value: selector });18 }19}20const appiumDriver = new AppiumDriver();21const xcuiTestDriver = new CustomXCUITestDriver(app

Full Screen

Using AI Code Generation

copy

Full Screen

1const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/find');2const { findNativeElementOrElements } = require('appium-xcuitest-driver').commands.find;3const { findNativeElementOrElements } = require('appium-xcuitest-driver/build/lib/commands/find');4const { findNativeElementOrElements } = require('appium-xcuitest-driver').commands.find;5[debug] [JSONWP Proxy] Got response with status 200: {"value":{"ELEMENT":"1B000000-0000-0000-5A7D-000000000000"},"sessionId":"5E3C3D7F-6F1C-4C4F-8B6F-4B9F0E7D0B8E","status":0}6[debug] [MJSONWP] Responding to client with driver.findElement() result: {"ELEMENT":"1B000000-0000-0000-5A7D-000000000000"}

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4chai.use(chaiAsPromised);5const expect = chai.expect;6const should = chai.should();7chaiAsPromised.transferPromiseness = wd.transferPromiseness;8const serverConfig = {9};10const capabilities = {11};12const driver = wd.promiseChainRemote(serverConfig);13describe('Native Context', () => {14 before(async () => {15 await driver.init(capabilities);16 await driver.sleep(3000);17 });18 it('should be able to find an element in the native context', async () => {19 const el = await driver.findNativeElementOrElements('accessibility id', 'Buttons', false);20 expect(el).to.exist;21 });22 it('should be able to find elements in the native context', async () => {23 const els = await driver.findNativeElementOrElements('accessibility id', 'Button', true);24 expect(els).to.exist;25 expect(els).to.have.length(3);26 });27 after(async () => {28 await driver.quit();29 });30});31const wd = require('wd');32const chai = require('chai');33const chaiAsPromised = require('chai-as-promised');34chai.use(chaiAsPromised);35const expect = chai.expect;36const should = chai.should();37chaiAsPromised.transferPromiseness = wd.transferPromiseness;38const serverConfig = {39};40const capabilities = {

Full Screen

Using AI Code Generation

copy

Full Screen

1const appium = require('appium');2const { XCUITestDriver } = appium;3const driver = new XCUITestDriver();4const element = driver.findNativeElementOrElements('accessibility id', 'elementID', false);5console.log(element);6findNativeElementOrElements(strategy, selector, mult) {7 return this.findElOrEls(strategy, selector, mult, true);8 }9async findElOrEls(strategy, selector, mult, context) {10 const el = await this.proxyCommand('/element', 'POST', {using: strategy, value: selector});11 return mult ? [el] : el;12 }13async proxyCommand(url, method, body = null) {14 return await this.proxyCommandTo(url, method, this.sessionId, body);15 }16async proxyCommandTo(url, method, sessionId, body = null) {17 return await this.proxyCommandToMJSONWP(url, method, sessionId, body);18 }19async proxyCommandToMJSONWP(url, method, sessionId, body = null) {20 const {protocol, host, port} = this;21 const reqOpts = {22 headers: {23 'Content-Type': 'application/json; charset=utf-8',24 },25 };26 const res = await request(reqOpts);27 if (res.statusCode === 200) {28 return res.body.value;29 }30 throw new Error(`Could not proxy command to remote server. Original error: ${res.body.value.message}`);31 }32function request(uri, options, callback) {33 if (typeof uri === 'undefined

Full Screen

Using AI Code Generation

copy

Full Screen

1const { execSync } = require('child_process');2const fs = require('fs');3const path = require('path');4const appium = require('appium');5const wd = require('wd');6const PORT = 4723;

Full Screen

Using AI Code Generation

copy

Full Screen

1const el = await this.findNativeElementOrElements('accessibility id', 'test', true);2console.log(el);3async findNativeElementOrElements (strategy, selector, multiple, context = null) {4 if (strategy === 'accessibility id') {5 const predicateString = `label == '${selector}'`;6 return await this.findElOrEls('predicate string', predicateString, multiple, context);7 }8 return await this.findElOrEls(strategy, selector, multiple, context);9 }10async findElOrEls (strategy, selector, multiple, context = null) {11 const command = multiple ? 'findElements' : 'findElement';12 const params = {strategy, selector, context};13 return await this.proxyCommand(command, params);14 }15async proxyCommand (command, params = {}) {16 return await this.proxy(command, params);17 }18async proxy (command, params = {}) {19 params = _.clone(params);20 if (this.opts.sessionOverride && !params.sessionId) {21 params.sessionId = this.opts.sessionOverride;22 }23 return await this.executeCommand(command, params);24 }25async executeCommand (cmd, opts = {}) {26 if (!this.wda) {27 throw new Error('Trying to proxy command to non-existent WDA session');28 }29 return await this.wda.executeCommand(cmd, opts);30 }31async executeCommand (cmd, opts = {}) {32 if (!this.url) {33 throw new Error(`Tried to proxy command to non-running WebDriverAgent`);34 }35 const {36 } = opts;37 const url = `${this.url

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Sign up Free
_

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful