How to use finder.find method in Appium Base Driver

Best JavaScript code snippet using appium-base-driver

Run Appium Base Driver automation tests on LambdaTest cloud grid

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

finder.js

Source: finder.js Github

copy
1describe('RcFinder', function () {
2  var RcFinder = require('../');
3  var path = require('path');
4  var expect = require('expect.js');
5  var fs = require('fs');
6
7  var fixtures = {
8    root: path.resolve(__dirname, 'fixtures/foo/foo/foo/foo/'),
9    json: path.resolve(__dirname, 'fixtures/foo/foo/bar.json'),
10    text: path.resolve(__dirname, 'fixtures/foo/foo/.baz'),
11    checkCount: 4,
12    config: {
13      baz: 'bog'
14    }
15  };
16
17  it('looks for config files', function () {
18    var rcFinder = new RcFinder('bar.json');
19    var config = rcFinder.find(path.resolve(__dirname, 'fixtures/foo/foo/foo/foo/'));
20    expect(config).to.eql(fixtures.config);
21  });
22
23  it('can be run async by using a callback', function (done) {
24    var rcFinder = new RcFinder('bar.json');
25    var count = 0;
26    rcFinder.find(fixtures.root, function (err, config) {
27      expect(count).to.eql(1); // prove async
28      expect(config).to.eql(fixtures.config);
29      done();
30    });
31    count ++;
32  });
33
34  it('caches config objects', function () {
35    var count = 0;
36    var rcFinder = new RcFinder('bar.json', {
37      loader: function (path) {
38        count ++;
39        return JSON.parse(fs.readFileSync(path));
40      }
41    });
42
43    var config = rcFinder.find(fixtures.root);
44    expect(count).to.eql(1);
45    expect(config).to.eql(fixtures.config);
46
47    // it should only be loaded once
48    config = rcFinder.find(fixtures.root);
49    expect(count).to.eql(1);
50    expect(config).to.eql(fixtures.config);
51  });
52
53  it('caches config objects from async calls', function (done) {
54    var count = 0;
55    var rcFinder = new RcFinder('bar.json', {
56      loader: function (path, cb) {
57        count ++;
58        fs.readFile(path, function (err, file) {
59          var config;
60          if (!err) {
61            try {
62              config = JSON.parse(file);
63            } catch(e) {
64              err = cb(new Error(path + ' is not valid JSON: ' + e.message));
65            }
66          }
67          cb(err, config);
68        });
69      }
70    });
71
72    rcFinder.find(fixtures.root, function (err, config) {
73      expect(count).to.eql(1);
74      expect(config).to.eql(fixtures.config);
75
76      rcFinder.find(fixtures.root, function (err, config) {
77        expect(count).to.eql(1);
78        expect(config).to.eql(fixtures.config);
79        done();
80      });
81    });
82  });
83
84  it('throws an error when called without a callback by an async loader is in use', function () {
85    expect(function () {
86      var rcFinder = new RcFinder('bar.json', {
87        loader: 'async'
88      });
89
90      rcFinder.find(fixtures.root);
91    }).to.throwException();
92  });
93
94  it('properly caches sync lookups when a config file is not found', function () {
95    var count = 0;
96    var expectedCount = fixtures.checkCount;
97    var rcFinder = new RcFinder('bar.json', {
98      _syncCheck: function (path) {
99        count++;
100        return fs.existsSync(path);
101      }
102    });
103
104    expect(count).to.eql(0);
105    rcFinder.find(fixtures.root);
106    expect(count).to.eql(expectedCount);
107    rcFinder.find(fixtures.root);
108    // it should still equal previous count
109    expect(count).to.eql(expectedCount);
110  });
111
112  it('properly caches async lookups when a config file is not found', function () {
113    var count = 0;
114    var expectedCount = fixtures.checkCount;
115    var rcFinder = new RcFinder('bar.json', {
116      _asyncCheck: function (path, cb) {
117        count++;
118        fs.stat(path, function (err, exists) {
119          if (err && err.code !== 'ENOENT') return cb(err);
120          cb(void 0, !err);
121        });
122      }
123    });
124
125    expect(count).to.eql(0);
126    rcFinder.find(fixtures.root, function (err, config) {
127      expect(count).to.eql(expectedCount);
128      rcFinder.find(fixtures.root, function (err, config) {
129        // it should still equal previous count
130        expect(count).to.eql(expectedCount);
131      });
132    });
133  });
134
135  it('throws errors from loader when loading and calling synchronously', function() {
136    var rcFinder = new RcFinder('bar.json', {
137      loader: function(path) {
138        return JSON.parse('{not really json');
139      }
140    });
141
142    expect(function () {
143      var config = rcFinder.find(fixtures.root);
144    }).to.throwException('Unexpected token n');
145  });
146
147  it('propagates errors from loader when loading synchronously and calling async', function(done) {
148    var rcFinder = new RcFinder('bar.json', {
149      loader: function(path) {
150        return JSON.parse('{not really json');
151      }
152    });
153
154    rcFinder.find(fixtures.root, function(err, config) {
155      expect(err).to.be.an(Error);
156      expect(err.message).to.be('Unexpected token n');
157      expect(config).to.be(undefined);
158      done();
159    });
160  });
161
162  it('propagates error from loader when loading asynchronously', function(done) {
163    var rcFinder = new RcFinder('bar.json', {
164      loader: function(path, callback) {
165        process.nextTick(function() {
166          var err, body;
167          try {
168            body = JSON.parse('{not really json');
169          } catch (e) {
170            err = e;
171          } finally {
172            callback(err, body);
173          }
174        });
175      }
176    });
177
178    rcFinder.find(fixtures.root, function(err, config) {
179      expect(err).to.be.an(Error);
180      expect(err.message).to.be('Unexpected token n');
181      expect(config).to.be(undefined);
182      done();
183    });
184  });
185});
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 Base 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)