Best JavaScript code snippet using appium
js-wdio.js
Source:js-wdio.js  
...108  codeFor_getClipboard () {109    return `let clipboardText = await driver.getClipboard();`;110  }111  codeFor_setClipboard (varNameIgnore, varIndexIgnore, clipboardText) {112    return `await driver.setClipboard('${clipboardText}')`;113  }114  codeFor_pressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {115    return `await driver.longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;116  }117  codeFor_longPressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {118    return `await driver.longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;119  }120  codeFor_hideDeviceKeyboard () {121    return `await driver.hideDeviceKeyboard();`;122  }123  codeFor_isKeyboardShown () {124    return `//isKeyboardShown not supported`;125  }126  codeFor_pushFileToDevice (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {...js-wd.js
Source:js-wd.js  
...103  codeFor_getClipboard () {104    return `let clipboardText = await driver.getClipboard();`;105  }106  codeFor_setClipboard (varNameIgnore, varIndexIgnore, clipboardText) {107    return `await driver.setClipboard('${clipboardText}')`;108  }109  codeFor_pressKeyCode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {110    return `await driver.longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;111  }112  codeFor_longPressKeyCode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {113    return `await driver.longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;114  }115  codeFor_hideKeyboard () {116    return `await driver.hideKeyboard();`;117  }118  codeFor_isKeyboardShown () {119    return `await driver.isKeyboardShown();`;120  }121  codeFor_pushFile (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {...js-oxygen.js
Source:js-oxygen.js  
1import Framework from './framework';2class JsOxygenFramework extends Framework {3  get language () {4    return 'js';5  }6  wrapWithBoilerplate (code) {7    let caps = JSON.stringify(this.caps);8    let url = JSON.stringify(`${this.scheme}://${this.host}:${this.port}${this.path}`);9    return `// Requires the Oxygen HQ client library10// (npm install oxygen-cli -g)11// Then paste this into a .js file and run with:12// oxygen <file>.js13mob.init(${caps}, ${url});14${code}15`;16  }17  codeFor_findAndAssign (strategy, locator, localVar, isArray) {18    // wdio has its own way of indicating the strategy in the locator string19    switch (strategy) {20      case 'xpath': break; // xpath does not need to be updated21      case 'accessibility id': locator = `~${locator}`; break;22      case 'id': locator = `id=${locator}`; break;23      case 'name': locator = `name=${locator}`; break;24      case 'class name': locator = `css=${locator}`; break;25      case '-android uiautomator': locator = `android=${locator}`; break;26      case '-android datamatcher': locator = `android=${locator}`; break;27      case '-ios predicate string': locator = `ios=${locator}`; break;28      case '-ios class chain': locator = `ios=${locator}`; break; // TODO: Handle IOS class chain properly. Not all libs support it. Or take it out29      default: throw new Error(`Can't handle strategy ${strategy}`);30    }31    if (isArray) {32      return `let ${localVar} = mob.findElements(${JSON.stringify(locator)});`;33    } else {34      return `let ${localVar} = mob.findElement(${JSON.stringify(locator)});`;35    }36  }37  codeFor_click (varName, varIndex) {38    return `mob.click(${this.getVarName(varName, varIndex)});`;39  }40  codeFor_clear (varName, varIndex) {41    return `mob.clear(${this.getVarName(varName, varIndex)});`;42  }43  codeFor_sendKeys (varName, varIndex, text) {44    return `mob.type(${this.getVarName(varName, varIndex)}, ${JSON.stringify(text)});`;45  }46  codeFor_back () {47    return `mob.back();`;48  }49  codeFor_tap (varNameIgnore, varIndexIgnore, x, y) {50    return `mob.tap(${x}, ${y});`;51  }52  codeFor_swipe (varNameIgnore, varIndexIgnore, x1, y1, x2, y2) {53    return `mob.swipeScreen(${x1}, ${y1}, ${x2}, ${y2});`;54  }55  codeFor_getCurrentActivity () {56    return `let activityName = mob.getCurrentActivity();`;57  }58  codeFor_getCurrentPackage () {59    return `let packageName = mob.getCurrentPackage();`;60  }61  codeFor_installAppOnDevice (varNameIgnore, varIndexIgnore, app) {62    return `mob.installApp('${app}');`;63  }64  codeFor_isAppInstalledOnDevice (varNameIgnore, varIndexIgnore, app) {65    return `let isAppInstalled = mob.isAppInstalled("${app}");`;66  }67  codeFor_launchApp () {68    return `mob.launchApp();`;69  }70  codeFor_backgroundApp (varNameIgnore, varIndexIgnore, timeout) {71    return `mob.driver().background(${timeout});`;72  }73  codeFor_closeApp () {74    return `mob.closeApp();`;75  }76  codeFor_resetApp () {77    return `mob.resetApp();`;78  }79  codeFor_removeAppFromDevice (varNameIgnore, varIndexIgnore, app) {80    return `mob.removeApp('${app}')`;81  }82  codeFor_getAppStrings (varNameIgnore, varIndexIgnore, language, stringFile) {83    return `let appStrings = mob.driver().getAppStrings(${language ? `${language}, ` : ''}${stringFile ? `"${stringFile}` : ''});`;84  }85  codeFor_getClipboard () {86    return `let clipboardText = mob.driver().getClipboard();`;87  }88  codeFor_setClipboard (varNameIgnore, varIndexIgnore, clipboardText) {89    return `mob.driver().setClipboard('${clipboardText}')`;90  }91  codeFor_pressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {92    return `mob.driver().longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;93  }94  codeFor_longPressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {95    return `mob.driver().longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;96  }97  codeFor_hideDeviceKeyboard () {98    return `mob.driver().hideKeyboard();`;99  }100  codeFor_isKeyboardShown () {101    return `//isKeyboardShown not supported`;102  }103  codeFor_pushFileToDevice (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {104    return `mob.driver().pushFile('${pathToInstallTo}', '${fileContentString}');`;105  }106  codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {107    return `let data = mob.driver().pullFile('${pathToPullFrom}');`;108  }109  codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {110    return `let data = mob.driver().pullFolder('${folderToPullFrom}');`;111  }112  codeFor_toggleAirplaneMode () {113    return `mob.driver().toggleAirplaneMode();`;114  }115  codeFor_toggleData () {116    return `mob.driver().toggleData();`;117  }118  codeFor_toggleWiFi () {119    return `mob.driver().toggleWiFi();`;120  }121  codeFor_toggleLocationServices () {122    return `mob.driver().toggleLocationServices();`;123  }124  codeFor_sendSMS () {125    return `// Not supported: sendSms;`;126  }127  codeFor_gsmCall () {128    return `// Not supported: gsmCall`;129  }130  codeFor_gsmSignal () {131    return `// Not supported: gsmSignal`;132  }133  codeFor_gsmVoice () {134    return `// Not supported: gsmVoice`;135  }136  codeFor_shake () {137    return `mob.shake();`;138  }139  codeFor_lock (varNameIgnore, varIndexIgnore, seconds) {140    return `mob.driver().lock(${seconds});`;141  }142  codeFor_unlock () {143    return `mob.driver().unlock();`;144  }145  codeFor_isLocked () {146    return `let isLocked = mob.driver().isLocked();`;147  }148  codeFor_rotateDevice (varNameIgnore, varIndexIgnore, x, y, radius, rotation, touchCount, duration) {149    return `mob.driver().rotateDevice(${x}, ${y}, ${radius}, ${rotation}, ${touchCount}, ${duration});`;150  }151  codeFor_getPerformanceData () {152    return `// Not supported: getPerformanceData`;153  }154  codeFor_getSupportedPerformanceDataTypes () {155    return `// Not supported: getSupportedPerformanceDataTypes`;156  }157  codeFor_performTouchId (varNameIgnore, varIndexIgnore, match) {158    return `mob.driver().touchId(${match});`;159  }160  codeFor_toggleTouchIdEnrollment (varNameIgnore, varIndexIgnore, enroll) {161    return `mob.driver().toggleEnrollTouchId(${enroll});`;162  }163  codeFor_openNotifications () {164    return `mob.driver().openNotifications();`;165  }166  codeFor_getDeviceTime () {167    return `let time = mob.getDeviceTime();`;168  }169  codeFor_fingerprint (varNameIgnore, varIndexIgnore, fingerprintId) {170    return `mob.driver().fingerPrint(${fingerprintId});`;171  }172  codeFor_sessionCapabilities () {173    return `let caps = mob.driver().capabilities;`;174  }175  codeFor_setPageLoadTimeout (varNameIgnore, varIndexIgnore, ms) {176    return `mob.driver().setTimeout({'pageLoad': ${ms}});`;177  }178  codeFor_setAsyncScriptTimeout (varNameIgnore, varIndexIgnore, ms) {179    return `mob.driver().setTimeout({'script': ${ms}});`;180  }181  codeFor_setImplicitWaitTimeout (varNameIgnore, varIndexIgnore, ms) {182    return `mob.driver().setTimeout({'implicit': ${ms}});`;183  }184  codeFor_setCommandTimeout () {185    return `// Not supported: setCommandTimeout`;186  }187  codeFor_getOrientation () {188    return `let orientation = mob.driver().getOrientation();`;189  }190  codeFor_setOrientation (varNameIgnore, varIndexIgnore, orientation) {191    return `mob.driver().setOrientation("${orientation}");`;192  }193  codeFor_getGeoLocation () {194    return `let location = mob.driver().getGeoLocation();`;195  }196  codeFor_setGeoLocation (varNameIgnore, varIndexIgnore, latitude, longitude, altitude) {197    return `mob.driver().setGeoLocation({latitude: ${latitude}, longitude: ${longitude}, altitude: ${altitude}});`;198  }199  codeFor_logTypes () {200    return `let logTypes = mob.driver().getLogTypes();`;201  }202  codeFor_log (varNameIgnore, varIndexIgnore, logType) {203    return `let logs = mob.driver().getLogs('${logType}');`;204  }205  codeFor_updateSettings (varNameIgnore, varIndexIgnore, settingsJson) {206    return `mob.driver().updateSettings(${settingsJson});`;207  }208  codeFor_settings () {209    return `let settings = mob.driver().getSettings();`;210  }211}212JsOxygenFramework.readableName = 'JS - Oxygen HQ';...gutenberg-editor-paste.test.js
Source:gutenberg-editor-paste.test.js  
...18		} );19		return;20	}21	beforeAll( async () => {22		await editorPage.driver.setClipboard( '', 'plaintext' );23	} );24	it( 'copies plain text from one paragraph block and pastes in another', async () => {25		await editorPage.addNewBlock( blockNames.paragraph );26		const paragraphBlockElement = await editorPage.getBlockAtPosition(27			blockNames.paragraph28		);29		if ( isAndroid() ) {30			await paragraphBlockElement.click();31		}32		await editorPage.typeTextToParagraphBlock(33			paragraphBlockElement,34			testData.pastePlainText35		);36		const textViewElement = await editorPage.getTextViewForParagraphBlock(...clipboard-e2e-specs.js
Source:clipboard-e2e-specs.js  
...13  after(async function () {14    await deleteSession();15  });16  it('should set and get clipboard', async function () {17    await driver.setClipboard(new Buffer.from('Hello').toString('base64'), 'plaintext');18    // 'SGVsbG8=' is 'Hello' in base 64 encoding with a new line.19    const text = await driver.getClipboard('PLAINTEXT');20    try {21      text.should.eql('SGVsbG8=');22    } catch (AssertionError) {23      // API level 23 and 25 emulator has '\n'24      text.should.eql('SGVsbG8=\n');25    }26    (Buffer.from(text, 'base64').toString()).should.eql('Hello');27  });...Using AI Code Generation
1driver.setClipboard("Hello World");2driver.getClipboard();3driver.isKeyboardShown();4driver.hideKeyboard();5driver.pressKeyCode(66);6driver.longPressKeyCode(66);7driver.currentActivity();8driver.startActivity("com.android.calculator2", "com.android.calculator2.Calculator");9driver.isAppInstalled("com.android.calculator2");10driver.removeApp("com.android.calculator2");11driver.launchApp();12driver.closeApp();13driver.resetApp();14driver.backgroundApp(5);15driver.lock(5);16driver.unlock();17driver.isLocked();18driver.rotate(DeviceRotation.LANDSCAPE);19driver.getOrientation();20driver.setOrientation(DeviceRotation.LANDSCAPE);21driver.getGeoLocation();22driver.setGeoLocation(new Location(37.423114, -122.083956, 100.123456));23driver.getPerformanceData("com.example.android.apis", "memoryinfo", 5);24driver.getPerformanceDataTypes();Using AI Code Generation
1driver.setClipboard("Hello World", "plaintext");2driver.getClipboard("plaintext");3driver.performTouchId(true);4driver.toggleAirplaneMode();5driver.toggleData();6driver.toggleWiFi();7driver.toggleLocationServices();8driver.getSettings();9driver.updateSettings({"ignoreUnimportantViews": false});10driver.openNotifications();11driver.startActivity("io.appium.android.apis", ".ApiDemos");12driver.backgroundApp(5);13driver.hideKeyboard();14driver.pressKeyCode(82);15driver.longPressKeyCode(82);16driver.isKeyboardShown();17driver.launchApp();18driver.closeApp();19driver.resetApp();20driver.removeApp("io.appium.android.apis");21driver.installApp("/path/to/my.apk");22driver.terminateApp("io.appium.android.apis");23driver.activateApp("io.appium.android.apis");24driver.getCurrentActivity();25driver.getDeviceTime();26driver.getPerformanceDataTypes();27driver.getPerformanceData("Using AI Code Generation
1driver.setClipboard({content: 'test', label: 'test'})2driver.getClipboard('test')3await driver.setClipboard({content: 'test', label: 'test'})4await driver.getClipboard('test')5await driver.setClipboard({content: 'test', label: 'test'})6await driver.getClipboard('test')7await driver.setClipboard({content: 'test', label: 'test'})8await driver.getClipboard('test')9await driver.setClipboard({content: 'test', label: 'test'})10await driver.getClipboard('test')11await driver.setClipboard({content: 'test', label: 'test'})12await driver.getClipboard('test')13await driver.setClipboard({content: 'test', label: 'test'})14await driver.getClipboard('test')15await driver.setClipboard({content: 'test', label: 'test'})16await driver.getClipboard('test')17await driver.setClipboard({content: 'test', label: 'test'})18await driver.getClipboard('test')19await driver.setClipboard({content: 'test', label: 'test'})20await driver.getClipboard('testUsing AI Code Generation
1driver.setClipboard('text/plain', 'Hello World');2driver.setClipboard('text/plain', 'Hello World');3[HTTP] {"contentType":"text/plain","content":"Hello World"}4[W3C] Calling AppiumDriver.setClipboard() with args: ["text/plain","Hello World","1e8d7f6c-2e0c-4c2b-9e9e-9d6c7c1a1b6a"]5[AndroidDriver]     at AndroidDriver.callee$0$0$ (/usr/local/lib/node_modules/appium/node_modules/appium-android-driver/lib/commands/clipboard.js:11:11)6[AndroidDriver]     at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:62:40)7[AndroidDriver]     at Generator.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:296:22)8[AndroidDriver]     at Generator.prototype.(anonymous function) [as next] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:114:21)9[AndroidDriver]     at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:62:40)10[AndroidDriver]     at invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:138:20)11[AndroidDriver]     at run (/usr/local/lib/node_modules/appium/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:75:22)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!!
