Best JavaScript code snippet using appium
element-e2e-specs.js
Source:element-e2e-specs.js
...93 describe('touch click', () => {94 it('should click an element', async () => {95 let el = await driver.elementByAccessibilityId('Buttons');96 await el.tap();97 (await driver.elementsByClassName('XCUIElementTypeButton')).should.have.length.above(4);98 await driver.back();99 });100 });101 describe('interactions', function () {102 describe('text fields', () => {103 let text1 = 'bunchoftext';104 let text2 = 'differenttext';105 let text3 = 'http://appium.io/';106 let secureText = _.map(new Array(text1.length), () => 'â¢').join('');107 let phText = 'Placeholder text';108 beforeEach(async function () {109 let el = await driver.elementByAccessibilityId('Text Fields');110 await driver.execute('mobile: scroll', {element: el, toVisible: true});111 await el.click();112 });113 afterEach(async () => {114 await driver.back();115 });116 describe('set value', () => {117 it('should type in the text field', async () => {118 let el = await driver.elementByClassName('XCUIElementTypeTextField');119 await el.type(text1);120 let text = await el.text();121 text.should.eql(text1);122 });123 it('should type in the text field even before the keyboard is up', async () => {124 let el = await driver.elementByClassName('XCUIElementTypeTextField');125 await el.type(text1);126 let text = await el.text();127 text.should.eql(text1);128 });129 it('should type a url in the text field', async () => {130 // in Travis this sometimes gets the wrong text131 let retries = process.env.TRAVIS ? 5 : 1;132 await retryInterval(retries, 100, async () => {133 let el = await driver.elementByClassName('XCUIElementTypeTextField');134 await el.clear();135 await el.type(text3);136 let text = await el.text();137 text.should.eql(text3);138 });139 });140 it('should be able to type into two text fields', async () => {141 let els = await driver.elementsByClassName('XCUIElementTypeTextField');142 await els[0].type(text1);143 await driver.hideKeyboard();144 await els[1].type(text2);145 let text = await els[0].text();146 text.should.eql(text1);147 text = await els[1].text();148 text.should.eql(text2);149 });150 it('should type in a secure text field', async () => {151 let els = await driver.elementsByClassName('XCUIElementTypeSecureTextField');152 await els[0].type(text1);153 let text = await els[0].text();154 text.should.not.eql(text1);155 text.length.should.eql(text1.length);156 text.should.eql(secureText);157 });158 it('should type a backspace', async () => {159 let el = await driver.elementByClassName('XCUIElementTypeTextField');160 await driver.type(el, ['0123456789\uE003']);161 let text = await el.text();162 text.should.eql('012345678');163 });164 it('should type a delete', async () => {165 let el = await driver.elementByClassName('XCUIElementTypeTextField');166 await driver.type(el, ['0123456789\ue017']);167 let text = await el.text();168 text.should.eql('012345678');169 });170 it('should type a newline', async () => {171 let el = await driver.elementByClassName('XCUIElementTypeTextField');172 await driver.type(el, ['0123456789\uE006']);173 let text = await el.text();174 text.should.eql('0123456789');175 });176 });177 describe('clear', () => {178 it('should clear a text field', async () => {179 let el = await driver.elementByClassName('XCUIElementTypeTextField');180 await el.type(text1);181 let text = await el.text();182 text.should.eql(text1);183 await el.clear();184 text = await el.text();185 text.should.eql(phText);186 });187 it('should be able to clear two text fields', async () => {188 let els = await driver.elementsByClassName('XCUIElementTypeTextField');189 await els[0].type(text1);190 let text = await els[0].text();191 text.should.eql(text1);192 await driver.hideKeyboard();193 await els[1].type(text2);194 text = await els[1].text();195 text.should.eql(text2);196 await els[0].clear();197 text = await els[0].text();198 text.should.eql(phText);199 await driver.hideKeyboard();200 await els[1].clear();201 text = await els[1].text();202 text.should.eql(phText);203 });204 it('should clear a secure text field', async () => {205 let el = await driver.elementByClassName('XCUIElementTypeSecureTextField');206 await el.type(text1);207 let text = await el.text();208 text.should.eql(secureText);209 await el.clear();210 text = await el.text();211 text.should.eql(phText);212 });213 });214 describe('keys', () => {215 it('should be able to send text to the active element', async () => {216 let el = await driver.elementByClassName('XCUIElementTypeTextField');217 // make sure the keyboard is up218 await el.click();219 await driver.keys('this is a test');220 });221 it('should type a backspace', async () => {222 let el = await driver.elementByClassName('XCUIElementTypeTextField');223 // make sure the keyboard is up224 await el.click();225 await driver.keys('0123456789\uE003');226 let text = await el.text();227 text.should.eql('012345678');228 });229 it('should type a delete', async () => {230 let el = await driver.elementByClassName('XCUIElementTypeTextField');231 // make sure the keyboard is up232 await el.click();233 await driver.keys('0123456789\ue017');234 let text = await el.text();235 text.should.eql('012345678');236 });237 it('should type a newline', async () => {238 let el = await driver.elementByClassName('XCUIElementTypeTextField');239 // make sure the keyboard is up240 await el.click();241 await driver.keys('0123456789\uE006');242 let text = await el.text();243 text.should.eql('0123456789');244 });245 });246 describe('hide keyboard', () => {247 it('should be able to hide the keyboard', async () => {248 let el = await driver.elementByClassName('XCUIElementTypeTextField');249 await el.click();250 let db = await driver.elementByAccessibilityId('Done');251 (await db.isDisplayed()).should.be.true;252 await driver.hideKeyboard();253 // pause for a second to allow keyboard to go out of view254 // otherwise slow systems will reject the search for `Done` and255 // fast ones will get the element but it will be invisible256 await B.delay(1000);257 db = await driver.elementByAccessibilityId('Done').should.eventually.be.rejected;258 });259 });260 });261 describe('picker wheel', () => {262 it('should be able to set the value', async () => {263 let el = await driver.elementByAccessibilityId('Picker View');264 await el.click();265 let wheels = await driver.elementsByClassName('XCUIElementTypePickerWheel');266 let values = [65, 205, 120];267 for (let i = 0; i < 3; i++) {268 let wheel = wheels[i];269 let value = await wheel.getAttribute('value');270 parseInt(value, 10).should.eql(values[i]);271 await wheel.type(150);272 value = await wheel.getAttribute('value');273 parseInt(value, 10).should.eql(150);274 }275 });276 });277 });...
find-element-specs.js
Source:find-element-specs.js
...79 els[0].value.should.exist;80 }).nodeify(done);81 });82 it('should not find any elements on the app but fail gracefully', function (done) {83 driver.elementsByClassName('UIAButtonNotThere').should.eventually.have.length(0)84 .nodeify(done);85 });86 it('should find element by valid name', function (done) {87 driver.elementByName('ComputeSumButton').should.eventually.exist88 .nodeify(done);89 });90 it('should not find element by invalid name but return respective error code', function (done) {91 driver.elementByName('InvalidNameForElement')92 .catch(function (err) {93 err['jsonwire-error'].summary.should.eql('NoSuchElement');94 throw err;95 }).should.be.rejectedWith(/status: 7/)96 .nodeify(done);97 });98 it('should find an element with accessibility id containing an apostrophe', function (done) {99 driver.element('accessibility id', "Access'ibility").then(function (el) {100 el.should.exist;101 }).nodeify(done);102 });103 it('should not find element by incomplete class name but return respective error code', function (done) {104 driver.elementsByClassName('notAValidReference')105 .catch(function (err) {106 err['jsonwire-error'].summary.should.eql('UnknownError');107 throw err;108 }).should.be.rejectedWith(/status: 13/)109 .nodeify(done);110 });111 it('should find multiple elements by valid name', function (done) {112 driver.elementsByName('AppElem').should.eventually.have.length(3)113 .nodeify(done);114 });...
long-click-specs.js
Source:long-click-specs.js
1"use strict";2var env = require('../../../../helpers/env')3 , setup = require("../../../common/setup-base")4 , desired = require("../desired")5 , reset = require("../reset")6 , droidText = 'android.widget.TextView'7 , Q = require('q');8describe("apidemo - gestures - long click", function () {9 var driver;10 setup(this, desired).then(function (d) { driver = d; });11 if (env.FAST_TESTS) {12 beforeEach(function () {13 return reset(driver);14 });15 }16 if (env.FAST_TESTS) {17 beforeEach(function (done) {18 function back(depth) {19 if (depth < 0) return new Q();20 return driver21 .elementByNameOrNull("Animation")22 .then(function (el) {23 if (el) return;24 else {25 return driver.back().then(function () {26 back(depth - 1);27 });28 }29 });30 }31 back(3).nodeify(done);32 });33 }34 it('should long click via element value', function (done) {35 var element;36 driver37 .elementsByClassName(droidText).then(function (els) { element = els[1]; })38 .then(function () { driver.execute("mobile: longClick", [{element: element.value}]); })39 .sleep(3000)40 .elementsByClassName(droidText).then(function (els) { return els[1]; }).text()41 .then(function (text) {42 ["Accessibility Node Provider"].should.include(text);43 }).nodeify(done);44 });45 it('should long click via element value with custom duration', function (done) {46 var element;47 driver48 .elementsByClassName(droidText).then(function (els) { element = els[1]; })49 .then(function () { driver.execute("mobile: longClick", [{element: element.value, duration: 1000}]); })50 .sleep(3000)51 .elementsByClassName(droidText).then(function (els) { return els[1]; }).text()52 .then(function (text) {53 ["Accessibility Node Provider"].should.include(text);54 }).nodeify(done);55 });56 it('should long click via pixel value', function (done) {57 var element, location, elSize;58 driver59 .elementsByClassName(droidText).then(function (els) { element = els[1]; })60 .then(function () { return element.getLocation(); })61 .then(function (loc) { location = loc; })62 .then(function () { return element.getSize(); })63 .then(function (size) { elSize = size; })64 .then(function () {65 var centerX = location.x + (elSize.width / 2);66 var centerY = location.y + (elSize.height / 2);67 driver.execute("mobile: longClick", [{x: centerX, y: centerY}]);68 })69 .sleep(3000)70 .elementsByClassName(droidText).then(function (els) { return els[1]; }).text()71 .then(function (text) {72 ["Accessibility Node Provider"].should.include(text);73 }).nodeify(done);74 });75 it('should long click via relative value', function (done) {76 var element, location, elSize, windowSize;77 driver78 .elementsByClassName(droidText).then(function (els) { element = els[1]; })79 .then(function () { return element.getLocation(); })80 .then(function (loc) { location = loc; })81 .then(function () { return element.getSize(); })82 .then(function (size) { elSize = size; })83 .then(function () { return driver.getWindowSize(); })84 .then(function (size) { windowSize = size; })85 .then(function () {86 var relX = (location.x + (elSize.width / 2)) / windowSize.width;87 var relY = (location.y + (elSize.height / 2)) / windowSize.height;88 driver.execute("mobile: longClick", [{x: relX, y: relY}]);89 })90 .sleep(3000)91 .elementsByClassName(droidText).then(function (els) { return els[1]; }).text()92 .then(function (text) {93 ["Accessibility Node Provider"].should.include(text);94 }).nodeify(done);95 });...
execute-specs.js
Source:execute-specs.js
1"use strict";2var env = require('../../../../helpers/env')3 , setup = require("../../../common/setup-base")4 , desired = require("../desired")5 , reset = require("../reset")6 , droidText = 'android.widget.TextView';7describe("apidemo - gestures - execute", function () {8 var driver;9 setup(this, desired).then(function (d) { driver = d; });10 if (env.FAST_TESTS) {11 beforeEach(function () {12 return reset(driver);13 });14 }15 it('should execute down/move/up via element value', function (done) {16 var element;17 driver18 .elementsByClassName(droidText).then(function (els) { element = els[1]; })19 .then(function () { driver.execute("mobile: down", [{element: element.value}]); })20 .sleep(3000)21 .then(function () { driver.execute("mobile: move", [{element: element.value}]); })22 .sleep(3000)23 .then(function () { driver.execute("mobile: up", [{element: element.value}]); })24 .sleep(3000)25 .elementsByClassName(droidText).then(function (els) { return els[1]; }).text()26 .then(function (text) {27 ["Accessibility Node Provider"].should.include(text);28 }).nodeify(done);29 });30 it('should execute down/move/up click via pixel value', function (done) {31 var element, location, elSize, centerX, centerY;32 driver33 .elementsByClassName(droidText).then(function (els) { element = els[1]; })34 .then(function () { return element.getLocation(); })35 .then(function (loc) { location = loc; })36 .then(function () { return element.getSize(); })37 .then(function (size) { elSize = size; })38 .then(function () {39 centerX = location.x + (elSize.width / 2);40 centerY = location.y + (elSize.height / 2);41 driver.execute("mobile: down", [{x: centerX, y: centerY}]);42 })43 .sleep(3000)44 .then(function () { driver.execute("mobile: move", [{x: centerX, y: centerY}]); })45 .sleep(3000)46 .then(function () { driver.execute("mobile: up", [{x: centerX, y: centerY}]); })47 .sleep(3000)48 .elementsByClassName(droidText).then(function (els) { return els[1]; }).text()49 .then(function (text) {50 ["Accessibility Node Provider"].should.include(text);51 }).nodeify(done);52 });53 it('should execute down/move/up via relative value', function (done) {54 var element, location, elSize, windowSize, relX, relY;55 driver56 .elementsByClassName(droidText).then(function (els) { element = els[1]; })57 .then(function () { return element.getLocation(); })58 .then(function (loc) { location = loc; })59 .then(function () { return element.getSize(); })60 .then(function (size) { elSize = size; })61 .then(function () { return driver.getWindowSize(); })62 .then(function (size) { windowSize = size; })63 .then(function () {64 relX = (location.x + (elSize.width / 2)) / windowSize.width;65 relY = (location.y + (elSize.height / 2)) / windowSize.height;66 driver.execute("mobile: down", [{x: relX, y: relY}]);67 })68 .sleep(3000)69 .then(function () { driver.execute("mobile: move", [{x: relX, y: relY}]); })70 .sleep(3000)71 .then(function () { driver.execute("mobile: up", [{x: relX, y: relY}]); })72 .sleep(3000)73 .elementsByClassName(droidText).then(function (els) { return els[1]; }).text()74 .then(function (text) {75 ["Accessibility Node Provider"].should.include(text);76 }).nodeify(done);77 });...
keyboard-specs.js
Source:keyboard-specs.js
1"use strict";2var setup = require("../../common/setup-base")3 , desired = require('./desired')4 , _ = require('underscore')5 , unorm = require('unorm');6describe('testapp - keyboard', function () {7 var ctx = this;8 var test = function (strategy) {9 describe('typing with strategy:' + (strategy || 'undefined'), function () {10 var text = 'Appium Rocks';11 var driver;12 var _desired = _.clone(desired);13 if (strategy) _desired.sendKeyStrategy = strategy;14 setup(ctx, _desired).then(function (d) { driver = d; });15 it("should send keys to a text field", function (done) {16 driver17 .execute('env')18 .then( function (env) {19 if (strategy) env.sendKeyStrategy.should.equal(strategy);20 else env.sendKeyStrategy.should.equal('oneByOne');21 })22 .elementByClassName('UIATextField')23 .clear()24 .sendKeys(text)25 .text().then(function (text) {26 if (strategy === 'grouped') {27 text.length.should.be.above(0);28 } else {29 text.should.equal(text);30 }31 }).nodeify(done);32 });33 });34 };35 _.each([undefined, 'oneByOne', 'grouped', 'setValue'], test);36 describe("typing", function () {37 var driver;38 setup(this, _.defaults({39 deviceName: 'iPad 2',40 }, desired)).then(function (d) { driver = d; });41 describe("stability @skip-ci", function () {42 var runs = 1043 , text = 'Delhi is New @@@ BREAKFAST-FOOD-0001';44 var test = function () {45 it("should send keys to a text field", function (done) {46 driver47 .elementByClassName('UIATextField')48 .clear()49 .sendKeys(text)50 .text().should.become(text)51 .nodeify(done);52 });53 };54 for (var n = 0; n < runs; n++) {55 describe('sendKeys test ' + (n + 1), test);56 }57 });58 it('should send accented text', function (done) {59 var testText = unorm.nfd("é Šù á¸");60 driver61 .elementsByClassName('UIATextField').at(1)62 .sendKeys(testText)63 .text()64 .should.become(testText)65 .nodeify(done);66 });67 it('should send backspace key', function (done) {68 driver69 .elementsByClassName('UIATextField').at(1)70 .clear()71 .sendKeys("abcd")72 .sendKeys('\uE003\uE003')73 .text()74 .should.become("ab")75 .nodeify(done);76 });77 it('should send delete key', function (done) {78 driver79 .elementsByClassName('UIATextField').at(1)80 .clear()81 .sendKeys("abcd")82 .sendKeys('\ue017\ue017')83 .text()84 .should.become("ab")85 .nodeify(done);86 });87 });...
find-basic-e2e-specs.js
Source:find-basic-e2e-specs.js
...22 const text = await el.text();23 text.toLowerCase().should.equal('api demos');24 });25 it('should find multiple elements by class name', async function () {26 await driver.elementsByClassName('android.widget.TextView')27 .should.eventually.have.length.at.least(10);28 });29 it('should not find multiple elements that doesnt exist', async function () {30 await driver.elementsByClassName('blargimarg')31 .should.eventually.have.length(0);32 });33 it('should fail on empty locator', async function () {34 await driver.elementsByClassName('').should.be.rejectedWith(/selector/);35 });36 it('should find a single element by resource-id', async function () {37 await driver.elementById(`android:id/${singleResourceId}`)38 .should.eventually.exist;39 });40 it('should find multiple elements by resource-id', async function () {41 await driver.elementsById('android:id/text1')42 .should.eventually.have.length.at.least(10);43 });44 it('should find multiple elements by resource-id even when theres just one', async function () {45 await driver.elementsById(`android:id/${singleResourceId}`)46 .should.eventually.have.length(1);47 });48 describe('implicit wait', function () {...
screen.js
Source:screen.js
1const fs = require('fs');2const logger = require('../lib/logger');3exports.getScreenSize = (driver, platform, callback) => {4 if (platform === 'android') {5 return driver6 .elementsByClassName('android.widget.FrameLayout')7 .first()8 .getSize()9 .then(callback);10 }11 return driver12 .elementsByClassName('XCUIElementTypeWindow')13 .first()14 .getSize()15 .then(callback);16};17exports.takeScreenshot = (driver, path, screen, sleep = 2000) => {18 const filename = `${path}/${screen}.png`;19 return (20 driver21 .sleep(sleep)22 // base64 screeshot23 .takeScreenshot()24 .should.eventually.exist// save screenshot to local file25 .then(() => {26 try {27 fs.unlinkSync(filename);28 } catch (error) {29 if(error) {30 logger.error(error);31 }32 }33 return fs.existsSync(filename).should.not.be.ok;34 })35 .saveScreenshot(filename)36 .then(() => {37 return fs.existsSync(filename).should.be.ok;38 })39 );...
rotation-gesture-specs.js
Source:rotation-gesture-specs.js
...5describe('testapp - rotation gesture', function () {6 var driver;7 setup(this, desired).then(function (d) { driver = d; });8 it('should rotate map after tapping Test Gesture', function (done) {9 driver.elementsByClassName('UIAButton')10 .then(function (buttons) { return buttons[5].click(); })11 .sleep(1000).then(function () { okIfAlert(driver); })12 .elementsByClassName('UIAMap')13 .rotate({x: 114, y: 198, duration: 5, radius: 3, rotation: 220, touchCount: 2})14 .nodeify(done);15 });...
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var chai = require('chai');4var chaiAsPromised = require('chai-as-promised');5chai.use(chaiAsPromised);6var expect = chai.expect;7var should = chai.should();8var desiredCaps = {9};10var driver = wd.promiseChainRemote('localhost', 4723);11driver.init(desiredCaps);12driver.elementsByClassName('android.widget.Button').then(function(buttons) {13 buttons.length.should.equal(2);14});15driver.quit();16 1 passing (3s)17var wd = require('wd');18var assert = require('assert');19var chai = require('chai');20var chaiAsPromised = require('chai-as-promised');21chai.use(chaiAsPromised);22var expect = chai.expect;23var should = chai.should();24var desiredCaps = {25};26var driver = wd.promiseChainRemote('localhost', 4723);27driver.init(desiredCaps);28 buttons.length.should.equal(1);29});30driver.quit();
Using AI Code Generation
1var webdriver = require('webdriverio');2var options = {3 desiredCapabilities: {4 }5};6 .remote(options)7 .init()8 .elementsByClassName('android.widget.Button')9 .then(function (buttons) {10 buttons[0].click();11 buttons[1].click();12 buttons[2].click();13 })14 .end();
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desired = {4};5var driver = wd.promiseChainRemote('localhost', 4723);6 .init(desired)7 .elementsByClassName('android.widget.TextView')8 .then(function(elements) {9 assert.ok(elements.length > 0);10 })11 .fin(function() { return driver.quit(); })12 .done();13info: Welcome to Appium v1.3.7 (REV 3e7a9b3a3f7e2b1a1b7d6f8c8a7a6b9ebf7f0a3f)
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder().forBrowser('chrome').build();3driver.findElement(webdriver.By.className('gb_P')).click();4driver.findElement(webdriver.By.className('gb_P')).sendKeys('selenium webdriver');5driver.findElement(webdriver.By.className('gb_P')).sendKeys(webdriver.Key.ENTER);6driver.findElement(webdriver.By.className('r')).getText().then(function(text){7console.log(text);8});9driver.quit();
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2var By = webdriver.By;3var until = webdriver.until;4var driver = new webdriver.Builder()5.forBrowser('chrome')6.build();7driver.findElement(By.name('q')).sendKeys('webdriver');8driver.findElement(By.name('btnG')).click();9driver.wait(until.titleIs('webdriver - Google Search'), 1000);10driver.quit();11driver.findElement(By.name('q')).sendKeys('webdriver');12driver.findElement(By.name('btnG')).click();13driver.wait(until.titleIs('webdriver - Google Search'), 1000);14driver.quit();15driver.findElement(By.name('q')).sendKeys('webdriver');16driver.findElement(By.name('btnG')).click();17driver.wait(until.titleIs('webdriver - Google Search'), 1000);18driver.quit();19driver.findElement(By.name('q')).sendKeys('webdriver');20driver.findElement(By.name('btnG')).click();21driver.wait(until.titleIs('webdriver - Google Search'), 1000);22driver.quit();23driver.findElement(By.name('q')).sendKeys('webdriver');24driver.findElement(By.name('btnG')).click();25driver.wait(until.titleIs('webdriver - Google Search'), 1000);26driver.quit();27driver.findElement(By.name('q')).sendKeys('webdriver');28driver.findElement(By.name('btnG')).click();29driver.wait(until.titleIs('webdriver - Google Search'), 1000);30driver.quit();31driver.findElement(By.name('q')).sendKeys('webdriver');32driver.findElement(By.name('btnG')).click();33driver.wait(until.titleIs('webdriver - Google Search'), 1000);34driver.quit();35driver.findElement(By.name('q')).sendKeys('webdriver');36driver.findElement(By.name('btnG')).click();37driver.wait(until.titleIs('webdriver - Google Search'), 1000);38driver.quit();39driver.findElement(By.name('q')).sendKeys('webdriver');40driver.findElement(By.name('btnG')).click();41driver.wait(until.titleIs('webdriver
Using AI Code Generation
1var wd = require('wd'),2 assert = require('assert'),3 _ = require('underscore'),4 serverConfigs = require('./helpers/appium-servers');5var serverConfig = serverConfigs.local;6var desired = {7};8var driver = wd.promiseChainRemote(serverConfig);9 .init(desired)10 .sleep(10000)11 .elementsByClassName('android.widget.Button')12 .then(function(buttons) {13 assert(buttons.length === 2);14 return buttons[0].click();15 })16 .sleep(5000)17 .quit();18exports.local = {
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!!