Best JavaScript code snippet using appium-android-driver
element.js
Source: element.js
1import androidHelpers from '../android-helpers';2import { retryInterval } from 'asyncbox';3import logger from '../logger';4let commands = {}, helpers = {}, extensions = {};5commands.getAttribute = async function (attribute, elementId) {6 let p = {attribute, elementId};7 return await this.bootstrap.sendAction("element:getAttribute", p);8};9commands.getName = async function (elementId) {10 return await this.getAttribute("className", elementId);11};12commands.elementDisplayed = async function (elementId) {13 return await this.getAttribute("displayed", elementId) === 'true';14};15commands.elementEnabled = async function (elementId) {16 return await this.getAttribute("enabled", elementId) === 'true';17};18commands.elementSelected = async function (elementId) {19 return await this.getAttribute("selected", elementId) === 'true';20};21helpers.setElementValue = async function (keys, elementId, replace = false) {22 let text = keys;23 if (keys instanceof Array) {24 text = keys.join("");25 }26 let params = {27 elementId,28 text,29 replace,30 unicodeKeyboard: this.opts.unicodeKeyboard31 };32 return this.doSetElementValue(params);33};34/**35 * Reason for isolating doSetElementValue from setElementValue is for reusing setElementValue36 * across android-drivers (like appium-uiautomator2-driver) and to avoid code duplication.37 * Other android-drivers (like appium-uiautomator2-driver) need to override doSetElementValue38 * to facilitate setElementValue.39 */40helpers.doSetElementValue = async function (params) {41 return await this.bootstrap.sendAction("element:setText", params);42};43commands.setValue = async function (keys, elementId) {44 return await this.setElementValue(keys, elementId, false);45};46commands.replaceValue = async function (keys, elementId) {47 return await this.setElementValue(keys, elementId, true);48};49commands.setValueImmediate = async function (keys, elementId) {50 let text = keys;51 if (keys instanceof Array) {52 text = keys.join("");53 }54 // first, make sure we are focused on the element55 await this.click(elementId);56 // then send through adb57 await this.adb.inputText(text);58};59commands.getText = async function (elementId) {60 return await this.bootstrap.sendAction("element:getText", {elementId});61};62commands.clear = async function (elementId) {63 let text = (await this.getText(elementId)) || '';64 let length = text.length;65 if (length === 0) {66 // if length is zero there are two possibilities:67 // 1. there is nothing in the text field68 // 2. it is a password field69 // since there is little overhead to the adb call, delete 100 elements70 // if we get zero, just in case it is #271 length = 100;72 }73 await this.click(elementId);74 logger.debug(`Sending up to ${length} clear characters to device`);75 return await retryInterval(5, 500, async () => {76 let remainingLength = length;77 while (remainingLength > 0) {78 let lengthToSend = remainingLength < 50 ? remainingLength : 50;79 logger.debug(`Sending ${lengthToSend} clear characters to device`);80 await this.adb.clearTextField(lengthToSend);81 remainingLength -= lengthToSend;82 }83 });84};85commands.click = async function (elementId) {86 return await this.bootstrap.sendAction("element:click", {elementId});87};88commands.getLocation = async function (elementId) {89 return await this.bootstrap.sendAction("element:getLocation", {elementId});90};91commands.getLocationInView = async function (elementId) {92 return await this.getLocation(elementId);93};94commands.getSize = async function (elementId) {95 return await this.bootstrap.sendAction("element:getSize", {elementId});96};97commands.touchLongClick = async function (elementId, x, y, duration) {98 let params = {elementId, x, y, duration};99 androidHelpers.removeNullProperties(params);100 return await this.bootstrap.sendAction("element:touchLongClick", params);101};102commands.touchDown = async function (elementId, x, y) {103 let params = {elementId, x, y};104 androidHelpers.removeNullProperties(params);105 return await this.bootstrap.sendAction("element:touchDown", params);106};107commands.touchUp = async function (elementId, x, y) {108 let params = {elementId, x, y};109 androidHelpers.removeNullProperties(params);110 return await this.bootstrap.sendAction("element:touchUp", params);111};112commands.touchMove = async function (elementId, x, y) {113 let params = {elementId, x, y};114 androidHelpers.removeNullProperties(params);115 return await this.bootstrap.sendAction("element:touchMove", params);116};117commands.complexTap = async function (tapCount, touchCount, duration, x, y) {118 return await this.bootstrap.sendAction("click", {x, y});119};120commands.tap = async function (elementId, x = 0, y = 0, count = 1) {121 for (let i = 0; i < count; i++) {122 if (elementId) {123 // we are either tapping on the default location of the element124 // or an offset from the top left corner125 if (x !== 0 || y !== 0) {126 await this.bootstrap.sendAction("element:click", {elementId, x, y});127 } else {128 await this.bootstrap.sendAction("element:click", {elementId});129 }130 } else {131 await this.bootstrap.sendAction("click", {x, y});132 }133 }134};135Object.assign(extensions, commands, helpers);136export { commands, helpers };...
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2 withCapabilities({3 build();4driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');5driver.findElement(webdriver.By.name('btnG')).click();6driver.wait(function() {7 return driver.getTitle().then(function(title) {8 return title === 'webdriver - Google Search';9 });10}, 1000);11driver.quit();12Appium Android Driver clear() method13driver.clear();14var webdriver = require('selenium-webdriver');15 withCapabilities({16 build();17driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');18driver.findElement(webdriver.By.name('btnG')).click();19driver.wait(function() {20 return driver.getTitle().then(function(title) {21 return title === 'webdriver - Google Search';22 });23}, 1000);24driver.quit();25Appium Android Driver click() method26driver.click();27var webdriver = require('selenium-webdriver');
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.android()).build();3driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');4driver.findElement(webdriver.By.name('btnG')).click();5driver.wait(function() {6 return driver.getTitle().then(function(title) {7 return title === 'webdriver - Google Search';8 });9}, 1000);10driver.quit();11driver.adb.clear("com.android.launcher");12driver.quit();
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var caps = {4};5var driver = wd.promiseChainRemote('localhost', 4723);6 .init(caps)7 .then(function() {8 return driver.clear('id=editText1');9 })10 .then(function() {11 return driver.sleep(10000);12 })13 .then(function() {14 return driver.quit();15 })16 .catch(function(err) {17 console.log(err);18 });19info: [debug] [ADB] 1 device(s) connected
Using AI Code Generation
1this.adb.clear('com.android.settings:id/search');2clear (elementId) {3 return this.adb.clear(elementId);4 }5async clear (elementId) {6 await this.shell(['input', 'keyevent', 'KEYCODE_MOVE_END']);7 await this.shell(['input', 'keyevent', 'KEYCODE_DEL']);8 }9async shell (args) {10 .concat(['shell'])11 .concat(args);12 log.debug(`Executing adb command '${cmd.join(' ')}'`);13 try {14 let {stdout} = await exec(this.executable.path, cmd);15 return stdout;16 } catch (e) {17 log.errorAndThrow(`Error executing adbExec. Original error: '${e.message}'; ` +18 `Stderr: '${e.stderr}'; Code: '${e.code}'`);19 }20 }21async function exec (cmd, args, opts = {}) {22 let {stdout, stderr} = await execSubProcess(cmd, args, opts);23 return {stdout, stderr};24}25async function execSubProcess (cmd, args, opts = {}) {26 let stdout = '';27 let stderr = '';28 let processIsAlive = true;29 let process = new SubProcess(cmd, args, opts);30 process.on('output', (stdout, stderr) => {31 if (stdout) {32 log.debug(`[STDOUT] ${stdout.trim()}`);33 }34 if (stderr) {35 log.debug(`[STDERR] ${stderr.trim()}`);36 }37 });38 process.on('die', (code) => {39 processIsAlive = false;40 log.debug(`[PROCESS] ${cmd} ${args.join(' ')} died with code ${code}`);41 });42 await process.start(0);43 await process.join();44 if (processIsAlive) {45 throw new Error(`Process never died! ${cmd} ${args.join(' ')}`);46 }47 return {stdout, stderr};48}49class SubProcess {50 constructor (cmd, args, opts = {}) {51 this.cmd = cmd;52 this.args = args;53 this.opts = opts;54 this.proc = null;55 }56 async start (
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var driver = wd.promiseChainRemote("localhost", 4723);4var desiredCaps = {5};6driver.init(desiredCaps)7 .then(function () {8 return driver.elementByName("Preference");9 })10 .then(function (el) {11 return el.click();12 })13 .then(function () {14 return driver.elementByName("3. Preference dependencies");15 })16 .then(function (el) {17 return el.click();18 })19 .then(function () {20 return driver.elementById("android:id/checkbox");21 })22 .then(function (el) {23 return el.click();24 })25 .then(function () {26 return driver.waitForElementById("android:id/edit", 5000);27 })28 .then(function (el) {29 return el.type("Hello World");30 })31 .then(function () {32 return driver.elementById("android:id/button1");33 })34 .then(function (el) {35 return el.click();36 })37 .then(function () {38 return driver.elementById("android:id/edit");39 })40 .then(function (el) {41 return el.text();42 })43 .then(function (text) {44 assert.equal(text, "Hello World");45 })46 .fin(function () {47 return driver.quit();48 })49 .done();50driver.init(desiredCaps, function () {51 driver.elementByName("Preference", function (err, el) {52 el.click(function () {53 driver.elementByName("3. Preference dependencies", function (err, el) {54 el.click(function () {55 driver.elementById("android:id/checkbox", function (err, el) {
Check out the latest blogs from LambdaTest on this topic:
Mobile apps have been an inseparable part of daily lives. Every business wants to be part of the ever-growing digital world and stay ahead of the competition by developing unique and stable applications.
Smartphones have changed the way humans interact with technology. Be it travel, fitness, lifestyle, video games, or even services, it’s all just a few touches away (quite literally so). We only need to look at the growing throngs of smartphone or tablet users vs. desktop users to grasp this reality.
Let’s put it short: Appium Desktop = Appium Server + Inspector. When Appium Server runs automation test scripts, Appium Inspector can identify the UI elements of every application under test. The core structure of an Appium Inspector is to ensure that you discover every visible app element when you develop your test scripts. Before you kickstart your journey with Appium Inspector, you need to understand the details of it.
Mobile devices and mobile applications – both are booming in the world today. The idea of having the power of a computer in your pocket is revolutionary. As per Statista, mobile accounts for more than half of the web traffic worldwide. Mobile devices (excluding tablets) contributed to 54.4 percent of global website traffic in the fourth quarter of 2021, increasing consistently over the past couple of years.
Before we discuss the Joomla testing, let us understand the fundamentals of Joomla and how this content management system allows you to create and maintain web-based applications or websites without having to write and implement complex coding requirements.
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!!