Best JavaScript code snippet using appium-android-driver
basic-specs.js
Source:basic-specs.js
...238 caps.intentCategory = "appium.android.intent.category.SAMPLE_CODE";239 setup(this, caps)240 .then(function (d) { driver = d; });241 it('should launch activity with intent category', function (done) {242 driver.getCurrentActivity()243 .should.eventually.include("HelloWorld")244 .nodeify(done);245 });246 });247 });248 describe('appium android', function () {249 this.timeout(env.MOCHA_INIT_TIMEOUT);250 var session;251 var title = getTitle(this);252 if (env.FAST_TESTS) {253 beforeEach(function (done) {254 androidReset('io.appium.android.apis', '.ApiDemos').nodeify(done);255 });256 }...
utils.js
Source:utils.js
...87 * @returns {boolean}88 */89function androidBrowserOpened() {90 try {91 driver.waitUntil(() => !driver.getCurrentActivity().includes('.MainActivity') && !driver.getCurrentActivity().includes('.GrantPermissionsActivity'));92 return true;93 } catch (e) {94 return false;95 }96}97/**98 * Verify that the browser is opened.99 * - iOS: For iOS we can check if Safari is running in the foreground100 * - Android: For Android we can check the current activity. If it holds a browser reference we know101 * for sure that the app is put on the background and that for example chrome is opened.102 *103 * @returns {boolean}104 */105export function isBrowserOpened() {...
driver-e2e-specs.js
Source:driver-e2e-specs.js
...32 });33 it('should start android session focusing on default pkg and act', async function () {34 driver = await initSession(APIDEMOS_CAPS);35 await driver.getCurrentPackage().should.eventually.equal(APIDEMOS_PACKAGE);36 await driver.getCurrentActivity().should.eventually.equal(APIDEMOS_MAIN_ACTIVITY);37 });38 it('should start android session focusing on custom pkg and act', async function () {39 const caps = amendCapabilities(APIDEMOS_CAPS, {40 'appium:appPackage': APIDEMOS_PACKAGE,41 'appium:appActivity': APIDEMOS_SPLIT_TOUCH_ACTIVITY,42 });43 driver = await initSession(caps);44 await driver.getCurrentPackage().should.eventually.equal(APIDEMOS_PACKAGE);45 await driver.getCurrentActivity().should.eventually.equal(APIDEMOS_SPLIT_TOUCH_ACTIVITY);46 });47 it('should error out for not apk extension', async function () {48 const caps = amendCapabilities(APIDEMOS_CAPS, {49 'appium:app': 'foo',50 'appium:appPackage': APIDEMOS_PACKAGE,51 'appium:appActivity': APIDEMOS_SPLIT_TOUCH_ACTIVITY,52 });53 await initSession(caps).should.eventually.be.rejectedWith(/does not exist or is not accessible/);54 });55 it('should error out for invalid app path', async function () {56 const caps = amendCapabilities(APIDEMOS_CAPS, {57 'appium:app': 'foo.apk',58 'appium:appPackage': APIDEMOS_PACKAGE,59 'appium:appActivity': APIDEMOS_SPLIT_TOUCH_ACTIVITY,60 });61 await initSession(caps).should.eventually.be.rejectedWith(/does not exist or is not accessible/);62 });63 });64 describe('custom adb port', function () {65 let adbPort = 5042;66 let driver;67 beforeEach(async function () {68 await killAndPrepareServer(DEFAULT_ADB_PORT, adbPort);69 });70 afterEach(async function () {71 if (driver) {72 await deleteSession();73 }74 driver = null;75 await killAndPrepareServer(adbPort, DEFAULT_ADB_PORT);76 });77 it('should start android session with a custom adb port', async function () {78 const caps = amendCapabilities(APIDEMOS_CAPS, {79 'appium:adbPort': adbPort,80 'appium:allowOfflineDevices': true,81 });82 driver = await initSession(caps, adbPort);83 await driver.getCurrentPackage().should.eventually.equal(APIDEMOS_PACKAGE);84 await driver.getCurrentActivity().should.eventually.equal(APIDEMOS_MAIN_ACTIVITY);85 });86 });87});88describe('close', function () {89 it('should close application', async function () {90 const driver = await initSession(APIDEMOS_CAPS);91 await driver.closeApp();92 APIDEMOS_PACKAGE.should.not.equal(await driver.getCurrentPackage());93 });...
general-e2e-specs.js
Source:general-e2e-specs.js
...14 });15 describe('startActivity', function () {16 it('should launch a new package and activity', async function () {17 let appPackage = await driver.getCurrentPackage();18 let appActivity = await driver.getCurrentActivity();19 appPackage.should.equal('io.appium.android.apis');20 appActivity.should.equal('.ApiDemos');21 let startAppPackage = 'io.appium.android.apis';22 let startAppActivity = '.view.SplitTouchView';23 await driver.startActivity({appPackage: startAppPackage, appActivity: startAppActivity});24 let newAppPackage = await driver.getCurrentPackage();25 let newAppActivity = await driver.getCurrentActivity();26 newAppPackage.should.equal(startAppPackage);27 newAppActivity.should.equal(startAppActivity);28 });29 it('should be able to launch activity with custom intent parameter category', async function () {30 let startAppPackage = 'io.appium.android.apis';31 let startAppActivity = 'io.appium.android.apis.app.HelloWorld';32 let startIntentCategory = 'appium.android.intent.category.SAMPLE_CODE';33 await driver.startActivity({appPackage: startAppPackage, appActivity: startAppActivity, intentCategory: startIntentCategory});34 let appActivity = await driver.getCurrentActivity();35 appActivity.should.include('HelloWorld');36 });37 it('should be able to launch activity with dontStopAppOnReset = true', async function () {38 let startAppPackage = 'io.appium.android.apis';39 let startAppActivity = '.os.MorseCode';40 await driver.startActivity({appPackage: startAppPackage, appActivity: startAppActivity});41 let appPackage = await driver.getCurrentPackage();42 let appActivity = await driver.getCurrentActivity();43 appPackage.should.equal(startAppPackage);44 appActivity.should.equal(startAppActivity);45 });46 it('should be able to launch activity with dontStopAppOnReset = false', async function () {47 let startAppPackage = 'io.appium.android.apis';48 let startAppActivity = '.os.MorseCode';49 await driver.startActivity({appPackage: startAppPackage, appActivity: startAppActivity});50 let appPackage = await driver.getCurrentPackage();51 let appActivity = await driver.getCurrentActivity();52 appPackage.should.equal(startAppPackage);53 appActivity.should.equal(startAppActivity);54 });55 });...
keystore-base.js
Source:keystore-base.js
1"use strict";2var setup = require("./setup-base")3 , env = require('../../helpers/env')4 , exec = require('child_process').exec5 , osType = require('os').type()6 , rimraf = require('rimraf');7module.exports = function () {8 var tmp = osType === 'Windows_NT' ? 'C:\\Windows\\Temp' : '/tmp';9 var keystorePath = tmp + '/appiumtest.keystore';10 var keyAlias = 'appiumtest';11 var desired = {12 app: "sample-code/apps/selendroid-test-app.apk",13 appPackage: 'io.selendroid.testapp',14 appActivity: '.HomeScreenActivity',15 useKeystore: true,16 keystorePath: keystorePath,17 keyAlias: keyAlias18 };19 this.timeout(env.MOCHA_INIT_TIMEOUT);20 before(function (done) {21 rimraf(keystorePath, function (err) {22 if (err) return done(err);23 var cmd = 'keytool -genkey -v -keystore ' + keystorePath + ' -alias ' + keyAlias + ' -storepass android -keypass android -keyalg RSA -validity 14000';24 var child = exec(cmd, done);25 // answer the questions that `keytool` asks26 child.stdin.write('Appium Testsuite\nAppium\nTest\nSan Francisco\nCalifornia\nUS\nyes\n');27 });28 });29 // custom keystore doesn't make sense when running on Sauce30 describe('brand-new custom keystore @skip-ci', function () {31 var driver;32 setup(this, desired).then(function (d) { driver = d; });33 it('should be able to launch an app with custom keystore', function (done) {34 driver35 .getCurrentActivity()36 .should.eventually.include(desired.appActivity)37 .nodeify(done);38 });39 });...
deeplink.js
Source:deeplink.js
1const expectChai = require('chai').expect;2describe('Deep Link', () => {3 it('should let me switch between app and browser', () => {4 const appPackage = driver.getCurrentPackage();5 const appActivity = driver.getCurrentActivity();6 const chromePackage = "com.android.chrome";7 const chromeActivity = "com.google.android.apps.chrome.Main";8 const paypalUrl = "https://paypal.github.io/react-paypal-js/iframe.html?id=example-venmobutton--horizontal&viewMode=story";9 // Launch Chrome app, switch to its context, and load PayPal URL10 driver.startActivity(chromePackage, chromeActivity);11 driver.waitUntil(12 () => driver.getContexts().length > 113 )14 driver.switchContext('WEBVIEW_chrome');15 driver.url(paypalUrl);16 // Switch to iframe17 const frame = $('/html/body/div[3]/div/div/iframe[1]');18 frame.waitForExist({timeout: 5000, timeoutMsg: 'expected iframe to appear'});19 browser.switchToFrame(frame);20 // Click button within iframe21 const payPalButton = $('/html/body/div[1]/div/div[1]/div');22 payPalButton.click();23 24 var venmoText = $('android=new UiSelector().text("Venmo").className("android.widget.TextView")');25 // Perform the rest of your test in the native app26 driver.pause(3000);27 driver.startActivity(appPackage, appActivity);28 expectChai(driver.getCurrentActivity()).to.equal(appActivity);29 });...
First.js
Source:First.js
...5//boolean value true / false6//driver.isLocked();7//Console log to print line8//To get Current Activity9console.log("Hello Ritzz", driver.getCurrentActivity());10//--To get Current Package11driver.getCurrentPackage();12//--To install Application into Device13driver.installApp(c://rit/flipkart.apk)14 //to check whether this app is installed or not package 15 driver.isAppInstalled(appId)16//to Terminate app17driver.terminateApp(appId)18//--To hide keyword 19driver.hideKeyboard()20//--To check keyboard is open or not true/false21driver.isKeyboardShown()22//--To open activity and start application23driver.startActivity(appPackage, appActivity, appWaitPackage, appWaitActivity, intentAction, intentCategory, intentFlags, optionalIntentArguments, dontStopAppOnReset)...
android-create-session.test.js
Source:android-create-session.test.js
...11 ...androidCaps,12 app: require('../helpers/apps').androidApiDemos13 });14 // Check that we're running the ApiDemos app by checking package and activity15 const activity = await driver.getCurrentActivity();16 const pkg = await driver.getCurrentPackage();17 assert.equal(`${pkg}${activity}`, 'io.appium.android.apis.ApiDemos');18 // Quit the session19 await driver.quit();20 });...
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2 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.getCurrentActivity().then(function(activityName) {11 console.log("Activity Name: " + activityName);12});13driver.quit();
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desiredCaps = {4};5driver.init(desiredCaps).then(function() {6 driver.getCurrentActivity().then(function(currentActivity) {7 console.log('Current activity is: ' + currentActivity);8 });9});10driver.getCurrentActivity().then(function(currentActivity) {11 console.log('Current activity is: ' + currentActivity);12});
Using AI Code Generation
1import wd from 'wd';2import chai from 'chai';3import chaiAsPromised from 'chai-as-promised';4import { androidDriver } from 'appium-uiautomator2-driver';5const should = chai.should();6chai.use(chaiAsPromised);7const PORT = 4723;8const HOST = 'localhost';9const config = {
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var browser = wd.promiseChainRemote("localhost", 4723);4browser.init({5}).then(function() {6 browser.getCurrentActivity().then(function(activity) {7 console.log(activity);8 });9});10var wd = require('wd');11var assert = require('assert');12var browser = wd.promiseChainRemote("localhost", 4723);13browser.init({14}).then(function() {15 browser.currentActivity().then(function(activity) {16 console.log(activity);17 });18});
Using AI Code Generation
1var wd = require('wd');2var chai = require('chai');3var chaiAsPromised = require('chai-as-promised');4chai.use(chaiAsPromised);5var expect = chai.expect;6var assert = chai.assert;7var should = chai.should();8var desiredCaps = {9};10var driver = wd.promiseChainRemote('localhost', 4723);11driver.on('status', function(info) {12});13driver.on('command', function(meth, path, data) {14});15driver.on('http', function(meth, path, data) {16});17 .init(desiredCaps)18 .then(function() {19 return driver.getCurrentActivity();20 })21 .then(function(currentActivity) {22 console.log('Current Activity: ' + currentActivity);23 return driver.quit();24 })25 .catch(function(err) {26 console.log(err);27 });
Using AI Code Generation
1var wd = require('wd');2var chai = require('chai');3var chaiAsPromised = require('chai-as-promised');4var assert = chai.assert;5chai.use(chaiAsPromised);6var desiredCaps = {7};8var driver = wd.promiseChainRemote('localhost', 4723);9driver.init(desiredCaps).then(function() {10 return driver.getCurrentActivity();11}).then(function(activityName) {12 console.log('Current activity name is: ' + activityName);13 return driver.quit();14}).done();
Using AI Code Generation
1var driver = require('appium-android-driver').AndroidDriver;2var desiredCaps = {3};4driver.createSession(desiredCaps).then(function (driver) {5 driver.getCurrentActivity().then(function (activityName) {6 console.log("Current activity name: " + activityName);7 });8});9var driver = require('appium-android-driver').AndroidDriver;10var desiredCaps = {11};12driver.createSession(desiredCaps).then(function (driver) {13 driver.getCurrentPackage().then(function (packageName) {14 console.log("Current package name: " + packageName);15 });16});17var driver = require('appium-android-driver').AndroidDriver;18var desiredCaps = {19};20driver.createSession(desiredCaps).then(function (driver) {21 driver.getDeviceTime().then(function (deviceTime) {22 console.log("Device time: " + deviceTime);23 });24});25var driver = require('appium-android-driver').AndroidDriver;26var desiredCaps = {27};28driver.createSession(desiredCaps).then(function (driver) {29 driver.getNetworkConnection().then(function (networkConnection) {30 console.log("Network connection: " + networkConnection);31 });32});
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desiredCaps = {4};5var driver = wd.promiseChainRemote('localhost', 4723);6 .init(desiredCaps)7 .then(function () {8 return driver.getCurrentActivity();9 })10 .then(function (activity) {11 console.log("Current activity: " + activity);12 })13 .fin(function () {14 return driver.quit();15 })16 .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!!