How to use driver.gsmSignal 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.

actions-specs.js

Source: actions-specs.js Github

copy
1import chai from 'chai';
2import chaiAsPromised from 'chai-as-promised';
3import sinon from 'sinon';
4import Bootstrap from '../../../lib/bootstrap';
5import path from 'path';
6import AndroidDriver from '../../../lib/driver';
7import * as support from '@appium/support';
8import ADB from 'appium-adb';
9import jimp from 'jimp';
10import helpers from '../../../lib/commands/actions';
11import * as teen_process from 'teen_process';
12
13
14let driver;
15let sandbox = sinon.createSandbox();
16chai.should();
17chai.use(chaiAsPromised);
18
19describe('Actions', function () {
20  beforeEach(function () {
21    driver = new AndroidDriver();
22    driver.adb = new ADB();
23    driver.bootstrap = new Bootstrap();
24    sandbox.stub(driver.bootstrap, 'sendAction');
25  });
26  afterEach(function () {
27    sandbox.restore();
28  });
29  describe('keyevent', function () {
30    it('shoudle be able to execute keyevent via pressKeyCode', async function () {
31      sandbox.stub(driver, 'pressKeyCode');
32      await driver.keyevent('66', 'meta');
33      driver.pressKeyCode.calledWithExactly('66', 'meta').should.be.true;
34    });
35    it('should set metastate to null by default', async function () {
36      sandbox.stub(driver, 'pressKeyCode');
37      await driver.keyevent('66');
38      driver.pressKeyCode.calledWithExactly('66', null).should.be.true;
39    });
40  });
41  describe('pressKeyCode', function () {
42    it('shoudle be able to press key code', async function () {
43      await driver.pressKeyCode('66', 'meta');
44      driver.bootstrap.sendAction
45        .calledWithExactly('pressKeyCode', {keycode: '66', metastate: 'meta'})
46        .should.be.true;
47    });
48    it('should set metastate to null by default', async function () {
49      await driver.pressKeyCode('66');
50      driver.bootstrap.sendAction
51        .calledWithExactly('pressKeyCode', {keycode: '66', metastate: null})
52        .should.be.true;
53    });
54  });
55  describe('longPressKeyCode', function () {
56    it('shoudle be able to press key code', async function () {
57      await driver.longPressKeyCode('66', 'meta');
58      driver.bootstrap.sendAction
59        .calledWithExactly('longPressKeyCode', {keycode: '66', metastate: 'meta'})
60        .should.be.true;
61    });
62    it('should set metastate to null by default', async function () {
63      await driver.longPressKeyCode('66');
64      driver.bootstrap.sendAction
65        .calledWithExactly('longPressKeyCode', {keycode: '66', metastate: null})
66        .should.be.true;
67    });
68  });
69  describe('getOrientation', function () {
70    it('shoudle be able to get orientation', async function () {
71      driver.bootstrap.sendAction.withArgs('orientation', {naturalOrientation: false})
72        .returns('landscape');
73      await driver.getOrientation().should.become('LANDSCAPE');
74      driver.bootstrap.sendAction
75        .calledWithExactly('orientation', {naturalOrientation: false})
76        .should.be.true;
77    });
78  });
79  describe('setOrientation', function () {
80    it('shoudle be able to set orientation', async function () {
81      let opts = {orientation: 'SOMESCAPE', naturalOrientation: false};
82      await driver.setOrientation('somescape');
83      driver.bootstrap.sendAction.calledWithExactly('orientation', opts)
84        .should.be.true;
85    });
86  });
87  describe('fakeFlick', function () {
88    it('shoudle be able to do fake flick', async function () {
89      await driver.fakeFlick(12, 34);
90      driver.bootstrap.sendAction
91        .calledWithExactly('flick', {xSpeed: 12, ySpeed: 34}).should.be.true;
92    });
93  });
94  describe('fakeFlickElement', function () {
95    it('shoudle be able to do fake flick on element', async function () {
96      await driver.fakeFlickElement(5000, 56, 78, 1.32);
97      driver.bootstrap.sendAction
98        .calledWithExactly('element:flick',
99          {xoffset: 56, yoffset: 78, speed: 1.32, elementId: 5000})
100        .should.be.true;
101    });
102  });
103  describe('swipe', function () {
104    it('should swipe an element', function () {
105      let swipeOpts = {startX: 10, startY: 11, endX: 20, endY: 22,
106                       steps: 3, elementId: 'someElementId'};
107      driver.swipe(10, 11, 20, 22, 0.1, null, 'someElementId');
108      driver.bootstrap.sendAction.calledWithExactly('element:swipe', swipeOpts)
109        .should.be.true;
110    });
111    it('should swipe without an element', function () {
112      driver.swipe(0, 0, 1, 1, 0, 1);
113      driver.bootstrap.sendAction.calledWith('swipe').should.be.true;
114    });
115    it('should set start point to (0.5;0.5) if startX and startY are "null"', function () {
116      let swipeOpts = {startX: 0.5, startY: 0.5, endX: 0, endY: 0, steps: 0};
117      sandbox.stub(driver, 'doSwipe');
118      driver.swipe('null', 'null', 0, 0, 0);
119      driver.doSwipe.calledWithExactly(swipeOpts).should.be.true;
120    });
121  });
122  describe('pinchClose', function () {
123    it('should be able to pinch in element', async function () {
124      let pinchOpts = {direction: 'in', elementId: 'el01', percent: 0.5, steps: 5};
125      await driver.pinchClose(null, null, null, null, null, 0.5, 5, 'el01');
126      driver.bootstrap.sendAction.calledWithExactly('element:pinch', pinchOpts)
127        .should.be.true;
128    });
129  });
130  describe('pinchOpen', function () {
131    it('should be able to pinch out element', async function () {
132      let pinchOpts = {direction: 'out', elementId: 'el01', percent: 0.5, steps: 5};
133      await driver.pinchOpen(null, null, null, null, null, 0.5, 5, 'el01');
134      driver.bootstrap.sendAction.calledWithExactly('element:pinch', pinchOpts)
135        .should.be.true;
136    });
137  });
138  describe('flick', function () {
139    it('should call fakeFlickElement if element is passed', async function () {
140      sandbox.stub(driver, 'fakeFlickElement');
141      await driver.flick('elem', null, null, 1, 2, 3);
142      driver.fakeFlickElement.calledWith('elem', 1, 2, 3).should.be.true;
143    });
144    it('should call fakeFlick if element is not passed', async function () {
145      sandbox.stub(driver, 'fakeFlick');
146      await driver.flick(null, 1, 2);
147      driver.fakeFlick.calledWith(1, 2).should.be.true;
148    });
149  });
150  describe('drag', function () {
151    let dragOpts = {
152      elementId: 'elem1', destElId: 'elem2',
153      startX: 1, startY: 2, endX: 3, endY: 4, steps: 1
154    };
155    it('should drag an element', function () {
156      driver.drag(1, 2, 3, 4, 0.02, null, 'elem1', 'elem2');
157      driver.bootstrap.sendAction.calledWithExactly('element:drag', dragOpts)
158        .should.be.true;
159    });
160    it('should drag without an element', function () {
161      dragOpts.elementId = null;
162      driver.drag(1, 2, 3, 4, 0.02, null, null, 'elem2');
163      driver.bootstrap.sendAction.calledWithExactly('drag', dragOpts)
164        .should.be.true;
165    });
166  });
167  describe('lock', function () {
168    it('should call adb.lock()', async function () {
169      sandbox.stub(driver.adb, 'lock');
170      await driver.lock();
171      driver.adb.lock.calledOnce.should.be.true;
172    });
173  });
174  describe('isLocked', function () {
175    it('should call adb.isScreenLocked()', async function () {
176      sandbox.stub(driver.adb, 'isScreenLocked').returns('lock_status');
177      await driver.isLocked().should.become('lock_status');
178      driver.adb.isScreenLocked.calledOnce.should.be.true;
179    });
180  });
181  describe('openNotifications', function () {
182    it('should be able to open notifications', async function () {
183      await driver.openNotifications();
184      driver.bootstrap.sendAction.calledWithExactly('openNotification')
185        .should.be.true;
186    });
187  });
188  describe('setLocation', function () {
189    it('should be able to set location', async function () {
190      sandbox.stub(driver.adb, 'sendTelnetCommand');
191      await driver.setLocation('lat', 'long');
192      driver.adb.sendTelnetCommand.calledWithExactly('geo fix long lat')
193        .should.be.true;
194    });
195  });
196
197  describe('fingerprint', function () {
198    it('should call fingerprint adb command for emulator', async function () {
199      sandbox.stub(driver.adb, 'fingerprint');
200      sandbox.stub(driver, 'isEmulator').returns(true);
201      await driver.fingerprint(1111);
202      driver.adb.fingerprint.calledWithExactly(1111).should.be.true;
203    });
204    it('should throw exception for real device', async function () {
205      sandbox.stub(driver.adb, 'fingerprint');
206      sandbox.stub(driver, 'isEmulator').returns(false);
207      await driver.fingerprint(1111).should.be
208        .rejectedWith('fingerprint method is only available for emulators');
209      driver.adb.fingerprint.notCalled.should.be.true;
210    });
211  });
212  describe('sendSMS', function () {
213    it('should call sendSMS adb command for emulator', async function () {
214      sandbox.stub(driver.adb, 'sendSMS');
215      sandbox.stub(driver, 'isEmulator').returns(true);
216      await driver.sendSMS(4509, 'Hello Appium');
217      driver.adb.sendSMS.calledWithExactly(4509, 'Hello Appium')
218        .should.be.true;
219    });
220    it('should throw exception for real device', async function () {
221      sandbox.stub(driver.adb, 'sendSMS');
222      sandbox.stub(driver, 'isEmulator').returns(false);
223      await driver.sendSMS(4509, 'Hello Appium')
224        .should.be.rejectedWith('sendSMS method is only available for emulators');
225      driver.adb.sendSMS.notCalled.should.be.true;
226    });
227  });
228  describe('sensorSet', function () {
229    it('should call sensor adb command for emulator', async function () {
230      sandbox.stub(driver.adb, 'sensorSet');
231      sandbox.stub(driver, 'isEmulator').returns(true);
232      await driver.sensorSet({sensorType: 'light', value: 0});
233      driver.adb.sensorSet.calledWithExactly('light', 0)
234        .should.be.true;
235    });
236    it('should throw exception for real device', async function () {
237      sandbox.stub(driver.adb, 'sensorSet');
238      sandbox.stub(driver, 'isEmulator').returns(false);
239      await driver.sensorSet({sensorType: 'light', value: 0})
240        .should.be.rejectedWith('sensorSet method is only available for emulators');
241      driver.adb.sensorSet.notCalled.should.be.true;
242    });
243  });
244  describe('gsmCall', function () {
245    it('should call gsmCall adb command for emulator', async function () {
246      sandbox.stub(driver.adb, 'gsmCall');
247      sandbox.stub(driver, 'isEmulator').returns(true);
248      await driver.gsmCall(4509, 'call');
249      driver.adb.gsmCall.calledWithExactly(4509, 'call').should.be.true;
250    });
251    it('should throw exception for real device', async function () {
252      sandbox.stub(driver.adb, 'gsmCall');
253      sandbox.stub(driver, 'isEmulator').returns(false);
254      await driver.gsmCall(4509, 'call')
255        .should.be.rejectedWith('gsmCall method is only available for emulators');
256      driver.adb.gsmCall.notCalled.should.be.true;
257    });
258  });
259  describe('gsmSignal', function () {
260    it('should call gsmSignal adb command for emulator', async function () {
261      sandbox.stub(driver.adb, 'gsmSignal');
262      sandbox.stub(driver, 'isEmulator').returns(true);
263      await driver.gsmSignal(3);
264      driver.adb.gsmSignal.calledWithExactly(3)
265        .should.be.true;
266    });
267    it('should throw exception for real device', async function () {
268      sandbox.stub(driver.adb, 'gsmSignal');
269      sandbox.stub(driver, 'isEmulator').returns(false);
270      await driver.gsmSignal(3)
271        .should.be.rejectedWith('gsmSignal method is only available for emulators');
272      driver.adb.gsmSignal.notCalled.should.be.true;
273    });
274  });
275  describe('gsmVoice', function () {
276    it('should call gsmVoice adb command for emulator', async function () {
277      sandbox.stub(driver.adb, 'gsmVoice');
278      sandbox.stub(driver, 'isEmulator').returns(true);
279      await driver.gsmVoice('roaming');
280      driver.adb.gsmVoice.calledWithExactly('roaming')
281        .should.be.true;
282    });
283    it('should throw exception for real device', async function () {
284      sandbox.stub(driver.adb, 'gsmVoice');
285      sandbox.stub(driver, 'isEmulator').returns(false);
286      await driver.gsmVoice('roaming')
287        .should.be.rejectedWith('gsmVoice method is only available for emulators');
288      driver.adb.gsmVoice.notCalled.should.be.true;
289    });
290  });
291  describe('powerAC', function () {
292    it('should call powerAC adb command for emulator', async function () {
293      sandbox.stub(driver.adb, 'powerAC');
294      sandbox.stub(driver, 'isEmulator').returns(true);
295      await driver.powerAC('off');
296      driver.adb.powerAC.calledWithExactly('off')
297        .should.be.true;
298    });
299    it('should throw exception for real device', async function () {
300      sandbox.stub(driver.adb, 'powerAC');
301      sandbox.stub(driver, 'isEmulator').returns(false);
302      await driver.powerAC('roaming')
303        .should.be.rejectedWith('powerAC method is only available for emulators');
304      driver.adb.powerAC.notCalled.should.be.true;
305    });
306  });
307  describe('powerCapacity', function () {
308    it('should call powerCapacity adb command for emulator', async function () {
309      sandbox.stub(driver.adb, 'powerCapacity');
310      sandbox.stub(driver, 'isEmulator').returns(true);
311      await driver.powerCapacity(5);
312      driver.adb.powerCapacity.calledWithExactly(5)
313        .should.be.true;
314    });
315    it('should throw exception for real device', async function () {
316      sandbox.stub(driver.adb, 'powerCapacity');
317      sandbox.stub(driver, 'isEmulator').returns(false);
318      await driver.powerCapacity(5)
319        .should.be.rejectedWith('powerCapacity method is only available for emulators');
320      driver.adb.powerCapacity.notCalled.should.be.true;
321    });
322  });
323  describe('networkSpeed', function () {
324    it('should call networkSpeed adb command for emulator', async function () {
325      sandbox.stub(driver.adb, 'networkSpeed');
326      sandbox.stub(driver, 'isEmulator').returns(true);
327      await driver.networkSpeed('gsm');
328      driver.adb.networkSpeed.calledWithExactly('gsm')
329        .should.be.true;
330    });
331    it('should throw exception for real device', async function () {
332      sandbox.stub(driver.adb, 'networkSpeed');
333      sandbox.stub(driver, 'isEmulator').returns(false);
334      await driver.networkSpeed('gsm')
335        .should.be.rejectedWith('networkSpeed method is only available for emulators');
336      driver.adb.networkSpeed.notCalled.should.be.true;
337    });
338  });
339  describe('getScreenshotDataWithAdbShell', function () {
340    const defaultDir = '/data/local/tmp/';
341    const png = '/path/sc.png';
342    const localFile = 'local_file';
343    beforeEach(function () {
344      sandbox.stub(support.tempDir, 'path');
345      sandbox.stub(support.fs, 'exists');
346      sandbox.stub(support.fs, 'unlink');
347      sandbox.stub(driver.adb, 'shell');
348      sandbox.stub(driver.adb, 'pull');
349      sandbox.stub(path.posix, 'resolve');
350      sandbox.stub(jimp, 'read');
351      sandbox.stub(driver.adb, 'fileSize');
352      support.tempDir.path.returns(localFile);
353      support.fs.exists.withArgs(localFile).returns(true);
354      support.fs.unlink.withArgs(localFile).returns(true);
355      path.posix.resolve.withArgs(defaultDir, 'screenshot.png').returns(png);
356      driver.adb.fileSize.withArgs(png).returns(1);
357      jimp.read.withArgs(localFile).returns('screenshoot_context');
358    });
359    it('should be able to get screenshot via adb shell', async function () {
360      await helpers.getScreenshotDataWithAdbShell(driver.adb, {})
361        .should.become('screenshoot_context');
362      driver.adb.shell.calledWithExactly(['/system/bin/rm', `${png};`
363        , '/system/bin/screencap', '-p', png]).should.be.true;
364      driver.adb.pull.calledWithExactly(png, localFile).should.be.true;
365      jimp.read.calledWithExactly(localFile).should.be.true;
366      support.fs.exists.calledTwice.should.be.true;
367      support.fs.unlink.calledTwice.should.be.true;
368    });
369    it('should be possible to change default png dir', async function () {
370      path.posix.resolve.withArgs('/custom/path/tmp/', 'screenshot.png').returns(png);
371      await helpers.getScreenshotDataWithAdbShell(driver.adb
372        , {androidScreenshotPath: '/custom/path/tmp/'})
373        .should.become('screenshoot_context');
374    });
375    it('should throw error if size of the screenshot is zero', async function () {
376      driver.adb.fileSize.withArgs(png).returns(0);
377      await helpers.getScreenshotDataWithAdbShell(driver.adb, {})
378        .should.be.rejectedWith('equals to zero');
379    });
380  });
381  describe('getScreenshotDataWithAdbExecOut', function () {
382    it('should be able to take screenshot via exec-out', async function () {
383      sandbox.stub(teen_process, 'exec');
384      sandbox.stub(jimp, 'read');
385      teen_process.exec.returns({stdout: 'stdout', stderr: ''});
386      driver.adb.executable.path = 'path/to/adb';
387      await helpers.getScreenshotDataWithAdbExecOut(driver.adb);
388      teen_process.exec.calledWithExactly(driver.adb.executable.path,
389        driver.adb.executable.defaultArgs
390          .concat(['exec-out', '/system/bin/screencap', '-p']),
391        {encoding: 'binary', isBuffer: true}).should.be.true;
392      jimp.read.calledWithExactly('stdout').should.be.true;
393    });
394    it('should throw error if size of the screenshot is zero', async function () {
395      sandbox.stub(teen_process, 'exec');
396      teen_process.exec.returns({stdout: '', stderr: ''});
397      await helpers.getScreenshotDataWithAdbExecOut(driver.adb)
398        .should.be.rejectedWith('Screenshot returned no data');
399    });
400    it('should throw error if code is not 0', async function () {
401      sandbox.stub(teen_process, 'exec');
402      teen_process.exec.returns({code: 1, stdout: '', stderr: ''});
403      await helpers.getScreenshotDataWithAdbExecOut(driver.adb)
404        .should.be.rejectedWith(`Screenshot returned error, code: '1', stderr: ''`);
405    });
406    it('should throw error if stderr is not empty', async function () {
407      sandbox.stub(teen_process, 'exec');
408      teen_process.exec.returns({code: 0, stdout: '', stderr: 'Oops'});
409      await helpers.getScreenshotDataWithAdbExecOut(driver.adb)
410        .should.be.rejectedWith(`Screenshot returned error, code: '0', stderr: 'Oops'`);
411    });
412  });
413  describe('getScreenshot', function () {
414    let image;
415    beforeEach(function () {
416      image = new jimp(1, 1);
417      sandbox.stub(driver.adb, 'getApiLevel');
418      sandbox.stub(driver.adb, 'getScreenOrientation');
419      sandbox.stub(driver, 'getScreenshotDataWithAdbExecOut');
420      sandbox.stub(driver, 'getScreenshotDataWithAdbShell');
421      sandbox.stub(image, 'getBuffer').callsFake(function (mime, cb) { // eslint-disable-line promise/prefer-await-to-callbacks
422        return cb.call(this, null, Buffer.from('appium'));
423      });
424      sandbox.stub(image, 'rotate');
425      driver.adb.getScreenOrientation.returns(2);
426      image.rotate.withArgs(-180).returns(image);
427    });
428    it('should be able to take screenshot via exec-out (API level > 20)', async function () {
429      driver.adb.getApiLevel.returns(24);
430      driver.getScreenshotDataWithAdbExecOut.withArgs(driver.adb).returns(image);
431      await driver.getScreenshot().should.become('YXBwaXVt');
432      driver.getScreenshotDataWithAdbExecOut.calledOnce.should.be.true;
433      driver.getScreenshotDataWithAdbShell.notCalled.should.be.true;
434      image.getBuffer.calledWith(jimp.MIME_PNG).should.be.true;
435    });
436    it('should be able to take screenshot via adb shell (API level <= 20)', async function () {
437      driver.adb.getApiLevel.returns(20);
438      driver.getScreenshotDataWithAdbShell.withArgs(driver.adb, driver.opts).returns(image);
439      await driver.getScreenshot().should.become('YXBwaXVt');
440      driver.getScreenshotDataWithAdbShell.calledOnce.should.be.true;
441      driver.getScreenshotDataWithAdbExecOut.notCalled.should.be.true;
442      image.getBuffer.calledWith(jimp.MIME_PNG).should.be.true;
443    });
444    it('should tries to take screenshot via adb shell if exec-out failed (API level > 20)', async function () {
445      driver.adb.getApiLevel.returns(24);
446      driver.getScreenshotDataWithAdbExecOut.throws();
447      driver.getScreenshotDataWithAdbShell.withArgs(driver.adb, driver.opts).returns(image);
448      await driver.getScreenshot().should.become('YXBwaXVt');
449      driver.getScreenshotDataWithAdbShell.calledOnce.should.be.true;
450      driver.getScreenshotDataWithAdbShell.calledOnce.should.be.true;
451    });
452    it('should throw error if adb shell failed', async function () {
453      driver.adb.getApiLevel.returns(20);
454      driver.getScreenshotDataWithAdbShell.throws();
455      await driver.getScreenshot().should.be.rejectedWith('Cannot get screenshot');
456    });
457    it('should rotate image if API level < 23', async function () {
458      driver.adb.getApiLevel.returns(22);
459      driver.getScreenshotDataWithAdbExecOut.withArgs(driver.adb).returns(image);
460      await driver.getScreenshot();
461      driver.adb.getScreenOrientation.calledOnce.should.be.true;
462      image.rotate.calledOnce.should.be.true;
463    });
464    it('should not rotate image if API level >= 23', async function () {
465      driver.adb.getApiLevel.returns(23);
466      driver.getScreenshotDataWithAdbExecOut.withArgs(driver.adb).returns(image);
467      await driver.getScreenshot();
468      driver.adb.getScreenOrientation.notCalled.should.be.true;
469      image.rotate.notCalled.should.be.true;
470    });
471    it('should not throws error if rotate image failed', async function () {
472      image.rotate.resetBehavior();
473      image.rotate.throws();
474      driver.adb.getApiLevel.returns(22);
475      driver.getScreenshotDataWithAdbExecOut.withArgs(driver.adb).returns(image);
476      await driver.getScreenshot().should.be.fulfilled;
477      image.rotate.threw().should.be.true;
478    });
479  });
480});
481
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)