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

touch-specs.js

Source: touch-specs.js Github

copy
1import setup from '../setup-base';
2import desired from './desired';
3import B from 'bluebird';
4import { okIfAlert } from '../helpers/recipes';
5
6//let okIfAlert = require('../../../helpers/alert').okIfAlert,
7
8describe('testapp - touch actions', function () {
9  let session = setup(this, desired);
10  let driver = session.driver;
11
12  async function goToMap () {
13    let map;
14    try {
15      map = await driver.findElement('xpath', '//UIAMapView');
16    } catch (ign) {}
17    if (!map) {
18      let buttons = await driver.findElements('class name', 'UIAButton');
19      let gestures = [{action: 'tap', options: {element: buttons[5].ELEMENT}}];
20      await driver.performTouch(gestures);
21      await B.delay(500);
22      await okIfAlert(driver);
23      await B.delay(500);
24    }
25  }
26
27  describe('tap', function () {
28    it('should tap on a specified element', async function () {
29      let buttons = await driver.findElements('class name', 'UIAButton');
30      let gestures = [{action: 'tap', options: { element: buttons[1].ELEMENT}}];
31      await driver.performTouch(gestures);
32      await B.delay(1000);
33      await okIfAlert(driver);
34    });
35  });
36
37  describe('wait', function () {
38    it('should move the page and wait a bit', async function () {
39      await goToMap();
40      let map = await driver.findElement('xpath', '//UIAMapView');
41      let gestures = [
42        {action: 'press', options: {element: map.ELEMENT}},
43        {action: 'moveTo', options: {element: map.ELEMENT, x: 0, y: 100}},
44        {action: 'wait', options: {ms: 5000}},
45        {action: 'moveTo', options: {element: map.ELEMENT, x: 0, y: 0}},
46        {action: 'release'}
47      ];
48      await driver.performTouch(gestures);
49    });
50  });
51
52  describe('pinch', function () {
53    it('should do some pinching', async function () {
54      await goToMap();
55      let map = await driver.findElement('xpath', '//UIAMapView');
56      let actions = [
57        [
58          {action: 'press', options: {element: map.ELEMENT}},
59          {action: 'moveTo', options: {element: map.ELEMENT, x: 0, y: 0}},
60          {action: 'release'}
61        ],
62        [
63          {action: 'press', options: {element: map.ELEMENT}},
64          {action: 'moveTo', options: {element: map.ELEMENT, x: 100, y: 100}},
65          {action: 'release'}
66        ],
67      ];
68      await driver.performMultiAction(actions);
69      await B.delay(1000);
70    });
71
72    it('should do more involved pinching in and out', async function () {
73      await goToMap();
74      let map = await driver.findElement('xpath', '//UIAMapView');
75      let actions = [
76        [
77          {action: 'press', options: {element: map.ELEMENT}},
78          {action: 'moveTo', options: {element: map.ELEMENT, x: 25, y: 25}},
79          {action: 'wait', options: {ms: 3000}},
80          {action: 'moveTo', options: {element: map.ELEMENT, x: 100, y: 100}},
81          {action: 'release'}
82        ],
83        [
84          {action: 'press', options: {element: map.ELEMENT}},
85          {action: 'moveTo', options: {element: map.ELEMENT, x: 100, y: 0}},
86          {action: 'wait', options: {ms: 3000}},
87          {action: 'moveTo', options: {element: map.ELEMENT, x: 0, y: 0}},
88          {action: 'release'}
89        ],
90      ];
91      await driver.performMultiAction(actions);
92      await B.delay(1000);
93    });
94  });
95});
96
97describe('testapp - swipe actions', function () {
98  let session = setup(this, desired);
99  let driver = session.driver;
100
101  describe('swipe', function () {
102    let slider, target, loc;
103    let leftPos = { x: 0, y: 0 },
104        rightPos = { x: 0, y: 0 },
105        centerPos = { x: 0, y: 0 };
106
107    let getNumericValue = function (pctVal) {
108      pctVal = pctVal.replace('%', '');
109      pctVal = parseInt(pctVal, 10);
110      return pctVal;
111    };
112
113    let testSliderValueNot0or100 = function (value) {
114      value = getNumericValue(value);
115      // should be ~50
116      value.should.be.above(15);
117      value.should.be.below(85);
118    };
119
120    let getSliderValue = async function () {
121      return await driver.getAttribute('value', slider);
122    };
123
124    before(async function () {
125      slider = await driver.findElement('class name', 'UIASlider');
126      loc = await driver.getLocation(slider);
127      let size = await driver.getSize(slider);
128      leftPos.x = loc.x - 5;
129      centerPos.x = loc.x + (size.width * 0.5);
130      rightPos.x = loc.x + size.width + 5;
131      leftPos.y = rightPos.y = centerPos.y = loc.y + (size.height * 0.5);
132      target = await driver.findElement('accessibility id', "Access'ibility");
133      testSliderValueNot0or100(await getSliderValue());
134    });
135
136    // TODO: For some reason it does not swipe to 100% in ci env, investigate
137    it('should work with: press {element}, moveTo {destEl} @skip-ci', async function () {
138      let origValue = await getSliderValue();
139      let gestures = [
140        {action: 'press', options: {element: slider.ELEMENT}},
141        {action: 'wait', options: {ms: 500}},
142        {action: 'moveTo', options: {element: target.ELEMENT}},
143        {action: 'release'}
144      ];
145      await driver.performTouch(gestures);
146      (await getSliderValue()).should.not.equal(origValue);
147      await B.delay(1000);
148      // TODO: in ios84 the destEl is in a weird place, so we don't test value
149      //(await getSliderValue()).should.equal("100%")
150    });
151
152    it('should work with: press {element, x, y}, moveTo {element, x, y}', async function () {
153      let gestures = [
154        {action: 'press', options: {element: slider.ELEMENT, x: 0.8665, y: 0.5}},
155        {action: 'wait', options: {ms: 500}},
156        {action: 'moveTo', options: {element: slider.ELEMENT, x: 0.5, y: 0.5}},
157        {action: 'release'}
158      ];
159      await driver.performTouch(gestures);
160      testSliderValueNot0or100(await getSliderValue());
161      await B.delay(1000);
162    });
163
164    it('should work with: press {x, y}, moveTo {x, y}', async function () {
165      let gestures = [
166        {action: 'press', options: {x: centerPos.x, y: centerPos.y}},
167        {action: 'wait', options: {ms: 500}},
168        {action: 'moveTo', options: {x: leftPos.x - centerPos.x, y: leftPos.y - centerPos.y}},
169        {action: 'release'}
170      ];
171      await driver.performTouch(gestures);
172      (await getSliderValue()).should.equal('0%');
173    });
174
175    it('should work with: {element, x, y}, moveTo {destEl, x, y} @skip-ci', async function () {
176      let gestures = [
177        {action: 'press', options: {element: slider.ELEMENT, x: 0, y: 0.5}},
178        {action: 'wait', options: {ms: 500}},
179        {action: 'moveTo', options: {element: target.ELEMENT, x: 50, y: 0.5}},
180        {action: 'release'}
181      ];
182      await driver.performTouch(gestures);
183      testSliderValueNot0or100(await getSliderValue());
184    });
185
186    // TODO: Crashes in ci env, investigate
187    // TODO: For some reason it does not swipe to 100% in ci env, investigate
188    it('should work with press {x, y}, moveTo {destEl} @skip-ci', async function () {
189      let origValue = await getSliderValue();
190      let gestures = [
191        {action: 'press', options: {x: centerPos.x, y: centerPos.y}},
192        {action: 'wait', options: {ms: 500}},
193        {action: 'moveTo', options: {element: target.ELEMENT}},
194        {action: 'release'}
195      ];
196      await driver.performTouch(gestures);
197      (await getSliderValue()).should.not.equal(origValue);
198      // TODO: weird element position in iOS 8.4 so not checking exact value.
199      //.then(getSliderValue).should.become("100%")
200    });
201  });
202});
203
Full Screen

