How to use driver.setGeoLocation method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

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

Sign up Free
_

location-specs.js

Source: location-specs.js Github

copy
1import sinon from 'sinon';
2import XCUITestDriver from '../../..';
3import { fs } from 'appium-support';
4import * as teenProcess from 'teen_process';
5
6describe('location commands', function () {
7  const udid = '1234';
8  const toolName = 'idevicelocation';
9
10  const driver = new XCUITestDriver();
11  const proxySpy = sinon.stub(driver, 'proxyCommand');
12
13  afterEach(function () {
14    proxySpy.reset();
15  });
16
17  describe('setLocation', function () {
18    let execStub;
19    let fsWhichStub;
20
21    beforeEach(function () {
22      execStub = sinon.stub(teenProcess, 'exec');
23      fsWhichStub = sinon.stub(fs, 'which');
24    });
25
26    afterEach(function () {
27      execStub.restore();
28      fsWhichStub.restore();
29    });
30
31    it('should fail when location object is wrong', async function () {
32      await driver.setGeoLocation({}).should.be.rejectedWith('Both latitude and longitude should be set');
33    });
34
35    describe('on real device', function () {
36      beforeEach(function () {
37        driver.opts.udid = udid;
38        driver.opts.realDevice = true;
39      });
40
41      it('should use idevicelocation to set a location', async function () {
42        fsWhichStub.returns(toolName);
43        await driver.setGeoLocation({latitude: '1.234', longitude: '2.789'});
44
45        execStub.calledOnce.should.be.true;
46        execStub.firstCall.args[0].should.eql(toolName);
47        execStub.firstCall.args[1].should.eql(['-u', udid, '1.234', '2.789']);
48      });
49
50      it('should use idevicelocation to set a location with negative values', async function () {
51        fsWhichStub.returns(toolName);
52        await driver.setGeoLocation({latitude: 1.234, longitude: -2});
53
54        execStub.calledOnce.should.be.true;
55        execStub.firstCall.args[0].should.eql(toolName);
56        execStub.firstCall.args[1].should.eql(['-u', udid, '1.234', '--', '-2']);
57      });
58
59      it('should fail when idevicelocation doesnt exist on the host', async function () {
60        fsWhichStub.throws();
61        await driver.setGeoLocation({
62          latitude: '1.234',
63          longitude: '2.789'}
64        ).should.be.rejectedWith(`idevicelocation doesn't exist on the host`);
65      });
66    });
67
68    describe('on simulator', function () {
69      let deviceSetLocationSpy;
70      beforeEach(function () {
71        driver.opts.realDevice = false;
72
73        deviceSetLocationSpy = sinon.spy();
74        driver.opts.device = {
75          setGeolocation: deviceSetLocationSpy,
76        };
77      });
78      afterEach(function () {
79        deviceSetLocationSpy.resetHistory();
80      });
81      it('should set string coordinates', async function () {
82        await driver.setGeoLocation({latitude: '1.234', longitude: '2.789'});
83        deviceSetLocationSpy.firstCall.args[0].should.eql('1.234');
84        deviceSetLocationSpy.firstCall.args[1].should.eql('2.789');
85      });
86      it('should set number coordinates', async function () {
87        await driver.setGeoLocation({latitude: 1, longitude: -2});
88        deviceSetLocationSpy.firstCall.args[0].should.eql('1');
89        deviceSetLocationSpy.firstCall.args[1].should.eql('-2');
90      });
91    });
92  });
93});
94
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 Xcuitest 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)