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

find-specs.js

Source: find-specs.js Github

copy
1import chai from 'chai';
2import chaiAsPromised from 'chai-as-promised';
3import sinon from 'sinon';
4import Bootstrap from '../../../lib/bootstrap';
5import AndroidDriver from '../../..';
6import { errors } from 'appium-base-driver';
7
8
9let driver;
10let sandbox = sinon.createSandbox();
11chai.should();
12chai.use(chaiAsPromised);
13
14describe('Find', function () {
15  beforeEach(function () {
16    driver = new AndroidDriver();
17    driver.bootstrap = new Bootstrap();
18    driver.implicitWaitMs = 0;
19    sandbox.stub(driver, 'validateLocatorStrategy');
20    sandbox.stub(driver.bootstrap, 'sendAction');
21    sandbox.stub(driver, 'doFindElementOrEls');
22  });
23  afterEach(function () {
24    sandbox.restore();
25  });
26  describe('doFindElementOrEls', function () {
27    it('should send find action to bootstrap', async function () {
28      driver.doFindElementOrEls.restore();
29      await driver.doFindElementOrEls('params');
30      driver.bootstrap.sendAction.calledWithExactly('find', 'params').should.be.true;
31    });
32  });
33  describe('findElorEls', function () {
34    it('should throw an error if there is no selector', async function () {
35      await driver.findElOrEls('xpath', null, false, 'some context')
36        .should.be.rejectedWith(/provide a selector/);
37    });
38    it('should be able to find element', async function () {
39      let params = {strategy: 'xpath', selector: '//*[1]', context: 'context', multiple: false};
40      driver.doFindElementOrEls.returns('el1');
41      await driver.findElOrEls('xpath', '//*[1]', false, 'context').should.become('el1');
42      driver.doFindElementOrEls.calledWithExactly(params).should.be.true;
43    });
44    it('should be able to find elements', async function () {
45      let params = {strategy: 'xpath', selector: '//*[1]', context: 'context', multiple: true};
46      driver.doFindElementOrEls.returns(['el1', 'el2']);
47      await driver.findElOrEls('xpath', '//*[1]', true, 'context')
48        .should.eventually.be.deep.equal(['el1', 'el2']);
49      driver.doFindElementOrEls.calledWithExactly(params).should.be.true;
50    });
51    it('should not throws NoSuchElementError when searching multiple if element does not exist', async function () {
52      driver.doFindElementOrEls.returns(null);
53      await driver.findElOrEls('xpaht', '//*[1]', true)
54        .should.eventually.be.deep.equal([]);
55    });
56    it('should throw NoSuchElementError if element does not exist', async function () {
57      driver.doFindElementOrEls.throws(new errors.NoSuchElementError());
58      await driver.findElOrEls('xpath', '//*[1]', false)
59        .should.be.rejectedWith(errors.NoSuchElementError);
60    });
61    it('should fails if locator strategy is not valid', async function () {
62      driver.validateLocatorStrategy.throws();
63      await driver.findElOrEls().should.be.rejected;
64    });
65    it('should fails if gets unexpected error', async function () {
66      driver.doFindElementOrEls.throws(new Error('unexpected_error'));
67      await driver.findElOrEls('x', 'loc').should.be.rejectedWith('unexpected_error');
68    });
69  });
70});
71
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)