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

timeout.js

Source: timeout.js Github

copy
1import log from '../logger';
2import { waitForCondition } from 'asyncbox';
3import _ from 'lodash';
4import { util } from 'appium-support';
5import { errors } from '../../protocol';
6
7
8let commands = {}, helpers = {}, extensions = {};
9
10const MIN_TIMEOUT = 0;
11
12commands.timeouts = async function timeouts (type, ms, script, pageLoad, implicit) {
13  if (util.hasValue(type) && util.hasValue(ms)) {
14    log.debug(`MJSONWP timeout arguments: ${JSON.stringify({type, ms})}}`);
15
16    switch (type) {
17      case 'command':
18        await this.newCommandTimeout(ms);
19        return;
20      case 'implicit':
21        await this.implicitWaitMJSONWP(ms);
22        return;
23      case 'page load':
24        await this.pageLoadTimeoutMJSONWP(ms);
25        return;
26      case 'script':
27        await this.scriptTimeoutMJSONWP(ms);
28        return;
29      default:
30        throw new Error(`'${type}' type is not supported for MJSONWP timeout`);
31    }
32  }
33
34  // Otherwise assume it is W3C protocol
35  log.debug(`W3C timeout argument: ${JSON.stringify({script, pageLoad, implicit})}}`);
36  if (util.hasValue(script)) {
37    await this.scriptTimeoutW3C(script);
38  }
39  if (util.hasValue(pageLoad)) {
40    await this.pageLoadTimeoutW3C(pageLoad);
41  }
42  if (util.hasValue(implicit)) {
43    await this.implicitWaitW3C(implicit);
44  }
45};
46
47commands.getTimeouts = async function getTimeouts () { // eslint-disable-line require-await
48  return {
49    command: this.newCommandTimeoutMs,
50    implicit: this.implicitWaitMs,
51  };
52};
53
54// implicit
55commands.implicitWaitW3C = async function implicitWaitW3C (ms) {
56  await this.implicitWait(ms);
57};
58
59commands.implicitWaitMJSONWP = async function implicitWaitMJSONWP (ms) {
60  await this.implicitWait(ms);
61};
62
63commands.implicitWait = async function implicitWait (ms) {
64  await this.setImplicitWait(this.parseTimeoutArgument(ms));
65};
66
67helpers.setImplicitWait = function setImplicitWait (ms) { // eslint-disable-line require-await
68  this.implicitWaitMs = ms;
69  log.debug(`Set implicit wait to ${ms}ms`);
70  if (this.managedDrivers && this.managedDrivers.length) {
71    log.debug('Setting implicit wait on managed drivers');
72    for (let driver of this.managedDrivers) {
73      if (_.isFunction(driver.setImplicitWait)) {
74        driver.setImplicitWait(ms);
75      }
76    }
77  }
78};
79
80// pageLoad
81// eslint-disable-next-line no-unused-vars
82commands.pageLoadTimeoutW3C = async function pageLoadTimeoutW3C (ms) { // eslint-disable-line require-await
83  throw new errors.NotImplementedError('Not implemented yet for pageLoad.');
84};
85
86// eslint-disable-next-line no-unused-vars
87commands.pageLoadTimeoutMJSONWP = async function pageLoadTimeoutMJSONWP (ms) { // eslint-disable-line require-await
88  throw new errors.NotImplementedError('Not implemented yet for pageLoad.');
89};
90
91// script
92// eslint-disable-next-line no-unused-vars
93commands.scriptTimeoutW3C = async function scriptTimeoutW3C (ms) { // eslint-disable-line require-await
94  throw new errors.NotImplementedError('Not implemented yet for script.');
95};
96
97// eslint-disable-next-line no-unused-vars
98commands.scriptTimeoutMJSONWP = async function scriptTimeoutMJSONWP (ms) { // eslint-disable-line require-await
99  throw new errors.NotImplementedError('Not implemented yet for script.');
100};
101
102// command
103commands.newCommandTimeout = async function newCommandTimeout (ms) { // eslint-disable-line require-await
104  this.setNewCommandTimeout(this.parseTimeoutArgument(ms));
105};
106
107helpers.setNewCommandTimeout = function setNewCommandTimeout (ms) {
108  this.newCommandTimeoutMs = ms;
109  log.debug(`Set new command timeout to ${ms}ms`);
110  if (this.managedDrivers && this.managedDrivers.length) {
111    log.debug('Setting new command timeout on managed drivers');
112    for (let driver of this.managedDrivers) {
113      if (_.isFunction(driver.setNewCommandTimeout)) {
114        driver.setNewCommandTimeout(ms);
115      }
116    }
117  }
118};
119
120helpers.clearNewCommandTimeout = function clearNewCommandTimeout () {
121  if (this.noCommandTimer) {
122    clearTimeout(this.noCommandTimer);
123    this.noCommandTimer = null;
124  }
125};
126
127helpers.startNewCommandTimeout = function startNewCommandTimeout () {
128  // make sure there are no rogue timeouts
129  this.clearNewCommandTimeout();
130
131  // if command timeout is 0, it is disabled
132  if (!this.newCommandTimeoutMs) return; // eslint-disable-line curly
133
134  this.noCommandTimer = setTimeout(async () => {
135    log.warn(`Shutting down because we waited ` +
136              `${this.newCommandTimeoutMs / 1000.0} seconds for a command`);
137    const errorMessage = `New Command Timeout of ` +
138              `${this.newCommandTimeoutMs / 1000.0} seconds ` +
139              `expired. Try customizing the timeout using the ` +
140              `'newCommandTimeout' desired capability`;
141    await this.startUnexpectedShutdown(new Error(errorMessage));
142  }, this.newCommandTimeoutMs);
143};
144
145helpers.implicitWaitForCondition = async function implicitWaitForCondition (condFn) {
146  log.debug(`Waiting up to ${this.implicitWaitMs} ms for condition`);
147  let wrappedCondFn = async (...args) => {
148    // reset command timeout
149    this.clearNewCommandTimeout();
150
151    return await condFn(...args);
152  };
153  return await waitForCondition(wrappedCondFn, {
154    waitMs: this.implicitWaitMs, intervalMs: 500, logger: log
155  });
156};
157
158helpers.parseTimeoutArgument = function parseTimeoutArgument (ms) {
159  let duration = parseInt(ms, 10);
160  if (_.isNaN(duration) || duration < MIN_TIMEOUT) {
161    throw new errors.UnknownError(`Invalid timeout value '${ms}'`);
162  }
163  return duration;
164};
165
166Object.assign(extensions, commands, helpers);
167export { commands, helpers };
168export default extensions;
169
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)