gesture-specs.js

Source: gesture-specs.js Github

copy
1import sinon from 'sinon';
2import XCUITestDriver from '../../..';
3
4
5describe('gesture commands', () => {
6  let driver = new XCUITestDriver();
7  let proxySpy = sinon.stub(driver, 'proxyCommand');
8
9  afterEach(() => {
10    proxySpy.reset();
11  });
12
13  describe('tap', () => {
14    it('should send POST request to /tap on WDA when no element is given', async () => {
15      let actions = [
16        {action: 'tap'}
17      ];
18      await driver.performTouch(actions);
19      proxySpy.calledOnce.should.be.true;
20      proxySpy.firstCall.args[0].should.eql('/tap/0');
21      proxySpy.firstCall.args[1].should.eql('POST');
22    });
23    it('should send POST request to /tap/element on WDA', async () => {
24      let actions = [
25        {action: 'tap', options: {element: 42}}
26      ];
27      await driver.performTouch(actions);
28      proxySpy.calledOnce.should.be.true;
29      proxySpy.firstCall.args[0].should.eql('/tap/42');
30      proxySpy.firstCall.args[1].should.eql('POST');
31    });
32    it('should send POST request to /tap/element with offset on WDA', async () => {
33      let actions = [
34        {action: 'tap', options: {element: 42, x: 1, y: 2}}
35      ];
36      await driver.performTouch(actions);
37      proxySpy.calledOnce.should.be.true;
38      proxySpy.firstCall.args[0].should.eql('/tap/42');
39      proxySpy.firstCall.args[1].should.eql('POST');
40    });
41  });
42
43  describe('mobile methods', () => {
44    describe('anything other than scroll', () => {
45      it('should throw an error', async () => {
46        await driver.execute('mobile: somesuch').should.be.rejected;
47      });
48    });
49
50    describe('scroll', () => {
51      it('should throw an error if no scroll type is specified', async () => {
52        await driver.execute('mobile: scroll', {element: 4})
53          .should.eventually.be.rejectedWith(/Mobile scroll supports the following strategies/);
54      });
55      it('should pass through bare element', async () => {
56        await driver.execute('mobile: scroll', {element: 4, direction: 'down'});
57        proxySpy.calledOnce.should.be.true;
58        proxySpy.firstCall.args[0].should.eql('/uiaElement/4/scroll');
59        proxySpy.firstCall.args[1].should.eql('POST');
60      });
61      it('should unpack element object', async () => {
62        await driver.execute('mobile: scroll', {element: {ELEMENT: 4}, direction: 'down'});
63        proxySpy.calledOnce.should.be.true;
64        proxySpy.firstCall.args[0].should.eql('/uiaElement/4/scroll');
65        proxySpy.firstCall.args[1].should.eql('POST');
66      });
67      it('should pass name strategy exclusively', async () => {
68        await driver.execute('mobile: scroll', {element: 4, direction: 'down', name: 'something'});
69        proxySpy.calledOnce.should.be.true;
70        proxySpy.firstCall.args[0].should.eql('/uiaElement/4/scroll');
71        proxySpy.firstCall.args[1].should.eql('POST');
72        proxySpy.firstCall.args[2].should.eql({name: 'something'});
73      });
74      it('should pass direction strategy exclusively', async () => {
75        await driver.execute('mobile: scroll', {element: 4, direction: 'down', predicateString: 'something'});
76        proxySpy.calledOnce.should.be.true;
77        proxySpy.firstCall.args[0].should.eql('/uiaElement/4/scroll');
78        proxySpy.firstCall.args[1].should.eql('POST');
79        proxySpy.firstCall.args[2].should.eql({direction: 'down'});
80      });
81      it('should pass predicateString strategy exclusively', async () => {
82        await driver.execute('mobile: scroll', {element: 4, toVisible: true, predicateString: 'something'});
83        proxySpy.calledOnce.should.be.true;
84        proxySpy.firstCall.args[0].should.eql('/uiaElement/4/scroll');
85        proxySpy.firstCall.args[1].should.eql('POST');
86        proxySpy.firstCall.args[2].should.eql({predicateString: 'something'});
87      });
88    });
89  });
90});
91
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)