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

execute-specs.js

Source: execute-specs.js Github

copy
1import chai from 'chai';
2import chaiAsPromised from 'chai-as-promised';
3import sinon from 'sinon';
4import AndroidDriver from '../../..';
5
6let driver;
7let sandbox = sinon.createSandbox();
8chai.should();
9chai.use(chaiAsPromised);
10
11describe('Execute', function () {
12  beforeEach(function () {
13    driver = new AndroidDriver();
14  });
15  afterEach(function () {
16    sandbox.restore();
17  });
18  describe('execute', function () {
19    it('should call sensorSet', async function () {
20      sandbox.stub(driver, 'sensorSet');
21      await driver.executeMobile('sensorSet', {sensorType: 'light', value: 0});
22      driver.sensorSet.calledWithExactly({sensorType: 'light', value: 0}).should.be.true;
23    });
24    it('should be reject if arguments are missing', function () {
25      driver.executeMobile('sensorSet', {sensor: 'light', value: 0})
26        .should.eventually.be.rejectedWith(`'sensorType' argument is required`);
27      driver.executeMobile('sensorSet', {sensorType: 'light', val: 0})
28        .should.eventually.be.rejectedWith(`'value' argument is required`);
29    });
30  });
31});
32
Full Screen

general-specs.js

Source: general-specs.js Github

copy
1import chai from 'chai';
2import chaiAsPromised from 'chai-as-promised';
3import sinon from 'sinon';
4import AndroidUiautomator2Driver from '../../..';
5import ADB from 'appium-adb';
6
7let driver;
8let sandbox = sinon.createSandbox();
9chai.should();
10chai.use(chaiAsPromised);
11
12describe('General', function () {
13  describe('getWindowRect', function () {
14    beforeEach(function () {
15      driver = new AndroidUiautomator2Driver();
16    });
17    afterEach(function () {
18      sandbox.restore();
19    });
20
21    it('should get window size', async function () {
22      sandbox.stub(driver, 'getWindowSize')
23          .withArgs().returns({width: 300, height: 400});
24      const result = await driver.getWindowRect();
25      result.width.should.be.equal(300);
26      result.height.should.be.equal(400);
27      result.x.should.be.equal(0);
28      result.y.should.be.equal(0);
29    });
30    it('should raise error on non-existent mobile command', async function () {
31      await driver.executeMobile('fruta', {}).should.eventually.be.rejectedWith('Unknown mobile command "fruta"');
32    });
33    it('should accept sensorSet on emulator', async function () {
34      sandbox.stub(driver, 'isEmulator').returns(true);
35      let stub = sandbox.stub(driver, 'sensorSet');
36      await driver.executeMobile('sensorSet', { sensorType: 'acceleration', value: '0:9.77631:0.812349' });
37      stub.calledOnce.should.equal(true);
38      stub.calledWithExactly({ sensorType: 'acceleration', value: '0:9.77631:0.812349' });
39    });
40  });
41
42  describe('mobileInstallMultipleApks', function () {
43    let adb = new ADB();
44
45    beforeEach(function () {
46      driver = new AndroidUiautomator2Driver();
47      driver.adb = adb;
48      driver.helpers = {
49        configureApp: () => {}
50      };
51    });
52    afterEach(function () {
53      sandbox.restore();
54    });
55
56    it('should call mobileInstallMultipleApks', async function () {
57      sandbox.stub(driver.helpers, 'configureApp')
58          .returns(['/path/to/test/apk.apk']);
59      sandbox.stub(driver.adb, 'installMultipleApks')
60          .withArgs().returns();
61      await driver.executeMobile('installMultipleApks',
62        {apks: ['/path/to/test/apk.apk']});
63    });
64
65    it('should raise error if no apks were given', async function () {
66      await driver.executeMobile('installMultipleApks', {apks: []})
67        .should.eventually.be.rejectedWith('No apks are given to install');
68    });
69
70    it('should raise error if no apks were given', async function () {
71      await driver.executeMobile('installMultipleApks', {})
72        .should.eventually.be.rejectedWith('No apks are given to install');
73    });
74  });
75});
76
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)