Best JavaScript code snippet using appium-android-driver
context-specs.js
Source:context-specs.js
1import chai from 'chai';2import chaiAsPromised from 'chai-as-promised';3import sinon from 'sinon';4import { default as webviewHelpers,5 NATIVE_WIN, WEBVIEW_BASE, WEBVIEW_WIN, CHROMIUM_WIN } from '../../../lib/webview-helpers';6import { setupNewChromedriver } from '../../../lib/commands/context';7import AndroidDriver from '../../..';8import Chromedriver from 'appium-chromedriver';9import PortFinder from 'portfinder';10import { errors } from 'appium-base-driver';11let driver;12let stubbedChromedriver;13let sandbox = sinon.sandbox.create();14let expect = chai.expect;15chai.should();16chai.use(chaiAsPromised);17describe('Context', function () {18 beforeEach(() => {19 sandbox.stub(PortFinder, 'getPort', function (cb) { // eslint-disable-line promise/prefer-await-to-callbacks20 return cb(null, 4444); // eslint-disable-line promise/prefer-await-to-callbacks21 });22 driver = new AndroidDriver();23 driver.adb = sandbox.stub();24 driver.adb.curDeviceId = 'device_id';25 driver.adb.getAdbServerPort = sandbox.stub().returns(5555);26 sandbox.stub(Chromedriver.prototype, 'restart');27 sandbox.stub(Chromedriver.prototype, 'start');28 sandbox.stub(Chromedriver.prototype.proxyReq, 'bind').returns('proxy');29 stubbedChromedriver = sinon.stub();30 stubbedChromedriver.proxyReq = sinon.stub();31 stubbedChromedriver.proxyReq.bind = sinon.stub();32 stubbedChromedriver.restart = sinon.stub();33 stubbedChromedriver.stop = sandbox.stub().throws();34 stubbedChromedriver.removeAllListeners = sandbox.stub();35 });36 afterEach(function () {37 sandbox.restore();38 });39 describe('getCurrentContext', function () {40 it('should return current context', async function () {41 driver.curContext = 'current_context';42 await driver.getCurrentContext().should.become('current_context');43 });44 });45 describe('getContexts', function () {46 it('should get Chromium context where appropriate', async function () {47 driver = new AndroidDriver({browserName: 'Chrome'});48 expect(await driver.getContexts()).to.include(CHROMIUM_WIN);49 });50 it('should use ADB to figure out which webviews are available', async function () {51 sandbox.stub(webviewHelpers, 'getWebviews');52 expect(await driver.getContexts()).to.not.include(CHROMIUM_WIN);53 webviewHelpers.getWebviews.calledOnce.should.be.true;54 });55 });56 describe('setContext', function () {57 beforeEach(() => {58 sandbox.stub(driver, 'getContexts').returns(['DEFAULT', 'WV', 'ANOTHER']);59 sandbox.stub(driver, 'switchContext');60 });61 it('should switch to default context if name is null', async function () {62 sandbox.stub(driver, 'defaultContextName').returns('DEFAULT');63 await driver.setContext(null);64 driver.switchContext.calledWithExactly('DEFAULT').should.be.true;65 driver.curContext.should.be.equal('DEFAULT');66 });67 it('should switch to default web view if name is WEBVIEW', async function () {68 sandbox.stub(driver, 'defaultWebviewName').returns('WV');69 await driver.setContext(WEBVIEW_WIN);70 driver.switchContext.calledWithExactly('WV').should.be.true;71 driver.curContext.should.be.equal('WV');72 });73 it('should throw error if context does not exist', async function () {74 await driver.setContext('fake')75 .should.be.rejectedWith(errors.NoSuchContextError);76 });77 it('should not switch to context if already in it', async function () {78 driver.curContext = 'ANOTHER';79 await driver.setContext('ANOTHER');80 driver.switchContext.notCalled.should.be.true;81 });82 });83 describe('switchContext', function () {84 beforeEach(() => {85 sandbox.stub(driver, 'stopChromedriverProxies');86 sandbox.stub(driver, 'startChromedriverProxy');87 sandbox.stub(driver, 'suspendChromedriverProxy');88 sandbox.stub(driver, 'isChromedriverContext');89 driver.curContext = 'current_cntx';90 });91 it('should start chrome driver proxy if requested context is webview', async function () {92 driver.isChromedriverContext.returns(true);93 await driver.switchContext('context');94 driver.startChromedriverProxy.calledWithExactly('context').should.be.true;95 });96 it('should stop chromedriver proxy if current context is webview and requested context is not', async function () {97 driver.opts = {recreateChromeDriverSessions: true};98 driver.isChromedriverContext.withArgs('requested_cntx').returns(false);99 driver.isChromedriverContext.withArgs('current_cntx').returns(true);100 await driver.switchContext('requested_cntx');101 driver.stopChromedriverProxies.calledOnce.should.be.true;102 });103 it('should suspend chrome driver proxy if current context is webview and requested context is not', async function () {104 driver.opts = {recreateChromeDriverSessions: false};105 driver.isChromedriverContext.withArgs('requested_cntx').returns(false);106 driver.isChromedriverContext.withArgs('current_cntx').returns(true);107 await driver.switchContext('requested_cntx');108 driver.suspendChromedriverProxy.calledOnce.should.be.true;109 });110 it('should throw error if requested and current context are not webview', async function () {111 driver.isChromedriverContext.withArgs('requested_cntx').returns(false);112 driver.isChromedriverContext.withArgs('current_cntx').returns(false);113 await driver.switchContext('requested_cntx')114 .should.be.rejectedWith(/switching to context/);115 });116 });117 describe('defaultContextName', function () {118 it('should return NATIVE_WIN', async function () {119 await driver.defaultContextName().should.be.equal(NATIVE_WIN);120 });121 });122 describe('defaultWebviewName', function () {123 it('should return WEBVIEW with package', async function () {124 driver.opts = {appPackage: 'pkg'};125 await driver.defaultWebviewName().should.be.equal(WEBVIEW_BASE + 'pkg');126 });127 });128 describe('isWebContext', function () {129 it('should return true if current context is not native', async function () {130 driver.curContext = 'current_context';131 await driver.isWebContext().should.be.true;132 });133 });134 describe('startChromedriverProxy', function () {135 beforeEach(() => {136 sandbox.stub(driver, 'onChromedriverStop');137 });138 it('should start new chromedriver session', async function () {139 await driver.startChromedriverProxy('WEBVIEW_1');140 driver.sessionChromedrivers.WEBVIEW_1.should.be.equal(driver.chromedriver);141 driver.chromedriver.start.getCall(0).args[0]142 .chromeOptions.androidDeviceSerial.should.be.equal('device_id');143 driver.chromedriver.proxyPort.should.be.equal(4444);144 driver.chromedriver.proxyReq.bind.calledWithExactly(driver.chromedriver);145 driver.proxyReqRes.should.be.equal('proxy');146 driver.jwpProxyActive.should.be.true;147 });148 it('should be able to extract package from context name', async function () {149 driver.opts.appPackage = 'pkg';150 driver.opts.extractChromeAndroidPackageFromContextName = true;151 await driver.startChromedriverProxy('WEBVIEW_com.pkg');152 driver.chromedriver.start.getCall(0).args[0]153 .chromeOptions.should.be.deep.include({androidPackage: 'com.pkg'});154 });155 it('should use package from opts if package extracted from context is empty', async function () {156 driver.opts.appPackage = 'pkg';157 driver.opts.extractChromeAndroidPackageFromContextName = true;158 await driver.startChromedriverProxy('WEBVIEW_');159 driver.chromedriver.start.getCall(0).args[0]160 .chromeOptions.should.be.deep.include({androidPackage: 'pkg'});161 });162 it('should handle chromedriver event with STATE_STOPPED state', async function () {163 await driver.startChromedriverProxy('WEBVIEW_1');164 await driver.chromedriver.emit(Chromedriver.EVENT_CHANGED,165 {state: Chromedriver.STATE_STOPPED});166 driver.onChromedriverStop.calledWithExactly('WEBVIEW_1').should.be.true;167 });168 it('should ignore events if status is not STATE_STOPPED', async function () {169 await driver.startChromedriverProxy('WEBVIEW_1');170 await driver.chromedriver.emit(Chromedriver.EVENT_CHANGED,171 {state: 'unhandled_state'});172 driver.onChromedriverStop.notCalled.should.be.true;173 });174 it('should reconnect if session already exists', async function () {175 stubbedChromedriver.hasWorkingWebview = sinon.stub().returns(true);176 driver.sessionChromedrivers = {WEBVIEW_1: stubbedChromedriver};177 await driver.startChromedriverProxy('WEBVIEW_1');178 driver.chromedriver.restart.notCalled.should.be.true;179 driver.chromedriver.should.be.equal(stubbedChromedriver);180 });181 it('should restart if chromedriver has not working web view', async function () {182 stubbedChromedriver.hasWorkingWebview = sinon.stub().returns(false);183 driver.sessionChromedrivers = {WEBVIEW_1: stubbedChromedriver};184 await driver.startChromedriverProxy('WEBVIEW_1');185 driver.chromedriver.restart.calledOnce.should.be.true;186 });187 });188 describe('suspendChromedriverProxy', function () {189 it('should suspend chrome driver proxy', async function () {190 await driver.suspendChromedriverProxy();191 (driver.chromedriver == null).should.be.true;192 (driver.proxyReqRes == null).should.be.true;193 driver.jwpProxyActive.should.be.false;194 });195 });196 describe('onChromedriverStop', function () {197 it('should call startUnexpectedShutdown if chromedriver in active context', async function () {198 sinon.stub(driver, 'startUnexpectedShutdown');199 driver.curContext = 'WEBVIEW_1';200 await driver.onChromedriverStop('WEBVIEW_1');201 let arg0 = driver.startUnexpectedShutdown.getCall(0).args[0];202 arg0.should.be.an('error');203 arg0.message.should.include('Chromedriver quit unexpectedly during session');204 });205 it('should delete session if chromedriver in non-active context', async function () {206 driver.curContext = 'WEBVIEW_1';207 driver.sessionChromedrivers = {WEBVIEW_2: 'CHROMIUM'};208 await driver.onChromedriverStop('WEBVIEW_2');209 driver.sessionChromedrivers.should.be.empty;210 });211 });212 describe('stopChromedriverProxies', function () {213 it('should stop all chromedriver', async function () {214 driver.sessionChromedrivers = {WEBVIEW_1: stubbedChromedriver, WEBVIEW_2: stubbedChromedriver};215 sandbox.stub(driver, 'suspendChromedriverProxy');216 await driver.stopChromedriverProxies();217 driver.suspendChromedriverProxy.calledOnce.should.be.true;218 stubbedChromedriver.removeAllListeners219 .calledWithExactly(Chromedriver.EVENT_CHANGED).should.be.true;220 stubbedChromedriver.removeAllListeners.calledTwice.should.be.true;221 stubbedChromedriver.stop.calledTwice.should.be.true;222 driver.sessionChromedrivers.should.be.empty;223 });224 });225 describe('isChromedriverContext', function () {226 it('should return true if context is webview or chromium', async function () {227 await driver.isChromedriverContext(WEBVIEW_WIN + '_1').should.be.true;228 await driver.isChromedriverContext(CHROMIUM_WIN).should.be.true;229 });230 });231 describe('setupNewChromedriver', function () {232 it('should be able to set app package from chrome options', async function () {233 let chromedriver = await setupNewChromedriver({chromeOptions: {androidPackage: 'apkg'}});234 chromedriver.start.getCall(0).args[0].chromeOptions.androidPackage235 .should.be.equal('apkg');236 });237 it('should use prefixed chromeOptions', async function () {238 let chromedriver = await setupNewChromedriver({239 'goog:chromeOptions': {240 androidPackage: 'apkg',241 },242 });243 chromedriver.start.getCall(0).args[0].chromeOptions.androidPackage244 .should.be.equal('apkg');245 });246 it('should merge chromeOptions', async function () {247 let chromedriver = await setupNewChromedriver({248 chromeOptions: {249 androidPackage: 'apkg',250 },251 'goog:chromeOptions': {252 androidWaitPackage: 'bpkg',253 },254 'appium:chromeOptions': {255 androidActivity: 'aact',256 },257 });258 chromedriver.start.getCall(0).args[0].chromeOptions.androidPackage259 .should.be.equal('apkg');260 chromedriver.start.getCall(0).args[0].chromeOptions.androidActivity261 .should.be.equal('aact');262 chromedriver.start.getCall(0).args[0].chromeOptions.androidWaitPackage263 .should.be.equal('bpkg');264 });265 it('should be able to set androidActivity chrome option', async function () {266 let chromedriver = await setupNewChromedriver({chromeAndroidActivity: 'act'});267 chromedriver.start.getCall(0).args[0].chromeOptions.androidActivity268 .should.be.equal('act');269 });270 it('should be able to set androidProcess chrome option', async function () {271 let chromedriver = await setupNewChromedriver({chromeAndroidProcess: 'proc'});272 chromedriver.start.getCall(0).args[0].chromeOptions.androidProcess273 .should.be.equal('proc');274 });275 it('should be able to set loggingPrefs capability', async function () {276 let chromedriver = await setupNewChromedriver({enablePerformanceLogging: true});277 chromedriver.start.getCall(0).args[0].loggingPrefs278 .should.deep.equal({performance: 'ALL'});279 });280 it('should set androidActivity to appActivity if browser name is chromium-webview', async function () {281 let chromedriver = await setupNewChromedriver({browserName: 'chromium-webview',282 appActivity: 'app_act'});283 chromedriver.start.getCall(0).args[0].chromeOptions.androidActivity284 .should.be.equal('app_act');285 });286 it('should be able to set loggingPrefs capability', async function () {287 let chromedriver = await setupNewChromedriver({pageLoadStrategy: "strategy"});288 chromedriver.start.getCall(0).args[0].pageLoadStrategy289 .should.be.equal("strategy");290 });291 });...
e2e.js
Source:e2e.js
...3import webdriver from 'selenium-webdriver';4import { expect } from 'chai';5import electronPath from 'electron-prebuilt';6import homeStyles from '../app/components/Home.css';7chromedriver.start(); // on port 95158process.on('exit', chromedriver.stop);9const delay = time => new Promise(resolve => setTimeout(resolve, time));10describe('main window', function spec() {11 this.timeout(5000);12 before(async () => {13 await delay(1000); // wait chromedriver start time14 this.driver = new webdriver.Builder()15 .usingServer('http://localhost:9515')16 .withCapabilities({17 chromeOptions: {18 binary: electronPath,19 args: [`app=${path.resolve()}`]20 }21 })...
.env.example.js
Source:.env.example.js
2module.exports = {3 "user" : "wp",4 "password" : "wp",5 before : function(done) {6 chromedriver.start();7 done();8 },9 after : function(done) {10 chromedriver.stop();11 done();12 },13 beforeEach: function( browser, done ) {14 var page = browser.page.WordPressHelper();15 page.login();16 }...
globals.js
Source:globals.js
1const chromedriver = require('chromedriver');2module.exports = {3 before: function(done) {4 chromedriver.start();5 require('dotenv').config();6 done();7 },8 after: function(done) {9 chromedriver.stop();10 done();11 }...
nightwatch.globals.js
Source:nightwatch.globals.js
1var chromedriver = require('chromedriver');2module.exports = {3 before : function(done) {4 chromedriver.start();5 done();6 },7 after : function(done) {8 chromedriver.stop();9 done();10 }...
external-globals.js
Source:external-globals.js
1var chromedriver = require("chromedriver");2module.exports = {3 before: function(done) {4 chromedriver.start();5 done();6 },7 after: function(done) {8 chromedriver.stop();9 done();10 },...
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();10var webdriver = require('selenium-webdriver'),11 until = webdriver.until;12var driver = new webdriver.Builder()13 .forBrowser('chrome')14 .build();15driver.findElement(By.name('q')).sendKeys('webdriver');16driver.findElement(By.name('btnG')).click();17driver.wait(until.titleIs('webdriver - Google Search'), 1000);18driver.quit();19var webdriver = require('selenium-webdriver');20var driver = new webdriver.Builder()21 .forBrowser('chrome')22 .build();23driver.findElement(By.name('q')).sendKeys('webdriver');24driver.findElement(By.name('btnG')).click();25driver.wait(until.titleIs('webdriver - Google Search'), 1000);26driver.quit();27[debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}28[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}29[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"value":"OK, shutting down","status":0}
Using AI Code Generation
1var webdriverio = require('webdriverio');2var opts = {3 desiredCapabilities: {4 }5};6 .remote(opts)7 .init()8 .getTitle().then(function(title) {9 console.log('Title was: ' + title);10 })11 .end()12 .catch(function(err) {13 console.log(err);14 });15var webdriverio = require('webdriverio');16var opts = {17 desiredCapabilities: {18 }19};20 .remote(opts)21 .init()22 .getTitle().then(function(title) {23 console.log('Title was: ' + title);24 })25 .end()26 .catch(function(err) {27 console.log(err);28 });29var webdriverio = require('webdriverio');30var opts = {31 desiredCapabilities: {32 }33};34 .remote(opts)35 .init()36 .getTitle().then(function(title) {37 console.log('Title was: ' + title);38 })39 .end()40 .catch(function(err) {41 console.log(err);42 });43var webdriverio = require('webdriverio');44var opts = {45 desiredCapabilities: {46 }47};48 .remote(opts)49 .init()50 .getTitle().then(function(title) {51 console.log('Title was: ' + title);52 })53 .end()54 .catch(function(err) {55 console.log(err);56 });57var webdriverio = require('webdriverio');58var opts = {59 desiredCapabilities: {60 }61};62 .remote(opts)63 .init()64 .getTitle().then(function(title) {65 console.log('Title was: ' + title);66 })67 .end()68 .catch(function(err) {69 console.log(err
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder()3 .forBrowser('chrome')4 .build();5driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');6driver.findElement(webdriver.By.name('btnG')).click();7driver.wait(function() {8 return driver.getTitle().then(function(title) {9 return title === 'webdriver - Google Search';10 });11}, 1000);12driver.quit();13var webdriver = require('selenium-webdriver');14var driver = new webdriver.Builder()15 .forBrowser('chrome')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();25var webdriver = require('selenium-webdriver');26var driver = new webdriver.Builder()27 .forBrowser('chrome')28 .build();29driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');30driver.findElement(webdriver.By.name('btnG')).click();31driver.wait(function() {32 return driver.getTitle().then(function(title) {33 return title === 'webdriver - Google Search';34 });35}, 1000);36driver.quit();37var webdriver = require('selenium-webdriver');38var driver = new webdriver.Builder()39 .forBrowser('chrome')40 .build();41driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');42driver.findElement(webdriver.By.name('btnG')).click();43driver.wait(function() {44 return driver.getTitle().then(function(title) {45 return title === 'webdriver - Google Search';46 });47}, 1000);48driver.quit();49var webdriver = require('selenium-webdriver');50var driver = new webdriver.Builder()51 .forBrowser('chrome')52 .build();
Using AI Code Generation
1var chromedriver = require('chromedriver');2chromedriver.start();3chromedriver.stop();4var chromedriver = require('chromedriver');5chromedriver.version();6var chromedriver = require('chromedriver');7chromedriver.path();8var chromedriver = require('chromedriver');9chromedriver.executable();10var chromedriver = require('chromedriver');11chromedriver.start();12chromedriver.stop();13var chromedriver = require('chromedriver');14chromedriver.version();15var chromedriver = require('chromedriver');16chromedriver.path();17var chromedriver = require('chromedriver');18chromedriver.executable();19var chromedriver = require('chromedriver');20chromedriver.start();21chromedriver.stop();22var chromedriver = require('chromedriver');23chromedriver.version();24var chromedriver = require('chromedriver');25chromedriver.path();26var chromedriver = require('chromedriver');27chromedriver.executable();28var chromedriver = require('chromedriver');29chromedriver.start();
Using AI Code Generation
1await driver.chromedriver.start({2});3await driver.chromedriver.stop();4await driver.chromedriver.start({5});6await driver.chromedriver.stop();7await driver.chromedriver.start({8});9await driver.chromedriver.stop();10await driver.chromedriver.start({11});12await driver.chromedriver.stop();13await driver.chromedriver.start({14});15await driver.chromedriver.stop();16await driver.chromedriver.start({17});18await driver.chromedriver.stop();19await driver.chromedriver.start({20});21await driver.chromedriver.stop();
Using AI Code Generation
1var wd = require('wd');2var AndroidDriver = require('appium-android-driver');3var chromedriver = require('appium-chromedriver');4var driver = wd.promiseChainRemote('localhost', 4723);5var caps = {6};7 .init(caps)8 .then(function() {9 })10 .then(function() {11 return driver.waitForElementByTagName('body', 10000);12 })13 .then(function(body) {14 return body.text();15 })16 .then(function(text) {17 console.log(text);18 })19 .catch(function(err) {20 console.error(err);21 })22 .fin(function() {23 driver.quit();24 })25 .done();26info: Welcome to Appium v1.6.5 (REV 8d8d1c1a0b9bb1c9e8a2a2e0e0d0c9fca8b7e16d)
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!!