Best JavaScript code snippet using appium
test.js
Source:test.js
...67 await action.perform();68 await action.perform();69 await action.perform();70 await action.perform();71 const list = await driver.elementsById("android:id/text1");72 await list[1].click();73 }else if(type == 2){ // set age less than 1874 await click('android:id/date_picker_header_year');75 let action = new wd.TouchAction(driver);76 action.press({77 x: anchor,78 y: startPoint79 });80 action.wait({ms: 10});81 action.moveTo({82 x: anchor,83 y:endPoint 84 });85 action.wait({ms: 10});86 action.release();87 await action.perform();88 const list = await driver.elementsById("android:id/text1");89 await list[1].click();90 }else{ // set age negative91 await click('android:id/date_picker_header_year');92 let action = new wd.TouchAction(driver);93 action.press({94 x: anchor,95 y: startPoint96 });97 action.wait({ms: 10});98 action.moveTo({99 x: anchor,100 y: height-endPoint 101 });102 action.wait({ms: 10});103 action.release();104 await action.perform();105 await action.perform();106 const list = await driver.elementsById("android:id/text1");107 await list[1].click();108 }109 await sleep(500);110 await click("android:id/button1");111 await sleep(500);112}113async function getDate(){114 const date = await driver.elementById('com.example.covidsurvey:id/birthdateText');115 return date.text();116}117async function setCity(){118 await click("com.example.covidsurvey:id/selectedCity");119 await sleep(1000);120 const list = await driver.elementsById("android:id/text1");121 await list[2].click();122 await sleep(500);123}124async function getCity(){125 return (await driver.elementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.ScrollView/android.view.ViewGroup/android.widget.Spinner[1]/android.widget.TextView")).text();126}127async function setGender(type){128 if(type == 0){129 await click("com.example.covidsurvey:id/maleButton");130 }else if(type == 1){131 await click("com.example.covidsurvey:id/femaleButton");132 }else{133 await click("com.example.covidsurvey:id/otherButton");134 }135 await sleep(500);136}137async function getGender(){138 let maleButton = await driver.elementById("com.example.covidsurvey:id/maleButton");139 let femaleButton = await driver.elementById("com.example.covidsurvey:id/femaleButton");140 let otherButton = await driver.elementById("com.example.covidsurvey:id/otherButton");141 if(await maleButton.getAttribute("checked")){142 return 0;143 }else if(await femaleButton.getAttribute("checked")){144 return 1;145 }else if(await otherButton.getAttribute("checked")){146 return 2;147 }else{148 return -1;149 }150}151async function setVaccine(){152 await click("com.example.covidsurvey:id/selectedVaccine");153 await sleep(1000);154 const list = await driver.elementsById("android:id/text1");155 await list[2].click();156 await sleep(500);157}158async function getVaccine(){159 return (await driver.elementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.ScrollView/android.view.ViewGroup/android.widget.Spinner[2]/android.widget.TextView")).text();160}161async function setSideEffect(sideEffect){162 const sideEffectText = await driver.elementById('com.example.covidsurvey:id/sideText');163 await sideEffectText.sendKeys(sideEffect);164 await sleep(500);165}166async function getSideEffect(){167 const sideEffectText = await driver.elementById('com.example.covidsurvey:id/sideText');168 return sideEffectText.text();...
basics-specs.js
Source:basics-specs.js
1"use strict";2var env = require('../../../../helpers/env')3 , ADB = require('appium-adb')4 , setup = require("../../../common/setup-base")5 , desired = require("../desired")6 , reset = require("../reset")7 , atv = 'android.widget.TextView';8describe("apidemo - find - basics", function () {9 var driver;10 var apiLevel = "0";11 var singleResourceId = "home";12 setup(this, desired).then(function (d) { driver = d; });13 before(function (done) {14 var adb = new ADB({});15 // the app behaves differently on different api levels when it comes to16 // which resource ids are available for testing, so we switch here to make17 // sure we're using the right resource id below18 adb.getApiLevel(function (err, level) {19 if (err) return done(err);20 apiLevel = level;21 if (apiLevel >= "21") {22 singleResourceId = "decor_content_parent";23 }24 done();25 });26 });27 if (env.FAST_TESTS) {28 beforeEach(function () {29 return reset(driver);30 });31 }32 it('should find a single element by content-description', function (done) {33 driver34 .elementByName("Animation").text().should.become("Animation")35 .nodeify(done);36 });37 it('should find an element by class name', function (done) {38 driver39 .elementByClassName("android.widget.TextView").text().should.become("API Demos")40 .nodeify(done);41 });42 it('should find multiple elements by class name', function (done) {43 driver44 .elementsByClassName("android.widget.TextView")45 .should.eventually.have.length.at.least(10)46 .nodeify(done);47 });48 it('should not find an element that doesnt exist', function (done) {49 driver50 .elementByClassName("blargimarg").should.be.rejectedWith(/status: 7/)51 .nodeify(done);52 });53 it('should not find multiple elements that doesnt exist', function (done) {54 driver55 .elementsByClassName("blargimarg").should.eventually.have.length(0)56 .nodeify(done);57 });58 it('should fail on empty locator', function (done) {59 driver.elementsByClassName("")60 .catch(function (err) { throw err.data; }).should.be.rejectedWith(/selector/)61 .elementsByClassName(atv).should.eventually.exist62 .nodeify(done);63 });64 // TODO: The new version of ApiDemo doesn't use id, find a better example.65 it('should find a single element by id @skip-android-all', function (done) {66 driver67 .complexFind(["scroll", [[3, "views"]], [[7, "views"]]]).click()68 .elementByXPath("//android.widget.TextView[@text='Buttons']").click()69 .elementById("buttons_1_normal").text().should.become("Normal")70 .nodeify(done);71 });72 // TODO: The new version of ApiDemo doesn't use id, find a better example.73 it('should find a single element by string id @skip-android-all', function (done) {74 driver75 .elementById("activity_sample_code").text().should.become("API Demos")76 .nodeify(done);77 });78 it('should find a single element by resource-id', function (done) {79 driver80 .elementById('android:id/' + singleResourceId).should.eventually.exist81 .nodeify(done);82 });83 it('should find multiple elements by resource-id', function (done) {84 driver85 .elementsById('android:id/text1')86 .should.eventually.have.length.at.least(10)87 .nodeify(done);88 });89 it('should find multiple elements by resource-id even when theres just one', function (done) {90 driver91 .elementsById('android:id/' + singleResourceId)92 .then(function (els) {93 els.length.should.equal(1);94 })95 .nodeify(done);96 });97 it('should find a single element by resource-id with implicit package', function (done) {98 driver99 .elementById(singleResourceId).should.eventually.exist100 .nodeify(done);101 });102 it('should find multiple elements by resource-id with implicit package', function (done) {103 driver104 .elementsById('text1')105 .should.eventually.have.length.at.least(10)106 .nodeify(done);107 });108 it('should find multiple elements by resource-id with implicit package even when theres just one', function (done) {109 driver110 .elementsById(singleResourceId)111 .then(function (els) {112 els.length.should.equal(1);113 })114 .nodeify(done);115 });...
index.js
Source:index.js
...56 assert.equal(searchParametersElement.length, 1);57 });58 it('should find elements by ID', async function () {59 // Look for element by ID. In Android this is the 'resource-id'60 const actionBarContainerElements = await driver.elementsById('android:id/action_bar_container');61 assert.equal(actionBarContainerElements.length, 1);62 });63 it('should find elements by class name', async function () {64 // Look for elements by the class name. In Android this is the Java Class Name of the view.65 const linearLayoutElements = await driver.elementsByClassName('android.widget.FrameLayout');66 assert.isAbove(linearLayoutElements.length, 1);67 });68 it('should find elements by XPath', async function () {69 // Find elements by XPath70 const linearLayoutElements = await driver.elementsByXPath(`//*[@class='android.widget.FrameLayout']`);71 assert.isAbove(linearLayoutElements.length, 1);72 });...
find-basic-e2e-specs.js
Source:find-basic-e2e-specs.js
...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.above(1);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 () {49 let implicitWaitTimeout = 5000;50 before(async function () {51 await driver.setImplicitWaitTimeout(implicitWaitTimeout);52 });53 it('should respect implicit wait with multiple elements', async function () {54 let beforeMs = Date.now();55 await driver.elementsById('there_is_nothing_called_this')56 .should.eventually.have.length(0);57 let afterMs = Date.now();58 (afterMs - beforeMs).should.be.below(implicitWaitTimeout * 2);59 });60 });...
geo-location-e2e-specs.js
Source:geo-location-e2e-specs.js
...18 return Math.floor(Math.random() * (max - min + 1) + min);19 }20 it('should set geo location', async function () {21 // If we hit the permission screen, click the 'Continue Button' (sdk >= 28)22 const continueButtons = await driver.elementsById('com.android.permissioncontroller:id/continue_button');23 if (continueButtons.length > 0) {24 await continueButtons[0].click();25 }26 // Get rid of the modal window saying that the app was built for an old version27 await B.delay(1000);28 const okButtons = await driver.elementsById('android:id/button1');29 if (okButtons.length > 0) {30 await okButtons[0].click();31 }32 // Get the text in the app that tells us the latitude and logitude33 const getText = async function () {34 return await retryInterval(10, 1000, async function () {35 const textViews = await driver.elementsByClassName('android.widget.TextView');36 textViews.length.should.be.at.least(2);37 return await textViews[1].text();38 });39 };40 const latitude = getRandomInt(-90, 90);41 const longitude = getRandomInt(-180, 180);42 let text = await getText();...
android-selectors.test.js
Source:android-selectors.test.js
...30// });31//32// it('should find elements by ID', async function () {33// // Look for element by ID. In Android this is the 'resource-id'34// const actionBarContainerElements = await driver.elementsById('android:id/action_bar_container');35// assert.equal(actionBarContainerElements.length, 1);36// });37//38// it('should find elements by class name', async function () {39// // Look for elements by the class name. In Android this is the Java Class Name of the view.40// const linearLayoutElements = await driver.elementsByClassName('android.widget.FrameLayout');41// assert.isAbove(linearLayoutElements.length, 1);42// });43//44// it('should find elements by XPath', async function () {45// // Find elements by XPath46// const linearLayoutElements = await driver.elementsByXPath(`//*[@class='android.widget.FrameLayout']`);47// assert.isAbove(linearLayoutElements.length, 1);48// });...
BasePage.js
Source:BasePage.js
...10 this.navDrawer = new NavDrawer(this.driver)11 }12 get searchButton () { return this.driver.elementById('ru.myshows.activity:id/action_search')}13 get searchField () { return this.driver.elementById('ru.myshows.activity:id/search_src_text')}14 get searchResults () { return this.driver.elementsById('ru.myshows.activity:id/show_name')}15 //todo: do smt 16 get backButton () { return this.driver.elementByAccessibilityId('Navigate up')}17 get collapseButton () { return this.driver.elementByAccessibilityId('Collapse')}18 async isLoggedIn () {19 await this.driver.sleep(3000)20 return this.navDrawer.navigationDrawerButton.isDisplayed()21 }22 async searchShow (series) {23 await this.searchButton.click()24 await this.searchField.sendKeys(series)25 await this.driver.waitFor(dHelper.keyboardIsShown, 3000, 500)26 // tap the search button on mobile keyboard27 await this.driver.tap({x: 992, y: 1698})28 await this.driver.sleep(1000)...
by-id-e2e-specs.js
Source:by-id-e2e-specs.js
...16 await driver.elementById('android:id/text1').should.eventually.exist;17 });18 it('should return an array of one element if the `multi` param is true', async function () {19 // TODO: this returns an object instead of an array. Investigate.20 let els = await driver.elementsById('android:id/text1');21 els.should.be.an.instanceof(Array);22 els.should.have.length.above(1);23 });...
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder()3 .withCapabilities({4 })5 .build();6driver.manage().timeouts().implicitlyWait(10000);7driver.init().then(function () {8 driver.elementsById('android:id/text1').then(function (els) {9 els[1].click();10 });11});12driver.quit();13driver.elementById('android:id/text1').then(function (el) {14 el.click();15});16driver.elementById('android:id/text1').then(function (el) {17 el.text().then(function (text) {18 console.log(text);19 });20});21driver.elementById('android:id/text1').then(function (el) {22 el.text().then(function (text) {23 console.log(text);24 });25});26driver.elementById('android:id/text1').then(function (el) {27 el.text().then(function (text) {28 console.log(text);29 });30});31driver.elementById('android:id/text1').then(function (el) {32 el.text().then(function (text) {33 console.log(text);34 });35});
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desiredCaps = {4};5var driver = wd.promiseChainRemote('localhost', 4723);6driver.init(desiredCaps).then(function() {7 driver.elementsById("android:id/text1").then(function(elements) {8 console.log(elements);9 });10});11driver.quit();
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desired = {4};5var driver = wd.remote('localhost', 4723);6driver.init(desired, function() {7 driver.elementsById('i am a link', function(err, els) {8 assert.ok(els.length > 0);9 driver.quit();10 });11 });12});
Using AI Code Generation
1var webdriver = require('wd');2var assert = require('assert');3var desiredCaps = {4};5var driver = webdriver.remote('localhost', 4723);6driver.init(desiredCaps, function() {7 driver.elementsById('android:id/text1', function(err, el) {8 driver.elementById('android:id/text1', function(err, el) {9 el.text(function(err, text) {10 assert.ok(text === 'Animation');11 console.log("Text of the element is: " + text);12 });13 });14 });15});16driver.quit();17info: Welcome to Appium v0.17.6 (REV 4c8e5d9f7c4e4c4f4e4f4e4f4e4f4e4f4e4f4e4f)
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desired = {4};5driver.init(desired, function(err) {6 if (err) {7 console.log(err);8 return;9 }10 if (err) {11 console.log(err);12 return;13 }14 driver.elementsById('gbqfq', function(err, els) {15 if (err) {16 console.log(err);17 return;18 }19 els[0].type('appium', function(err) {20 if (err) {21 console.log(err);22 return;23 }24 driver.quit();25 });26 });27 });28});29var wd = require('wd');30var assert = require('assert');31var desired = {32};33driver.init(desired, function(err) {34 if (err) {35 console.log(err);36 return;37 }38 if (err) {39 console.log(err);40 return;41 }42 driver.elementsByClassName('gbqfif', function(err, els) {43 if (err) {44 console.log(err);45 return;46 }47 els[0].click(function(err) {48 if (err) {49 console.log(err);50 return;51 }52 driver.quit();53 });54 });55 });56});57var wd = require('wd');58var assert = require('assert');59var desired = {60};61driver.init(desired, function(err) {62 if (err) {63 console.log(err);64 return;65 }66 if (err) {67 console.log(err);68 return;69 }70 driver.elementsByCssSelector('input.gbqfif', function(err, els) {71 if (err)
Using AI Code Generation
1var driver = wd.promiseChainRemote("localhost", 4723);2driver.init(desired).then(function() {3 driver.elementsById('android:id/text1').then(function() {4 console.log("elementsById method worked");5 });6});7var driver = wd.promiseChainRemote("localhost", 4723);8driver.init(desired).then(function() {9 driver.elementByClassName('android.widget.TextView').then(function() {10 console.log("elementByClassName method worked");11 });12});13var driver = wd.promiseChainRemote("localhost", 4723);14driver.init(desired).then(function() {15 driver.elementsByClassName('android.widget.TextView').then(function() {16 console.log("elementsByClassName method worked");17 });18});19var driver = wd.promiseChainRemote("localhost", 4723);20driver.init(desired).then(function() {21 driver.elementsByClassName('android.widget.TextView').then(function() {22 console.log("elementsByClassName method worked");23 });24});25var driver = wd.promiseChainRemote("localhost", 4723);26driver.init(desired).then(function()
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!!