Best JavaScript code snippet using appium-android-driver
element-specs.js
Source:element-specs.js
...41 describe('elementDisplayed', () => {42 it('should return true if element displayed', async () => {43 sandbox.stub(driver, 'getAttribute');44 driver.getAttribute.returns('true');45 await driver.elementDisplayed('el1').should.become(true);46 driver.getAttribute.calledWithExactly('displayed', 'el1').should.be.true;47 });48 it('should return false if element not displayed', async () => {49 sandbox.stub(driver, 'getAttribute');50 driver.getAttribute.returns('false');51 await driver.elementDisplayed('el1').should.become(false);52 driver.getAttribute.calledWithExactly('displayed', 'el1').should.be.true;53 });54 });55 describe('elementEnabled', () => {56 it('should return true if element enabled', async () => {57 sandbox.stub(driver, 'getAttribute');58 driver.getAttribute.returns('true');59 await driver.elementEnabled('el1').should.become(true);60 driver.getAttribute.calledWithExactly('enabled', 'el1').should.be.true;61 });62 it('should return false if element not enabled', async () => {63 sandbox.stub(driver, 'getAttribute');64 driver.getAttribute.returns('false');65 await driver.elementEnabled('el1').should.become(false);...
plugin-specs.js
Source:plugin-specs.js
1import chai from 'chai';2import chaiAsPromised from 'chai-as-promised';3import ImageElementPlugin, { IMAGE_STRATEGY } from '../../index';4import { MATCH_FEATURES_MODE, GET_SIMILARITY_MODE, MATCH_TEMPLATE_MODE } from '../../lib/compare';5import BaseDriver from '@appium/base-driver';6import { W3C_ELEMENT_KEY } from '../../lib/finder';7import { TEST_IMG_1_B64, TEST_IMG_2_B64, TEST_IMG_2_PART_B64 } from '../fixtures';8chai.use(chaiAsPromised);9chai.should();10describe('ImageElementPlugin#handle', function () {11 const next = () => {};12 const driver = new BaseDriver();13 const p = new ImageElementPlugin();14 describe('compareImages', function () {15 this.timeout(6000);16 it('should compare images via match features mode', async function () {17 const res = await p.compareImages(next, driver, MATCH_FEATURES_MODE, TEST_IMG_1_B64, TEST_IMG_2_B64, {});18 res.count.should.eql(0);19 });20 it('should compare images via get similarity mode', async function () {21 const res = await p.compareImages(next, driver, GET_SIMILARITY_MODE, TEST_IMG_1_B64, TEST_IMG_2_B64, {});22 res.score.should.be.above(0.2);23 });24 it('should compare images via match template mode', async function () {25 const res = await p.compareImages(next, driver, MATCH_TEMPLATE_MODE, TEST_IMG_1_B64, TEST_IMG_2_B64, {});26 res.rect.height.should.be.above(0);27 res.rect.width.should.be.above(0);28 res.score.should.be.above(0.2);29 });30 it('should throw an error if comparison mode is not supported', async function () {31 await p.compareImages(next, driver, 'some mode', '', '')32 .should.eventually.be.rejectedWith(/comparison mode is unknown/);33 });34 });35 describe('findElement(s)', function () {36 driver.settings = {getSettings: () => ({})};37 driver.isW3CProtocol = () => true;38 driver.getScreenshot = () => TEST_IMG_2_B64;39 driver.getWindowSize = () => ({width: 64, height: 64});40 it('should defer execution to regular command if not a find command', async function () {41 const next = () => true;42 await p.handle(next, driver, 'sendKeys').should.eventually.become(true);43 });44 it('should defer execution to regular command if it is a find command but a different strategy', async function () {45 const next = () => true;46 await p.findElement(next, driver, 'xpath', '//foo/bar').should.eventually.become(true);47 await p.findElements(next, driver, 'xpath', '//foo/bar').should.eventually.become(true);48 });49 it('should find an image element inside a screenshot', async function () {50 const el = await p.findElement(next, driver, IMAGE_STRATEGY, TEST_IMG_2_PART_B64);51 el[W3C_ELEMENT_KEY].should.include('appium-image-element');52 });53 it('should find image elements inside a screenshot', async function () {54 const els = await p.findElements(next, driver, IMAGE_STRATEGY, TEST_IMG_2_PART_B64);55 els.should.have.length(1);56 els[0][W3C_ELEMENT_KEY].should.include('appium-image-element');57 });58 });59 describe('Element interactions', function () {60 let elId;61 before(async function () {62 driver.settings = {getSettings: () => ({})};63 driver.isW3CProtocol = () => true;64 driver.getScreenshot = () => TEST_IMG_2_B64;65 driver.getWindowSize = () => ({width: 64, height: 64});66 const el = await p.findElement(next, driver, IMAGE_STRATEGY, TEST_IMG_2_PART_B64);67 elId = el[W3C_ELEMENT_KEY];68 });69 it('should click on the screen coords of the middle of the element', async function () {70 let action = null;71 driver.performActions = (a) => { action = a; };72 await p.handle(next, driver, 'click', elId);73 action.should.eql([{74 type: 'pointer',75 id: 'mouse',76 parameters: {pointerType: 'touch'},77 actions: [78 {type: 'pointerMove', x: 24, y: 40, duration: 0},79 {type: 'pointerDown', button: 0},80 {type: 'pause', duration: 125},81 {type: 'pointerUp', button: 0},82 ]83 }]);84 });85 it('should always say the element is displayed', async function () {86 await p.handle(next, driver, 'elementDisplayed', elId).should.eventually.be.true;87 });88 it('should return the matched region size', async function () {89 await p.handle(next, driver, 'getSize', elId).should.eventually.eql({90 width: 48,91 height: 48,92 });93 });94 it('should return the matched region location', async function () {95 await p.handle(next, driver, 'getLocation', elId).should.eventually.eql({96 x: 0,97 y: 16,98 });99 });100 it('should return the region rect', async function () {101 await p.handle(next, driver, 'getElementRect', elId).should.eventually.eql({102 x: 0,103 y: 16,104 height: 48,105 width: 48,106 });107 });108 it('should return the match score as the score attr', async function () {109 await p.handle(next, driver, 'getAttribute', 'score', elId).should.eventually.be.above(0.7);110 });111 it('should return the match visualization as the visual attr', async function () {112 driver.settings = {getSettings: () => ({113 getMatchedImageResult: true,114 })};115 const el = await p.findElement(next, driver, IMAGE_STRATEGY, TEST_IMG_2_PART_B64);116 elId = el[W3C_ELEMENT_KEY];117 await p.handle(next, driver, 'getAttribute', 'visual', elId).should.eventually.include('iVBOR');118 });119 it('should not allow any other attrs', async function () {120 await p.handle(next, driver, 'getAttribute', 'rando', elId).should.eventually.be121 .rejectedWith(/not yet/i);122 });123 });...
calc-app-1-specs.js
Source:calc-app-1-specs.js
...55 await computeAndCheck();56 });57 it('should confirm that button is displayed', async function () {58 let el = await driver.findElement('class name', 'UIATextField');59 (await driver.elementDisplayed(el)).should.be.ok;60 });61 it('should confirm that the disabled button is disabled', async function () {62 let el = await driver.findElement('accessibility id', 'DisabledButton');63 (await driver.elementEnabled(el)).should.not.be.ok;64 });65 it('should confirm that the compute sum button is enabled', async function () {66 let el = await driver.findElement('accessibility id', 'ComputeSumButton');67 (await driver.elementEnabled(el)).should.be.ok;68 });69 it('should interact with alert', async function () {70 let button = (await driver.findElements('class name', 'UIAButton'))[1];71 await driver.click(button);72 await driver.postAcceptAlert();73 await driver.click(button);...
basic-specs.js
Source:basic-specs.js
...28 it('should confirm element is not visible', async function () {29 let el1 = await driver.findElement('xpath', `//UIAStaticText[contains(${textTag}, 'Buttons')]`);30 await driver.click(el1);31 let el2 = await driver.findElement('xpath', '//UIANavigationBar/UIAImage');32 (await driver.elementDisplayed(el2)).should.not.be.ok;33 });34 it('should confirm element is visible', async function () {35 let el1 = await driver.findElement('xpath', `//UIAStaticText[contains(${textTag}, 'Buttons')]`);36 await driver.click(el1);37 let el2 = await driver.findElement('xpath', "//UIATableGroup[@name = 'SYSTEM (CONTACT ADD)']");38 (await driver.elementDisplayed(el2)).should.be.ok;39 });40 it('should confirm element is selected', async function () {41 await driver.execute('mobile: scroll', {direction: 'down'});42 let el1 = await driver.findElement('xpath', "//UIAStaticText[contains(@label, 'Switches')]");43 await driver.click(el1);44 let el2 = await driver.findElement('class name', 'UIASwitch');45 (await driver.elementSelected(el2)).should.be.ok;46 });47 it('should confirm element is not selected returns false', async function () {48 try {49 await driver.execute('mobile: scroll', {direction: 'down'});50 } catch (ign) {51 // Instruments is broken in 8.1, 8.2, 8.352 // can't scroll if already scrolled all the way down...
element.js
Source:element.js
...44 log.info(45 `Element with ${this.strategy} strategy for ${this.selector} selector found.`46 );47 log.info('Check if element is displayed');48 return await this.driver.elementDisplayed(element);49 }50 async elementState() {51 const response = await fetch(52 `${this.sessionInfo.baseUrl}session/${this.sessionInfo.jwProxySession}/element`,53 {54 body: JSON.stringify({55 strategy: this.strategy,56 selector: this.selector,57 context: '',58 multiple: false,59 }),60 method: 'POST',61 headers: { 'Content-Type': 'application/json' },62 }...
clear-specs.js
Source:clear-specs.js
...16 it('should hide keyboard using "Done" key', async function () {17 let el1 = await driver.findElement('class name', 'UIATextField');18 await driver.setValue('1', el1);19 let el2 = await driver.findElement('class name', 'UIASwitch');20 (await driver.elementDisplayed(el2)).should.not.be.ok;21 await driver.hideKeyboard(undefined, 'Done', driver.sessionId);22 (await driver.elementDisplayed(el2)).should.be.ok;23 });24 it('should hide keyboard using "pressKey" strategy with "Done" key', async function () {25 let el1 = await driver.findElement('class name', 'UIATextField');26 await driver.setValue('1', el1);27 let el2 = await driver.findElement('class name', 'UIASwitch');28 (await driver.elementDisplayed(el2)).should.not.be.ok;29 await driver.hideKeyboard('pressKey', 'Done', driver.sessionId);30 (await driver.elementDisplayed(el2)).should.be.ok;31 });32 it('should hide keyboard using "pressKey" strategy with "Done" keyName', async function () {33 let el1 = await driver.findElement('class name', 'UIATextField');34 await driver.setValue('1', el1);35 let el2 = await driver.findElement('class name', 'UIASwitch');36 (await driver.elementDisplayed(el2)).should.not.be.ok;37 await driver.hideKeyboard('pressKey', undefined, undefined, 'Done', driver.sessionId);38 (await driver.elementDisplayed(el2)).should.be.ok;39 });40 it('should hide keyboard using "press" strategy with "Done" key', async function () {41 let el1 = await driver.findElement('class name', 'UIATextField');42 await driver.setValue('1', el1);43 let el2 = await driver.findElement('class name', 'UIASwitch');44 (await driver.elementDisplayed(el2)).should.not.be.ok;45 await driver.hideKeyboard('press', 'Done', driver.sessionId);46 (await driver.elementDisplayed(el2)).should.be.ok;47 });48 // swipedown just doesn't work with testapp49 it.skip('should hide keyboard using "swipeDown" strategy', async function () {50 let el1 = await driver.findElement('class name', 'UIATextField');51 await driver.setValue('1', el1);52 let el2 = await driver.findElement('class name', 'UIASwitch');53 (await driver.elementDisplayed(el2)).should.not.be.ok;54 await driver.hideKeyboard('swipeDown', driver.sessionId);55 (await driver.elementDisplayed(el2)).should.be.ok;56 });...
attribute-e2e-specs.js
Source:attribute-e2e-specs.js
...38 it('should be able to find displayed attribute', async function () {39 await driver.getAttribute('displayed', animationEl).should.eventually.become('true');40 });41 it('should be able to find displayed attribute through normal func', async function () {42 await driver.elementDisplayed(animationEl).should.eventually.become(true);43 });44 it('should be able to get element location using getLocation', async function () {45 let location = await driver.getLocation(animationEl);46 location.x.should.be.at.least(0);47 location.y.should.be.at.least(0);48 });49 it('should be able to get element location using getLocationInView', async function () {50 let location = await driver.getLocationInView(animationEl);51 location.x.should.be.at.least(0);52 location.y.should.be.at.least(0);53 });54 it('should be able to get element size', async function () {55 let size = await driver.getSize(animationEl);56 size.width.should.be.at.least(0);...
recipes.js
Source:recipes.js
...5 return els[0];6}7async function clickBack (driver) {8 let el = await elOrNull(driver, 'accessibility id', 'Back');9 if (el && (await driver.elementDisplayed(el))) {10 await driver.click(el);11 }12}13async function clickButton (driver, name) {14 let el = await elOrNull(driver, 'xpath', `//UIAButton[@name = '${name}']`);15 if (el && (await driver.elementDisplayed(el))) {16 await driver.click(el);17 }18}19function throwMatchableError (err) {20 // When using several versions of a library (i.e. mobile-json-wire-protocol),21 // instanceof doesn't work as expected, so in tests we just use jsonwpCode22 // and message to determine that the expected error was sent.23 throw new Error(`jsonwpCode: ${err.jsonwpCode} ${err.message}`);24}25async function filterDisplayed (driver, els) {26 let displayedEls = await B.all(_.map(els, function (el) { return driver.elementDisplayed(el); }));27 return _.filter(els, function (el, i) { return displayedEls[i]; });28}29function filterVisibleUiaSelector (selector) {30 return selector.replace(/;$/, '.withPredicate("isVisible == 1");');31}32async function okIfAlert (driver) {33 let text;34 try {35 text = await driver.getAlertText();36 } catch (err) {37 // if NoAlertOpenError (27) continue38 if (err.jsonwpCode !== 27) {39 throw err;40 }...
Using AI Code Generation
1var webdriver = require('selenium-webdriver'),2 until = webdriver.until;3var driver = new webdriver.Builder()4 .forBrowser('chrome')5 .build();6driver.findElement(By.name('q')).sendKeys('webdriver');7driver.findElement(By.name('btnG')).click();8driver.wait(until.titleIs('webdriver - Google Search'), 1000);9driver.quit();10driver.elementDisplayed('id', 'com.android.calculator2:id/digit_7', function(err, displayed) {11});12driver.quit();13Copyright (c) 2012-present, Appium Committers14Copyright (c) 2013-2012, Appium Committers15Copyright (c) 2013-2012, Appium Committers16Copyright (c) 2013-2012, Appium Committers17Copyright (c) 2013-2012, Appium Committers18Copyright (c)
Using AI Code Generation
1require("chromedriver");2let swd = require("selenium-webdriver");3let bldr = new swd.Builder();4let driver = bldr.forBrowser("chrome").build();5let gTab;6driver.manage().setTimeouts({7})8driver.findElement(swd.By.css("input[title='Search']")).then(function (inputBox) {9 inputBox.sendKeys("pepcoding");10 inputBox.sendKeys(swd.Key.ENTER);11}).then(function () {12 return driver.wait(swd.until.elementLocated(swd.By.css("div[class='g']")), 10000);13}).then(function () {14 return driver.findElements(swd.By.css("div[class='g']"));15}).then(function (elements) {16 let allLinksPromise = [];17 for (let i = 0; i < elements.length; i++) {18 let linkPromise = elements[i].findElement(swd.By.css("a")).getAttribute("href");19 allLinksPromise.push(linkPromise);20 }21 let combinedPromise = Promise.all(allLinksPromise);22 return combinedPromise;23}).then(function (allLinks) {24 let allTabOpenPromise = [];25 for (let i = 0; i < allLinks.length; i++) {26 let newTabPromise = driver.newWindow(allLinks[i]);27 allTabOpenPromise.push(newTabPromise);28 }29 let combinedPromise = Promise.all(allTabOpenPromise);30 return combinedPromise;31}).then(function () {32 let allTabsPromise = driver.getAllWindowHandles();33 return allTabsPromise;34}).then(function (allTabs) {35 let allTabSwitchPromise = [];36 for (let i = 0; i < allTabs.length; i++) {37 let tabSwitchPromise = driver.switchTo().window(allTabs[i]);38 allTabSwitchPromise.push(tabSwitchPromise);39 }40 let combinedPromise = Promise.all(allTabSwitchPromise);41 return combinedPromise;42}).then(function () {43 let allTabTitlePromise = [];44 for (let i = 0; i < allTabs.length; i++) {45 let tabTitlePromise = driver.getTitle();46 allTabTitlePromise.push(tabTitlePromise);47 }48 let combinedPromise = Promise.all(allTabTitlePromise);49 return combinedPromise;50}).then(function (allTabTitles) {51 console.log(allTab
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder()3 .withCapabilities({4 })5 .build();6driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');7driver.findElement(webdriver.By.name('btnG')).click();8driver.wait(function() {9 return driver.getTitle().then(function(title) {10 return title === 'webdriver - Google Search';11 });12}, 1000);13driver.findElement(webdriver.By.id('resultStats')).isDisplayed().then(function(isDisplayed) {14 console.log(isDisplayed);15});16driver.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('btnK')).click();9driver.wait(until.titleIs('webdriver - Google Search'), 1000);10driver.findElement(By.name('q')).sendKeys('webdriver');11driver.findElement(By.name('btnK')).click();12driver.wait(until.titleIs('webdriver - Google Search'), 1000);13driver.findElement(By.name('q')).sendKeys('webdriver');14driver.findElement(By.name('btnK')).click();15driver.wait(until.titleIs('webdriver - Google Search'), 1000);16driver.findElement(By.name('q')).sendKeys('webdriver');17driver.findElement(By.name('btnK')).click();18driver.wait(until.titleIs('webdriver - Google Search'), 1000);19driver.findElement(By.name('q')).sendKeys('webdriver');20driver.findElement(By.name('btnK')).click();21driver.wait(until.titleIs('webdriver - Google Search'), 1000);22driver.findElement(By.name('q')).sendKeys('webdriver');23driver.findElement(By.name('btnK')).click();24driver.wait(until.titleIs('webdriver - Google Search'), 1000);25driver.findElement(By.name('q')).sendKeys('webdriver');26driver.findElement(By.name('btnK')).click();27driver.wait(until.titleIs('webdriver - Google Search'), 1000);28driver.findElement(By.name('q')).sendKeys('webdriver');29driver.findElement(By.name('btnK')).click();30driver.wait(until.titleIs('webdriver - Google Search'), 1000);31driver.findElement(By.name('q')).sendKeys('webdriver');32driver.findElement(By.name('btnK')).click();33driver.wait(until.titleIs('webdriver - Google Search'), 1000);34driver.findElement(By.name('q')).sendKeys('webdriver');35driver.findElement(By.name('btnK')).click();36driver.wait(until.titleIs('webdriver - Google Search'), 1000);37driver.findElement(By.name('q')).sendKeys('webdriver');38driver.findElement(By.name('btnK')).click();39driver.wait(until.titleIs('webdriver - Google Search'), 1000);40driver.findElement(By.name('q')).sendKeys('webdriver');41driver.findElement(By.name('btnK')).click();42driver.wait(until.titleIs('webdriver - Google Search'),
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 .then(function() {8 return driver.elementDisplayed('android=new UiSelector().text("Animation")');9 })10 .then(function(isDisplayed) {11 console.log("Is the element displayed: " + isDisplayed);12 })13 .then(function() {14 return driver.quit();15 })16 .done();
Using AI Code Generation
1driver.elementDisplayed("id","android:id/content").then(function(isDisplayed){2 if(isDisplayed){3 console.log("Element is displayed");4 }else{5 console.log("Element is not displayed");6 }7});
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.findElement(By.name('q')).sendKeys('webdriver');11driver.findElement(By.name('btnG')).click();12driver.quit();13driver.findElement(By.name('q')).sendKeys('webdriver');14driver.findElement(By.name('btnG')).click();15driver.quit();16driver.findElement(By.name('q')).sendKeys('webdriver');17driver.findElement(By.name('btnG')).click();18driver.quit();19driver.findElement(By.name('q')).sendKeys('webdriver');20driver.findElement(By.name('btnG')).click();21driver.quit();22driver.findElement(By.name('q')).sendKeys('webdriver');23driver.findElement(By.name('btnG')).click();24driver.quit();25driver.findElement(By.name('q')).sendKeys('webdriver');26driver.findElement(By.name('btnG')).click();27driver.quit();28driver.findElement(By.name('q')).sendKeys('webdriver');29driver.findElement(By.name('btnG')).click();30driver.quit();31driver.findElement(By.name('q')).sendKeys('webdriver');32driver.findElement(By.name('btnG')).click();33driver.quit();34driver.findElement(By.name('q')).sendKeys('webdriver');35driver.findElement(By.name('btnG')).click();36driver.quit();37driver.findElement(By.name('q')).sendKeys('webdriver');38driver.findElement(By.name('btnG')).click();39driver.quit();40driver.findElement(By.name('q')).sendKeys('webdriver');41driver.findElement(By.name('btnG')).click();42driver.quit();43driver.findElement(By.name('q')).sendKeys('webdriver');44driver.findElement(By.name('btnG')).click();45driver.quit();46driver.findElement(By.name('q')).sendKeys('webdriver');47driver.findElement(By.name('btnG')).click();48driver.quit();49driver.findElement(By.name('q')).sendKeys('webdriver');50driver.findElement(By.name('btnG')).click();51driver.quit();52driver.findElement(By.name('q')).sendKeys('webdriver');53driver.findElement(By.name('btnG')).click();54driver.quit();55driver.findElement(By.name('q')).sendKeys('webdriver');56driver.findElement(By.name('btnG')).click();57driver.quit();
Using AI Code Generation
1var wd = require('wd'),2 assert = require('assert'),3 _ = require('underscore'),4 Q = require('q');5var desired = {6};7var driver = wd.promiseChainRemote("ondemand.saucelabs.com", 80, "adityagarg", "a2f6c95f-1f07-4a6d-a3b3-8a3f7c3d9c9e");8driver.init(desired).then(function () {9 return driver.elementDisplayed('id', 'com.example.android.contactmanager:id/addContactButton');10}).then(function (isDisplayed) {11 assert.ok(isDisplayed, 'The Add Contact button is displayed');12 return driver.quit();13}).done();
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!!