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

element-specs.js

Source: element-specs.js Github

copy
1import chai from 'chai';
2import chaiAsPromised from 'chai-as-promised';
3import sinon from 'sinon';
4import Bootstrap from 'appium-android-bootstrap';
5import AndroidDriver from '../../..';
6import ADB from 'appium-adb';
7import androidHelpers from '../../../lib/android-helpers';
8
9let driver;
10let sandbox = sinon.sandbox.create();
11chai.should();
12chai.use(chaiAsPromised);
13
14describe('Element', () => {
15  beforeEach(() => {
16    driver = new AndroidDriver();
17    driver.adb = new ADB();
18    driver.bootstrap = new Bootstrap();
19    sandbox.stub(driver.bootstrap, 'sendAction');
20    sandbox.stub(androidHelpers, 'removeNullProperties');
21    driver.opts = {unicodeKeyboard: true};
22  });
23  afterEach(() => {
24    sandbox.restore();
25  });
26  describe('getAttribute', () => {
27    it('should get element attribute', async () => {
28      driver.bootstrap.sendAction.withArgs('element:getAttribute').returns('attr_value');
29      await driver.getAttribute('attr', 'el1').should.become('attr_value');
30      driver.bootstrap.sendAction
31        .calledWithExactly('element:getAttribute', {attribute: 'attr', elementId: 'el1'})
32        .should.be.true;
33    });
34  });
35  describe('getName', () => {
36    it('should get element name', async () => {
37      sandbox.stub(driver, 'getAttribute');
38      driver.getAttribute.returns('el_name');
39      await driver.getName('el1').should.become('el_name');
40      driver.getAttribute.calledWithExactly('className', 'el1').should.be.true;
41    });
42  });
43  describe('elementDisplayed', () => {
44    it('should return true if element displayed', async () => {
45      sandbox.stub(driver, 'getAttribute');
46      driver.getAttribute.returns('true');
47      await driver.elementDisplayed('el1').should.become(true);
48      driver.getAttribute.calledWithExactly('displayed', 'el1').should.be.true;
49    });
50    it('should return false if element not displayed', async () => {
51      sandbox.stub(driver, 'getAttribute');
52      driver.getAttribute.returns('false');
53      await driver.elementDisplayed('el1').should.become(false);
54      driver.getAttribute.calledWithExactly('displayed', 'el1').should.be.true;
55    });
56  });
57  describe('elementEnabled', () => {
58    it('should return true if element enabled', async () => {
59      sandbox.stub(driver, 'getAttribute');
60      driver.getAttribute.returns('true');
61      await driver.elementEnabled('el1').should.become(true);
62      driver.getAttribute.calledWithExactly('enabled', 'el1').should.be.true;
63    });
64    it('should return false if element not enabled', async () => {
65      sandbox.stub(driver, 'getAttribute');
66      driver.getAttribute.returns('false');
67      await driver.elementEnabled('el1').should.become(false);
68      driver.getAttribute.calledWithExactly('enabled', 'el1').should.be.true;
69    });
70  });
71  describe('elementSelected', () => {
72    it('should return true if element selected', async () => {
73      sandbox.stub(driver, 'getAttribute');
74      driver.getAttribute.returns('true');
75      await driver.elementSelected('el1').should.become(true);
76      driver.getAttribute.calledWithExactly('selected', 'el1').should.be.true;
77    });
78    it('should return false if element not selected', async () => {
79      sandbox.stub(driver, 'getAttribute');
80      driver.getAttribute.returns('false');
81      await driver.elementSelected('el1').should.become(false);
82      driver.getAttribute.calledWithExactly('selected', 'el1').should.be.true;
83    });
84  });
85  describe('setElementValue', () => {
86    const params = {elementId: 'el0', text: 'text to set',  replace: true,
87                  unicodeKeyboard: true};
88    it('should call doSetElementValue', async () => {
89      sandbox.stub(driver, 'doSetElementValue');
90      await driver.setElementValue('text to set', 'el0', true);
91      driver.doSetElementValue.calledWithExactly(params).should.be.true;
92    });
93    it('should join keys parameter if keys is instance of Array', async () => {
94      sandbox.stub(driver, 'doSetElementValue');
95      await driver.setElementValue(['t', 'ext', ' to ', 'se', 't'], 'el0', true);
96      driver.doSetElementValue.calledWithExactly(params).should.be.true;
97    });
98    it('should set replace to false by default', async () => {
99      params.replace = false;
100      sandbox.stub(driver, 'doSetElementValue');
101      await driver.setElementValue(['t', 'ext', ' to ', 'se', 't'], 'el0');
102      driver.doSetElementValue.calledWithExactly(params).should.be.true;
103    });
104  });
105  describe('doSetElementValue', () => {
106    it('should call setText to set element value', async () => {
107      await driver.doSetElementValue('params');
108      driver.bootstrap.sendAction.calledWithExactly('element:setText',
109        'params').should.be.true;
110    });
111  });
112  describe('setValue', () => {
113    it('should call setElementValue to set value', async () => {
114      sandbox.stub(driver, 'setElementValue');
115      await driver.setValue('keys', 'el1');
116      driver.setElementValue.calledWithExactly('keys', 'el1', false).should.be.true;
117    });
118  });
119  describe('replaceValue', () => {
120    it('should call setElementValue to replace value', async () => {
121      sandbox.stub(driver, 'setElementValue');
122      await driver.replaceValue('keys', 'el1');
123      driver.setElementValue.calledWithExactly('keys', 'el1', true).should.be.true;
124    });
125  });
126  describe('setValueImmediate', () => {
127    it('should set value via adb inputText command', async () => {
128      sandbox.stub(driver, 'click');
129      sandbox.stub(driver.adb, 'inputText');
130      await driver.setValueImmediate('keys', 'el1');
131      driver.click.calledWithExactly('el1').should.be.true;
132      driver.adb.inputText.calledWithExactly('keys').should.be.true;
133    });
134    it('should join keys parameter if keys is instance of Array', async () => {
135      sandbox.stub(driver, 'click');
136      sandbox.stub(driver.adb, 'inputText');
137      await driver.setValueImmediate(['k', 'ey', 's'], 'el1');
138      driver.adb.inputText.calledWithExactly('keys').should.be.true;
139    });
140  });
141  describe('getText', () => {
142    it('should get element text', async () => {
143      driver.bootstrap.sendAction.withArgs('element:getText').returns('el_text');
144      await driver.getText('el1').should.become('el_text');
145      driver.bootstrap.sendAction
146        .calledWithExactly('element:getText', {elementId: 'el1'})
147        .should.be.true;
148    });
149  });
150  describe('clear', () => {
151    it('should clear text of an element', async () => {
152      sandbox.stub(driver, 'getText');
153      sandbox.stub(driver, 'click');
154      sandbox.stub(driver.adb, 'clearTextField');
155      driver.getText.withArgs('el1').returns('#'.repeat(110));
156      await driver.clear('el1');
157      driver.getText.calledWithExactly('el1').should.be.true;
158      driver.click.calledWithExactly('el1').should.be.true;
159      driver.adb.clearTextField.getCall(0).args[0].should.be.equal(50);
160      driver.adb.clearTextField.getCall(1).args[0].should.be.equal(50);
161      driver.adb.clearTextField.getCall(2).args[0].should.be.equal(10);
162    });
163    it('should do five retries and then fail if clearTextField throws error', async function () {
164      this.timeout(10000);
165
166      sandbox.stub(driver, 'getText');
167      sandbox.stub(driver, 'click');
168      sandbox.stub(driver.adb, 'clearTextField');
169      driver.adb.clearTextField.throws();
170      driver.getText.withArgs('el1').returns('#'.repeat(1));
171      await driver.clear('el1').should.be.rejected;
172      driver.adb.clearTextField.alwaysCalledWith(1).should.be.true;
173      driver.adb.clearTextField.callCount.should.be.equal(5);
174    });
175    it('it should assume that the text have 100 chars if getText returns empty string', async () => {
176      sandbox.stub(driver, 'getText');
177      sandbox.stub(driver, 'click');
178      sandbox.stub(driver.adb, 'clearTextField');
179      driver.getText.withArgs('el1').returns('');
180      await driver.clear('el1');
181      driver.adb.clearTextField.getCall(0).args[0].should.be.equal(50);
182      driver.adb.clearTextField.getCall(1).args[0].should.be.equal(50);
183    });
184  });
185  describe('click', () => {
186    it('should click an element', async () => {
187      await driver.click('el1');
188      driver.bootstrap.sendAction.calledWithExactly('element:click', {elementId: 'el1'})
189        .should.be.true;
190    });
191  });
192  describe('getLocation', () => {
193    it('should get location of an element', async () => {
194      driver.bootstrap.sendAction
195        .withArgs('element:getLocation').returns('loc_info');
196      await driver.getLocation('el1').should.become('loc_info');
197      driver.bootstrap.sendAction
198        .calledWithExactly('element:getLocation', {elementId: 'el1'})
199        .should.be.true;
200    });
201  });
202  describe('getLocationInView', () => {
203    it('should get location of an element', async () => {
204      sandbox.stub(driver, 'getLocation');
205      driver.getLocation.returns('loc_info');
206      await driver.getLocationInView('el1').should.become('loc_info');
207      driver.getLocation.calledWithExactly('el1').should.be.true;
208    });
209  });
210  describe('getSize', () => {
211    it('should get size of an element', async () => {
212      driver.bootstrap.sendAction
213        .withArgs('element:getSize').returns('size_info');
214      await driver.getSize('el1').should.become('size_info');
215      driver.bootstrap.sendAction
216        .calledWithExactly('element:getSize', {elementId: 'el1'})
217        .should.be.true;
218    });
219  });
220  describe('touchLongClick', () => {
221    it('should do touch long click on element', async () => {
222      let params = {elementId: 'el1', x: 12, y: 34, duration: 5};
223      await driver.touchLongClick('el1', 12, 34, 5);
224      androidHelpers.removeNullProperties.calledWithExactly(params)
225        .should.be.true;
226      driver.bootstrap.sendAction.calledWithExactly('element:touchLongClick', params)
227        .should.be.true;
228    });
229  });
230  describe('touchDown', () => {
231    it('should do touch down on element', async () => {
232      let params = {elementId: 'el1', x: 12, y: 34};
233      await driver.touchDown('el1', 12, 34);
234      androidHelpers.removeNullProperties.calledWithExactly(params)
235        .should.be.true;
236      driver.bootstrap.sendAction.calledWithExactly('element:touchDown', params)
237        .should.be.true;
238    });
239  });
240  describe('touchUp', () => {
241    it('should do touch up on element', async () => {
242      let params = {elementId: 'el1', x: 12, y: 34};
243      await driver.touchUp('el1', 12, 34);
244      androidHelpers.removeNullProperties.calledWithExactly(params)
245        .should.be.true;
246      driver.bootstrap.sendAction.calledWithExactly('element:touchUp', params)
247        .should.be.true;
248    });
249  });
250  describe('touchMove', () => {
251    it('should get element attribute', async () => {
252      let params = {elementId: 'el1', x: 12, y: 34};
253      await driver.touchMove('el1', 12, 34);
254      androidHelpers.removeNullProperties.calledWithExactly(params)
255        .should.be.true;
256      driver.bootstrap.sendAction.calledWithExactly('element:touchMove', params)
257        .should.be.true;
258    });
259  });
260  describe('complexTap', () => {
261    it('should tap an element', async () => {
262      await driver.complexTap(null, null, null, 12, 34);
263      driver.bootstrap.sendAction.calledWithExactly('click', {x: 12, y:34})
264        .should.be.true;
265    });
266  });
267  describe('tap', () => {
268    it('shoulde tap an element', async () => {
269      await driver.tap('el1', 12, 34, 3);
270      driver.bootstrap.sendAction.alwaysCalledWith('element:click',
271        {elementId: 'el1', x: 12, y: 34}).should.be.true;
272      driver.bootstrap.sendAction.calledThrice.should.be.true;
273    });
274    it('should tap without an element', async () => {
275      await driver.tap(null, 12, 34, 3);
276      driver.bootstrap.sendAction.alwaysCalledWith('click', {x: 12, y: 34})
277        .should.be.true;
278      driver.bootstrap.sendAction.calledThrice.should.be.true;
279    });
280    it('should perform single tap on element if x, y and count are not passed', async () => {
281      await driver.tap('el1');
282      driver.bootstrap.sendAction.alwaysCalledWith('element:click').should.be.true;
283      driver.bootstrap.sendAction.calledOnce.should.be.true;
284    });
285  });
286});
287
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)