How to use driver.onChromedriverStop method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

Run Appium Android Driver automation tests on LambdaTest cloud grid

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

context-specs.js

Source: context-specs.js Github

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

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Android Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)