How to use chromedriver.restart method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

context-specs.js

Source:context-specs.js Github

copy

Full Screen

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 '../../../lib/driver';8import Chromedriver from 'appium-chromedriver';9import PortFinder from 'portfinder';10import { errors } from '@appium/base-driver';11let driver;12let stubbedChromedriver;13let sandbox = sinon.createSandbox();14let expect = chai.expect;15chai.should();16chai.use(chaiAsPromised);17describe('Context', function () {18 beforeEach(function () {19 sandbox.stub(PortFinder, 'getPort').callsFake(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.jwproxy = sinon.stub();31 stubbedChromedriver.jwproxy.command = sinon.stub();32 stubbedChromedriver.jwproxy.command.bind = sinon.stub();33 stubbedChromedriver.proxyReq = sinon.stub();34 stubbedChromedriver.proxyReq.bind = sinon.stub();35 stubbedChromedriver.restart = sinon.stub();36 stubbedChromedriver.stop = sandbox.stub().throws();37 stubbedChromedriver.removeAllListeners = sandbox.stub();38 });39 afterEach(function () {40 sandbox.restore();41 });42 describe('getCurrentContext', function () {43 it('should return current context', async function () {44 driver.curContext = 'current_context';45 await driver.getCurrentContext().should.become('current_context');46 });47 it('should return NATIVE_APP if no context is set', async function () {48 driver.curContext = null;49 await driver.getCurrentContext().should.become(NATIVE_WIN);50 });51 });52 describe('getContexts', function () {53 it('should get Chromium context where appropriate', async function () {54 sandbox.stub(webviewHelpers, 'getWebViewsMapping');55 driver = new AndroidDriver({browserName: 'Chrome'});56 expect(await driver.getContexts()).to.include(CHROMIUM_WIN);57 webviewHelpers.getWebViewsMapping.calledOnce.should.be.true;58 });59 it('should use ADB to figure out which webviews are available', async function () {60 sandbox.stub(webviewHelpers, 'parseWebviewNames').returns(['DEFAULT', 'VW', 'ANOTHER']);61 sandbox.stub(webviewHelpers, 'getWebViewsMapping');62 expect(await driver.getContexts()).to.not.include(CHROMIUM_WIN);63 webviewHelpers.parseWebviewNames.calledOnce.should.be.true;64 webviewHelpers.getWebViewsMapping.calledOnce.should.be.true;65 });66 });67 describe('setContext', function () {68 beforeEach(function () {69 sandbox.stub(webviewHelpers, 'getWebViewsMapping').returns(70 [{'webviewName': 'DEFAULT'}, {'webviewName': 'WV'}, {'webviewName': 'ANOTHER'}]71 );72 sandbox.stub(driver, 'switchContext');73 });74 it('should switch to default context if name is null', async function () {75 sandbox.stub(driver, 'defaultContextName').returns('DEFAULT');76 await driver.setContext(null);77 driver.switchContext.calledWithExactly(78 'DEFAULT', [{'webviewName': 'DEFAULT'}, {'webviewName': 'WV'}, {'webviewName': 'ANOTHER'}]).should.be.true;79 driver.curContext.should.be.equal('DEFAULT');80 });81 it('should switch to default web view if name is WEBVIEW', async function () {82 sandbox.stub(driver, 'defaultWebviewName').returns('WV');83 await driver.setContext(WEBVIEW_WIN);84 driver.switchContext.calledWithExactly(85 'WV', [{'webviewName': 'DEFAULT'}, {'webviewName': 'WV'}, {'webviewName': 'ANOTHER'}]).should.be.true;86 driver.curContext.should.be.equal('WV');87 });88 it('should throw error if context does not exist', async function () {89 await driver.setContext('fake')90 .should.be.rejectedWith(errors.NoSuchContextError);91 });92 it('should not switch to context if already in it', async function () {93 driver.curContext = 'ANOTHER';94 await driver.setContext('ANOTHER');95 driver.switchContext.notCalled.should.be.true;96 });97 });98 describe('switchContext', function () {99 beforeEach(function () {100 sandbox.stub(driver, 'stopChromedriverProxies');101 sandbox.stub(driver, 'startChromedriverProxy');102 sandbox.stub(driver, 'suspendChromedriverProxy');103 sandbox.stub(driver, 'isChromedriverContext');104 driver.curContext = 'current_cntx';105 });106 it('should start chrome driver proxy if requested context is webview', async function () {107 driver.isChromedriverContext.returns(true);108 await driver.switchContext('context', ['current_cntx', 'context']);109 driver.startChromedriverProxy.calledWithExactly('context', ['current_cntx', 'context']).should.be.true;110 });111 it('should stop chromedriver proxy if current context is webview and requested context is not', async function () {112 driver.opts = {recreateChromeDriverSessions: true};113 driver.isChromedriverContext.withArgs('requested_cntx').returns(false);114 driver.isChromedriverContext.withArgs('current_cntx').returns(true);115 await driver.switchContext('requested_cntx');116 driver.stopChromedriverProxies.calledOnce.should.be.true;117 });118 it('should suspend chrome driver proxy if current context is webview and requested context is not', async function () {119 driver.opts = {recreateChromeDriverSessions: false};120 driver.isChromedriverContext.withArgs('requested_cntx').returns(false);121 driver.isChromedriverContext.withArgs('current_cntx').returns(true);122 await driver.switchContext('requested_cntx');123 driver.suspendChromedriverProxy.calledOnce.should.be.true;124 });125 it('should throw error if requested and current context are not webview', async function () {126 driver.isChromedriverContext.withArgs('requested_cntx').returns(false);127 driver.isChromedriverContext.withArgs('current_cntx').returns(false);128 await driver.switchContext('requested_cntx')129 .should.be.rejectedWith(/switching to context/);130 });131 });132 describe('defaultContextName', function () {133 it('should return NATIVE_WIN', async function () {134 await driver.defaultContextName().should.be.equal(NATIVE_WIN);135 });136 });137 describe('defaultWebviewName', function () {138 it('should return WEBVIEW with package', async function () {139 driver.opts = {appPackage: 'pkg'};140 await driver.defaultWebviewName().should.be.equal(WEBVIEW_BASE + 'pkg');141 });142 });143 describe('isWebContext', function () {144 it('should return true if current context is not native', async function () {145 driver.curContext = 'current_context';146 await driver.isWebContext().should.be.true;147 });148 });149 describe('startChromedriverProxy', function () {150 beforeEach(function () {151 sandbox.stub(driver, 'onChromedriverStop');152 });153 it('should start new chromedriver session', async function () {154 await driver.startChromedriverProxy('WEBVIEW_1');155 driver.sessionChromedrivers.WEBVIEW_1.should.be.equal(driver.chromedriver);156 driver.chromedriver.start.getCall(0).args[0]157 .chromeOptions.androidDeviceSerial.should.be.equal('device_id');158 driver.chromedriver.proxyPort.should.be.equal(4444);159 driver.chromedriver.proxyReq.bind.calledWithExactly(driver.chromedriver);160 driver.proxyReqRes.should.be.equal('proxy');161 driver.jwpProxyActive.should.be.true;162 });163 it('should be able to extract package from context name', async function () {164 driver.opts.appPackage = 'pkg';165 driver.opts.extractChromeAndroidPackageFromContextName = true;166 await driver.startChromedriverProxy('WEBVIEW_com.pkg');167 driver.chromedriver.start.getCall(0).args[0]168 .chromeOptions.should.be.deep.include({androidPackage: 'com.pkg'});169 });170 it('should use package from opts if package extracted from context is empty', async function () {171 driver.opts.appPackage = 'pkg';172 driver.opts.extractChromeAndroidPackageFromContextName = true;173 await driver.startChromedriverProxy('WEBVIEW_');174 driver.chromedriver.start.getCall(0).args[0]175 .chromeOptions.should.be.deep.include({androidPackage: 'pkg'});176 });177 it('should handle chromedriver event with STATE_STOPPED state', async function () {178 await driver.startChromedriverProxy('WEBVIEW_1');179 await driver.chromedriver.emit(Chromedriver.EVENT_CHANGED,180 {state: Chromedriver.STATE_STOPPED});181 driver.onChromedriverStop.calledWithExactly('WEBVIEW_1').should.be.true;182 });183 it('should ignore events if status is not STATE_STOPPED', async function () {184 await driver.startChromedriverProxy('WEBVIEW_1');185 await driver.chromedriver.emit(Chromedriver.EVENT_CHANGED,186 {state: 'unhandled_state'});187 driver.onChromedriverStop.notCalled.should.be.true;188 });189 it('should reconnect if session already exists', async function () {190 stubbedChromedriver.hasWorkingWebview = sinon.stub().returns(true);191 driver.sessionChromedrivers = {WEBVIEW_1: stubbedChromedriver};192 await driver.startChromedriverProxy('WEBVIEW_1');193 driver.chromedriver.restart.notCalled.should.be.true;194 driver.chromedriver.should.be.equal(stubbedChromedriver);195 });196 it('should restart if chromedriver has not working web view', async function () {197 stubbedChromedriver.hasWorkingWebview = sinon.stub().returns(false);198 driver.sessionChromedrivers = {WEBVIEW_1: stubbedChromedriver};199 await driver.startChromedriverProxy('WEBVIEW_1');200 driver.chromedriver.restart.calledOnce.should.be.true;201 });202 });203 describe('suspendChromedriverProxy', function () {204 it('should suspend chrome driver proxy', async function () {205 await driver.suspendChromedriverProxy();206 (driver.chromedriver == null).should.be.true;207 (driver.proxyReqRes == null).should.be.true;208 driver.jwpProxyActive.should.be.false;209 });210 });211 describe('onChromedriverStop', function () {212 it('should call startUnexpectedShutdown if chromedriver in active context', async function () {213 sinon.stub(driver, 'startUnexpectedShutdown');214 driver.curContext = 'WEBVIEW_1';215 await driver.onChromedriverStop('WEBVIEW_1');216 let arg0 = driver.startUnexpectedShutdown.getCall(0).args[0];217 arg0.should.be.an('error');218 arg0.message.should.include('Chromedriver quit unexpectedly during session');219 });220 it('should delete session if chromedriver in non-active context', async function () {221 driver.curContext = 'WEBVIEW_1';222 driver.sessionChromedrivers = {WEBVIEW_2: 'CHROMIUM'};223 await driver.onChromedriverStop('WEBVIEW_2');224 driver.sessionChromedrivers.should.be.empty;225 });226 });227 describe('stopChromedriverProxies', function () {228 it('should stop all chromedriver', async function () {229 driver.sessionChromedrivers = {WEBVIEW_1: stubbedChromedriver, WEBVIEW_2: stubbedChromedriver};230 sandbox.stub(driver, 'suspendChromedriverProxy');231 await driver.stopChromedriverProxies();232 driver.suspendChromedriverProxy.calledOnce.should.be.true;233 stubbedChromedriver.removeAllListeners234 .calledWithExactly(Chromedriver.EVENT_CHANGED).should.be.true;235 stubbedChromedriver.removeAllListeners.calledTwice.should.be.true;236 stubbedChromedriver.stop.calledTwice.should.be.true;237 driver.sessionChromedrivers.should.be.empty;238 });239 });240 describe('isChromedriverContext', function () {241 it('should return true if context is webview or chromium', async function () {242 await driver.isChromedriverContext(WEBVIEW_WIN + '_1').should.be.true;243 await driver.isChromedriverContext(CHROMIUM_WIN).should.be.true;244 });245 });246 describe('setupNewChromedriver', function () {247 it('should be able to set app package from chrome options', async function () {248 let chromedriver = await setupNewChromedriver({chromeOptions: {androidPackage: 'apkg'}});249 chromedriver.start.getCall(0).args[0].chromeOptions.androidPackage250 .should.be.equal('apkg');251 });252 it('should use prefixed chromeOptions', async function () {253 let chromedriver = await setupNewChromedriver({254 'goog:chromeOptions': {255 androidPackage: 'apkg',256 },257 });258 chromedriver.start.getCall(0).args[0].chromeOptions.androidPackage259 .should.be.equal('apkg');260 });261 it('should merge chromeOptions', async function () {262 let chromedriver = await setupNewChromedriver({263 chromeOptions: {264 androidPackage: 'apkg',265 },266 'goog:chromeOptions': {267 androidWaitPackage: 'bpkg',268 },269 'appium:chromeOptions': {270 androidActivity: 'aact',271 },272 });273 chromedriver.start.getCall(0).args[0].chromeOptions.androidPackage274 .should.be.equal('apkg');275 chromedriver.start.getCall(0).args[0].chromeOptions.androidActivity276 .should.be.equal('aact');277 chromedriver.start.getCall(0).args[0].chromeOptions.androidWaitPackage278 .should.be.equal('bpkg');279 });280 it('should be able to set androidActivity chrome option', async function () {281 let chromedriver = await setupNewChromedriver({chromeAndroidActivity: 'act'});282 chromedriver.start.getCall(0).args[0].chromeOptions.androidActivity283 .should.be.equal('act');284 });285 it('should be able to set androidProcess chrome option', async function () {286 let chromedriver = await setupNewChromedriver({chromeAndroidProcess: 'proc'});287 chromedriver.start.getCall(0).args[0].chromeOptions.androidProcess288 .should.be.equal('proc');289 });290 it('should be able to set loggingPrefs capability', async function () {291 let chromedriver = await setupNewChromedriver({enablePerformanceLogging: true});292 chromedriver.start.getCall(0).args[0].loggingPrefs293 .should.deep.equal({performance: 'ALL'});294 });295 it('should set androidActivity to appActivity if browser name is chromium-webview', async function () {296 let chromedriver = await setupNewChromedriver({browserName: 'chromium-webview',297 appActivity: 'app_act'});298 chromedriver.start.getCall(0).args[0].chromeOptions.androidActivity299 .should.be.equal('app_act');300 });301 it('should be able to set loggingPrefs capability', async function () {302 let chromedriver = await setupNewChromedriver({pageLoadStrategy: 'strategy'});303 chromedriver.start.getCall(0).args[0].pageLoadStrategy304 .should.be.equal('strategy');305 });306 });...

Full Screen

Full Screen

android-hybrid.js

Source:android-hybrid.js Github

copy

Full Screen

...165 if (works) return cb();166 logger.debug("ChromeDriver is not associated with a window. " +167 "Re-initializing the session.");168 this.chromedriverRestartingContext = context;169 this.chromedriver.restart().then(function () {170 this.chromedriverRestartingContext = null;171 cb();172 }.bind(this), cb);173 }.bind(this), cb);174};175androidHybrid.onChromedriverStop = function (context) {176 logger.warn("Chromedriver for context " + context + " stopped unexpectedly");177 if (context === this.curContext) {178 // if we don't have a stop callback, we exited unexpectedly and so want179 // to shut down the session and respond with an error180 // TODO: this kind of thing should be emitted and handled by a higher-level181 // controlling function182 var error = new UnknownError("Chromedriver quit unexpectedly during session");183 logger.error(error.message);...

Full Screen

Full Screen

context.js

Source:context.js Github

copy

Full Screen

...169 // if there is an error, we want to recreate the ChromeDriver session170 if (!await chromedriver.hasWorkingWebview()) {171 logger.debug("ChromeDriver is not associated with a window. " +172 "Re-initializing the session.");173 await chromedriver.restart();174 }175 return chromedriver;176}177async function setupNewChromedriver (opts, curDeviceId, adb) {178 // if a port wasn't given, pick a random available one179 if (!opts.chromeDriverPort) {180 let getPort = B.promisify(PortFinder.getPort, {context: PortFinder});181 opts.chromeDriverPort = await getPort();182 logger.debug(`A port was not given, using random port: ${opts.chromeDriverPort}`);183 }184 let chromeArgs = {185 port: opts.chromeDriverPort,186 executable: opts.chromedriverExecutable,187 adb,...

Full Screen

Full Screen

protractor.conf.js

Source:protractor.conf.js Github

copy

Full Screen

1exports.config = {2 directConnect: true,3 capabilities: {4 'browserName': 'chrome',5 'chromeOptions': {6 'args': ['incognito', 'disable-extensions', 'start-maximized']7 },8 shardTestFiles: true,9 maxInstances: 510 },11 chromeDriver: 'node_modules/chromedriver/lib/chromedriver/chromedriver',12 restartBrowserBetweenTests: true,13 specs: [14 'src/scripts/e2e/spec/**/*.js'15 ],16 jasmineNodeOpts: {17 showColors: true,18 defaultTimeoutInterval: 3000019 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var chromedriver = require('chromedriver');4var browser = wd.promiseChainRemote('localhost', 4723);5browser.on('status', function(info) {6 console.log(info);7});8browser.on('command', function(meth, path, data) {9 console.log(' > ' + meth, path, data || '');10});11 .init({12 chromeOptions: {13 }14 })15 .title()16 .then(function(title) {17 assert.ok(~title.indexOf('Google'), 'Title should include "Google"');18 })19 .elementByName('q')20 .type('Appium')21 .sleep(1000)22 .elementByCss('#tsbb')23 .click()24 .sleep(1000)25 .takeScreenshot()26 .then(function(screenShot) {27 console.log(screenShot);28 })29 .elementByLinkText('Appium: Mobile App Automation Made Awesome.')30 .click()31 .sleep(1000)32 .takeScreenshot()33 .then(function(screenShot) {34 console.log(screenShot);35 })36 .back()37 .sleep(1000)38 .takeScreenshot()39 .then(function(screenShot) {40 console.log(screenShot);41 })42 .elementByCss('#gsr')43 .text()44 .then(function(text) {45 assert.ok(~text.indexOf('Appium'), 'Text should include "Appium"');46 })47 .sleep(1000)48 .quit();49chromedriver.start();

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var desiredCapabilities = {3};4driver.init(desiredCapabilities).then(function(){5}).then(function(){6}).then(function(){7}).then(function(){8}).then(function(){9}).then(function(){10}).then(function(){11}).then(function(){12}).then(function(){13}).then(function(){14}).then(function(){15}).then(function(){16}).then(function(){17}).then(function(){18}).then(function(){19}).then(function(){20}).then(function(){21}).then(function(){22}).then(function(){23}).then(function(){24}).then(function(){25}).then(function(){26}).then(function(){27}).then(function(){28}).then(function(){29}).then(function(){30 return driver.get("

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require("wd");2var driver = wd.promiseChainRemote("localhost", 4723);3var desired = {4};5 .init(desired)6 .sleep(3000)7 .quit()8 .then(function() {9 console.log("App is closed");10 .init(desired)11 .sleep(3000)12 .quit()13 .then(function() {14 console.log("App is closed");15 .init(desired)16 .sleep(3000)17 .quit()18 .then(function() {19 console.log("App is closed");20 .init(desired)21 .sleep(3000)22 .quit()23 .then(function() {24 console.log("App is closed");25 .init(desired)26 .sleep(3000)27 .quit()28 .then(function() {29 console.log("App is closed");30 .init(desired)31 .sleep(3000)32 .quit()33 .then(function() {34 console.log("App is closed");35 .init(desired)36 .sleep(3000)37 .quit()38 .then(function() {39 console.log("App is closed");40 .init(desired)41 .sleep(3000)42 .quit()43 .then(function() {44 console.log("App is closed");45 .init(desired)46 .sleep(3000)47 .quit()48 .then(function() {49 console.log("App is closed");50 .init(desired)51 .sleep(3000)52 .quit()53 .then(function() {54 console.log("App is closed");55 .init(desired)56 .sleep(3000)57 .quit()58 .then(function() {59 console.log("App is closed");60 .init(desired)61 .sleep(3000)

Full Screen

Using AI Code Generation

copy

Full Screen

1var driver = require('./driver.js');2driver.restart(function(err){3 if (err) {4 console.log(err);5 }6 else {7 console.log('App restarted');8 }9});10driver.restartApp(function(err){11 if (err) {12 console.log(err);13 }14 else {15 console.log('App restarted');16 }17});18driver.reset(function(err){19 if (err) {20 console.log(err);21 }22 else {23 console.log('App restarted');24 }25});26driver.resetApp(function(err){27 if (err) {28 console.log(err);29 }30 else {31 console.log('App restarted');32 }33});34driver.background(function(err){35 if (err) {36 console.log(err);37 }38 else {39 console.log('App restarted');40 }41});42driver.backgroundApp(function(err){43 if (err) {44 console.log(err);45 }46 else {47 console.log('App restarted');48 }49});50driver.closeApp(function(err){51 if (err) {52 console.log(err);53 }54 else {55 console.log('App restarted');56 }57});58driver.launchApp(function(err){59 if (err) {60 console.log(err);61 }62 else {63 console.log('App restarted');64 }65});66driver.terminateApp(function(err){67 if (err) {68 console.log(err);69 }70 else {71 console.log('App restarted');72 }73});74driver.activateApp(function(err){75 if (err) {76 console.log(err);77 }78 else {79 console.log('App restarted');80 }81});82driver.endTestCoverage(function(err){83 if (err) {84 console.log(err);85 }86 else {87 console.log('App restarted');88 }89});90driver.startTestCoverage(function(err){91 if (err) {92 console.log(err);93 }94 else {95 console.log('App restarted');96 }97});98driver.lock(function(err){99 if (err) {100 console.log(err);101 }102 else {103 console.log('App restarted');104 }105});

Full Screen

Using AI Code Generation

copy

Full Screen

1import { remote } from 'webdriverio';2import { AndroidDriver } from 'appium-android-driver';3import { Chromedriver } from 'appium-chromedriver';4import { ChromedriverRestart } from 'appium-chromedriver';5import { ChromedriverStart } from 'appium-chromedriver';6import { ChromedriverStop } from 'appium-chromedriver';7import { ChromedriverCleanup } from 'appium-chromedriver';8const opts = {9 capabilities: {

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Android Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful