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

validators.js

Source: validators.js Github

copy
1import _ from 'lodash';
2
3
4function isNumber (o) {
5  return _.isNumber(o) || !_.isNaN(parseInt(o)) || !_.isNaN(parseFloat(o));
6}
7
8function msValidator (ms) {
9  if (!_.isNumber(ms) || ms < 0) {
10    throw new Error('Wait ms must be a number equal to 0 or greater');
11  }
12}
13
14const validators = {
15  setUrl: (url) => {
16    // either an `xyz://` or `about:` scheme is allowed
17    if (!url || !url.match(/^([a-zA-Z0-9_-]+:\/\/)|(about:)/)) {
18      throw new Error('Url or Uri must start with <scheme>://');
19    }
20  },
21  implicitWait: (ms) => {
22    msValidator(ms);
23  },
24  asyncScriptTimeout: (ms) => {
25    msValidator(ms);
26  },
27  timeouts: (type, ms) => {
28    msValidator(ms);
29    if (!_.contains(['script', 'implicit', 'page load', 'command'], type)) {
30      throw new Error(`'${type}' is not a valid timeout type`);
31    }
32  },
33  clickCurrent: (button) => {
34    if (!(isNumber(button) || _.isUndefined(button)) || (button < 0 || button > 2)) {
35      throw new Error('Click button must be 0, 1, or 2');
36    }
37  },
38  setNetworkConnection: (type) => {
39    if (!isNumber(type) || [0, 1, 2, 4, 6].indexOf(type) === -1) {
40      throw new Error('Network type must be one of 0, 1, 2, 4, 6');
41    }
42  }
43};
44
45export { validators };
46
Full Screen

main.js

Source: main.js Github

copy
1const fs = require('fs');
2const path = require('path');
3const uuidv4 = require('uuid');
4const appDir = path.dirname(require.main.filename);
5
6const SITE_JSON_PATH = appDir + '/data/siteinfo.json';
7const AUTH_JSON_PATH = appDir + '/data/auth.json';
8
9class CMS {
10  constructor(data = {}) {
11    data = Object.assign({}, CMS.defaults(), data);
12    this.id = data.id;
13    this.sitename = data.sitename;
14    this.frontendurl = data.frontendurl;
15    this.apiurl = data.apiurl;
16    this.haswww = data.haswww;
17    this.isHttps = data.isHttps;
18    this.authkey = data.authkey;
19
20    this.googleAdClient = data.googleAdClient;
21    this.googleAnalytics = data.googleAnalytics;
22    this.msValidator = data.msValidator;
23    this.defMetaDesc = data.defMetaDesc;
24    this.defMetaKey = data.defMetaKey;
25    this.lang = data.lang;
26  }
27
28  validate() {
29    if (!this.sitename || !this.frontendurl || !this.apiurl) {
30      return false;
31    }
32    return true;
33  }
34
35  /**
36   * Returns new object with current data.
37   * @param {{needAuthKey:boolean;}} data Optional, has props like authkey flag
38   */
39  toJSON(data = {}) {
40    if (data.needAuthKey == void 0) { data.needAuthKey = false; }
41    const json = {};
42    json.id = this.id;
43    json.sitename = this.sitename;
44    json.frontendurl = this.frontendurl;
45    json.apiurl = this.apiurl;
46    json.haswww = this.haswww;
47    json.isHttps = this.isHttps;
48
49    if (data.needAuthKey) {
50      json.authkey = this.authkey;
51    }
52
53    json.googleAdClient = this.googleAdClient;
54    json.googleAnalytics = this.googleAnalytics;
55    json.msValidator = this.msValidator;
56    json.defMetaDesc = this.defMetaDesc;
57    json.defMetaKey = this.defMetaKey;
58    json.lang = this.lang;
59    return json;
60  }
61
62  clean() {
63    for (let key in this) {
64      if (this.hasOwnProperty(key)) {
65        delete this[key];
66      }
67    }
68  }
69
70  set(data) {
71    this.clean();
72    Object.assign(this, {}, data);
73  }
74
75  getFrontendURL() {
76    let www = '';
77    if (this.haswww) {
78      www = 'www.';
79    }
80
81    let https = 'http://';
82    if (this.isHttps) {
83      https = 'https://';
84    }
85
86    return https + www + this.frontendurl;
87  }
88
89  getAPIURL() {
90    let https = 'http://';
91    if (this.isHttps) {
92      https = 'https://';
93    }
94
95    return https + this.apiurl;
96  }
97}
98
99CMS.SCHEMA = {
100  properties: [{
101    name: 'id',
102    isPrimaryKey: true,
103    type: 'uuid'
104  }, {
105    name: 'sitename',
106    type: 'string'
107  }, {
108    name: 'frontendurl',
109    type: 'string'
110  }, {
111    name: 'apiurl',
112    type: 'uuid'
113  }, {
114    name: 'haswww',
115    type: 'boolean'
116  }, {
117    name: 'isHttps',
118    type: 'boolean'
119  }, {
120    name: 'authkey',
121    type: 'string'
122  }, {
123    name: 'googleAdClient',
124    type: 'string'
125  }, {
126    name: 'googleAnalytics',
127    type: 'string'
128  }, {
129    name: 'msValidator',
130    type: 'string'
131  }, {
132    name: 'defMetaDesc',
133    type: 'string'
134  }, {
135    name: 'defMetaKey',
136    type: 'string'
137  }, {
138    name: 'lang',
139    type: 'string'
140  }]
141};
142
143CMS.defaults = () => {
144  return {
145    id: uuidv4.v4(),
146    haswww: true,
147    isHttps: false,
148    googleAdClient: '',
149    googleAnalytics: '',
150    msValidator: '',
151    defMetaDesc: '',
152    defMetaKey: '',
153    lang: ''
154  };
155};
156
157CMS.isConfigured = false;
158
159/**
160 * Returns promise containing JSON object.
161 * @param {string} path path of the json to read from
162 */
163CMS.readJSON = (path) => {
164  return new Promise((res, rej) => {
165    fs.readFile(path, 'utf8', (err, data) => {
166      if (err) {
167        rej(err);
168      } else {
169        res(JSON.parse(data));
170      }
171    });
172  });
173};
174
175/**
176 * Writes json by stringifying it
177 * Returns promise containing JSON object.
178 * @param {string} path path of the json to write to
179 * @param {Object} json JSON object to write
180 */
181CMS.writeJSON = (path, json) => {
182  return new Promise((res, rej) => {
183    fs.writeFile(path, JSON.stringify(json), (err) => {
184      if (err) {
185        rej(err);
186      } else {
187        res(json);
188      }
189    });
190  });
191};
192
193CMS.readConfigJSONFIle = () => CMS.readJSON(SITE_JSON_PATH);
194
195/**
196 * Automatically configures site incase of memory failure.
197 */
198CMS.autoConfigure = () => {
199  return new Promise((res, rej) => {
200    CMS.readConfigJSONFIle().then((data) => {
201      CMS._instance.set(data);
202      CMS.isConfigured = true;
203      res({ message: 'configured', siteinfo: CMS._instance.toJSON() });
204      return data;
205    }, (err) => {
206      if (err.code == 'ENOENT') {
207        res({ message: 'jsonfilenotpresent' });
208        CMS.isConfigured = false;
209        console.log('Site is not configured yet, needs to be configured');
210      } else {
211        CMS.isConfigured = false;
212        rej(err);
213      }
214    });
215  });
216};
217
218CMS._instance = new CMS();
219
220CMS.getInstance = () => CMS._instance;
221
222module.exports = { CMS };
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)