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

namespace-test.js

Source: namespace-test.js Github

copy
1/*global bean:true, buster:true, Syn:true, assert:true, defer:true, features:true, globalSetUp:true, globalTearDown:true*/
2
3buster.testCase('namespaces', {
4    'setUp': globalSetUp
5  , 'tearDown': globalTearDown
6
7  , 'should be able to name handlers': {
8        'setUp': function () {
9          var self = this
10          this.runTest = function (done, regFn) {
11            var el1     = self.byId('foo')
12              , trigger = self.trigger()
13              , spy     = self.spy()
14
15            trigger.after(function () {
16              assert.equals(spy.callCount, 1, 'triggered click event')
17              done()
18            })
19
20            regFn(el1, trigger.wrap(spy))
21
22            Syn.click(el1)
23          }
24        }
25      , 'on()': function (done) {
26          this.runTest(done, function (el1, wrappedSpy) {
27            bean.on(el1, 'click.fat', wrappedSpy)
28          })
29        }
30      , 'add()': function (done) {
31          this.runTest(done, function (el1, wrappedSpy) {
32            bean.add(el1, 'click.fat', wrappedSpy)
33          })
34        }
35    }
36
37  , 'should be able to add multiple handlers under the same namespace to the same element': {
38        'setUp': function () {
39          var self = this
40          this.runTest = function (done, regFn) {
41            var el1     = self.byId('foo')
42              , trigger = self.trigger()
43              , spy1    = self.spy()
44              , spy2    = self.spy()
45
46            trigger.after(function () {
47              assert.equals(spy1.callCount, 1, 'triggered click event')
48              assert.equals(spy2.callCount, 1, 'triggered click event')
49              done()
50            })
51
52            regFn(el1, trigger.wrap(spy1), trigger.wrap(spy2))
53
54            Syn.click(el1)
55          }
56        }
57      , 'on()': function (done) {
58          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2) {
59            bean.on(el1, 'click.fat', wrappedSpy1)
60            bean.on(el1, 'click.fat', wrappedSpy2)
61          })
62        }
63      , 'add()': function (done) {
64          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2) {
65            bean.add(el1, 'click.fat', wrappedSpy1)
66            bean.add(el1, 'click.fat', wrappedSpy2)
67          })
68        }
69    }
70
71  , 'should be able to fire an event without handlers': function () {
72      var el1 = this.byId('foo')
73
74      bean.fire(el1, 'click.fat')
75
76      assert(true, 'fire namespaced event with no handlers (no exception)')
77    }
78
79  , 'should be able to target namespaced event handlers with fire': {
80        'setUp': function () {
81          var self = this
82          this.runTest = function (done, regFn) {
83            var el1     = self.byId('foo')
84              , trigger = self.trigger()
85              , spy1    = self.spy()
86              , spy2    = self.spy()
87
88            trigger.after(function () {
89              assert.equals(spy1.callCount, 1, 'triggered click event (namespaced)')
90              assert.equals(spy2.callCount, 0, 'should not trigger click event (plain)')
91              done()
92            })
93
94            regFn(el1, trigger.wrap(spy1), trigger.wrap(spy2))
95
96            bean.fire(el1, 'click.fat')
97          }
98        }
99      , 'on()': function (done) {
100          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2) {
101            bean.on(el1, 'click.fat', wrappedSpy1)
102            bean.on(el1, 'click', wrappedSpy2)
103          })
104        }
105      , 'add()': function (done) {
106          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2) {
107            bean.add(el1, 'click.fat', wrappedSpy1)
108            bean.add(el1, 'click', wrappedSpy2)
109          })
110        }
111    }
112
113    // changed in 0.5 so this doesn't fire, namespaces need to match
114  , 'should not be able to target multiple namespaced event handlers with fire': {
115        'setUp': function () {
116          var self = this
117          this.runTest = function (done, regFn) {
118            var el1     = self.byId('foo')
119              , spy1    = self.spy()
120              , spy2    = self.spy()
121              , spy3    = self.spy()
122
123            regFn(el1, spy1, spy2, spy3)
124
125            bean.fire(el1, 'click.fat.ded')
126
127            defer(function () {
128              assert.equals(spy1.callCount, 0, 'should not trigger click event (namespaced)')
129              assert.equals(spy2.callCount, 0, 'should not trigger click event (namespaced)')
130              assert.equals(spy3.callCount, 0, 'should not trigger click event (plain)')
131              done()
132            })
133          }
134        }
135      , 'on()': function (done) {
136          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2, wrappedSpy3) {
137            bean.on(el1, 'click.fat', wrappedSpy1)
138            bean.on(el1, 'click.ded', wrappedSpy2)
139            bean.on(el1, 'click', wrappedSpy3)
140          })
141        }
142      , 'add()': function (done) {
143          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2, wrappedSpy3) {
144            bean.add(el1, 'click.fat', wrappedSpy1)
145            bean.add(el1, 'click.ded', wrappedSpy2)
146            bean.add(el1, 'click', wrappedSpy3)
147          })
148        }
149    }
150
151  , 'should be able to remove handlers based on name': {
152        'setUp': function () {
153          var self = this
154          this.runTest = function (done, regFn) {
155            var el1     = self.byId('foo')
156              , trigger = self.trigger()
157              , spy1    = self.spy()
158              , spy2    = self.spy()
159
160            trigger.after(function () {
161              assert.equals(spy1.callCount, 0, 'should not trigger click event (namespaced)')
162              assert.equals(spy2.callCount, 1, 'triggered click event (plain)')
163              done()
164            })
165
166            regFn(el1, trigger.wrap(spy1), trigger.wrap(spy2))
167
168            bean.remove(el1, 'click.ded')
169
170            Syn.click(el1)
171          }
172        }
173      , 'on()': function (done) {
174          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2) {
175            bean.on(el1, 'click.ded', wrappedSpy1)
176            bean.on(el1, 'click', wrappedSpy2)
177          })
178        }
179      , 'add()': function (done) {
180          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2) {
181            bean.add(el1, 'click.ded', wrappedSpy1)
182            bean.add(el1, 'click', wrappedSpy2)
183          })
184        }
185    }
186
187    // changed in 0.5 so this doesn't remove, namespaces need to match
188  , 'should not be able to remove multiple handlers based on name': {
189        'setUp': function () {
190          var self = this
191          this.runTest = function (done, regFn) {
192            var el1     = self.byId('foo')
193              , trigger = self.trigger()
194              , spy1    = self.spy()
195              , spy2    = self.spy()
196              , spy3    = self.spy()
197
198            trigger.after(function () {
199              assert.equals(spy1.callCount, 1, 'triggered click event (namespaced)')
200              assert.equals(spy2.callCount, 1, 'triggered click event (namespaced)')
201              assert.equals(spy3.callCount, 1, 'triggered click event (plain)')
202              done()
203            })
204
205            regFn(el1, trigger.wrap(spy1), trigger.wrap(spy2), trigger.wrap(spy3))
206
207            bean.remove(el1, 'click.ded.fat')
208
209            Syn.click(el1)
210          }
211        }
212      , 'on()': function (done) {
213          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2, wrappedSpy3) {
214            bean.on(el1, 'click.fat', wrappedSpy1)
215            bean.on(el1, 'click.ded', wrappedSpy2)
216            bean.on(el1, 'click', wrappedSpy3)
217          })
218        }
219      , 'add()': function (done) {
220          this.runTest(done, function (el1, wrappedSpy1, wrappedSpy2, wrappedSpy3) {
221            bean.add(el1, 'click.fat', wrappedSpy1)
222            bean.add(el1, 'click.ded', wrappedSpy2)
223            bean.add(el1, 'click', wrappedSpy3)
224          })
225        }
226    }
227
228  , 'should be able to add multiple custom events to a single handler and call them individually': {
229        'setUp': function () {
230          var self = this
231          this.runTest = function (done, regFn) {
232            var el1     = self.byId('foo')
233              , trigger = self.trigger()
234              , spy     = self.spy()
235
236            trigger.after(function () {
237              assert.equals(spy.callCount, 2, 'triggered custom event')
238              assert.equals(spy.firstCall.args[0], '1', 'expected array argument')
239              assert.equals(spy.secondCall.args[0], '2', 'expected array argument')
240              done()
241            })
242
243            regFn(el1, trigger.wrap(spy))
244
245            bean.fire(el1, 'fat.test1', ['1'])
246            bean.fire(el1, 'fat.test2', ['2'])
247          }
248        }
249      , 'on()': function (done) {
250          this.runTest(done, function (el1, wrappedSpy) {
251            bean.on(el1, 'fat.test1 fat.test2', wrappedSpy)
252          })
253        }
254      , 'add()': function (done) {
255          this.runTest(done, function (el1, wrappedSpy) {
256            bean.add(el1, 'fat.test1 fat.test2', wrappedSpy)
257          })
258        }
259    }
260    
261  , 'should be able to fire an event if the fired namespace is within the event namespace range': {
262        'setUp': function () {
263          var self = this
264          this.runTest = function (done, regFn) {
265            var el1     = self.byId('foo')
266              , trigger = self.trigger()
267              , spy     = self.spy()
268
269            trigger.after(function () {
270              assert.equals(spy.callCount, 4, 'triggered custom event')
271              assert.equals(spy.firstCall.args[0], '1', 'expected array argument')
272              assert.equals(spy.secondCall.args[0], '2', 'expected array argument')
273              assert.equals(spy.thirdCall.args[0], '3', 'expected array argument')
274              assert.equals(spy.lastCall.args[0], '3', 'expected array argument')
275              done()
276            })
277
278            regFn(el1, trigger.wrap(spy))
279
280            bean.fire(el1, 'fat.test1', ['1'])
281            bean.fire(el1, 'fat.test2', ['2'])
282            bean.fire(el1, 'fat.foo', ['3'])
283          }
284        }
285      , 'on()': function (done) {
286          this.runTest(done, function (el1, wrappedSpy) {
287            bean.on(el1, 'fat.test1.foo fat.test2.foo', wrappedSpy)
288          })
289        }
290      , 'add()': function (done) {
291          this.runTest(done, function (el1, wrappedSpy) {
292            bean.add(el1, 'fat.test1.foo fat.test2.foo', wrappedSpy)
293          })
294        }
295    }
296
297  , 'should be able to fire multiple events and fire them regardless of the order of the namespaces': {
298        'setUp': function () {
299          var self = this
300          this.runTest = function (done, regFn) {
301            var el1     = self.byId('foo')
302              , trigger = self.trigger()
303              , spy     = self.spy()
304
305            trigger.after(function () {
306              assert.equals(spy.callCount, 4, 'triggered custom event')
307              assert.equals(spy.firstCall.args[0], '1', 'expected array argument')
308              assert.equals(spy.secondCall.args[0], '1', 'expected array argument')
309              assert.equals(spy.thirdCall.args[0], '2', 'expected array argument')
310              assert.equals(spy.lastCall.args[0], '2', 'expected array argument')
311              done()
312            })
313
314            regFn(el1, trigger.wrap(spy))
315
316            bean.fire(el1, 'fat.test.foo', ['1'])
317            bean.fire(el1, 'fat.foo.test', ['2'])
318          }
319        }
320      , 'on()': function (done) {
321          this.runTest(done, function (el1, wrappedSpy) {
322            bean.on(el1, 'fat.test.foo fat.foo.test', wrappedSpy)
323          })
324        }
325      , 'add()': function (done) {
326          this.runTest(done, function (el1, wrappedSpy) {
327            bean.add(el1, 'fat.test.foo fat.foo.test', wrappedSpy)
328          })
329        }
330    }
331    
332  , 'should only fire an event if the fired namespaces is within the event namespace or if the event namespace is within the fired namespace': {
333        'setUp': function () {
334          var self = this
335          this.runTest = function (done, regFn) {
336            var el1     = self.byId('foo')
337              , trigger = self.trigger()
338              , spy     = self.spy()
339
340            trigger.after(function () {
341              assert.equals(spy.callCount, 5, 'triggered custom event')
342              assert.equals(spy.firstCall.args[0], '1', 'expected array argument')
343              assert.equals(spy.secondCall.args[0], '1', 'expected array argument')
344              assert.equals(spy.thirdCall.args[0], '2', 'expected array argument')
345              assert.equals(spy.getCall(3).args[0], '2', 'expected array argument')
346              assert.equals(spy.getCall(4).args[0], '3', 'expected array argument')
347              done()
348            })
349
350            regFn(el1, trigger.wrap(spy))
351
352            bean.fire(el1, 'fat.test.foo', ['1'])
353            bean.fire(el1, 'fat.foo.test', ['2'])
354            bean.fire(el1, 'fat.test.ded', ['3'])
355          }
356        }
357      , 'on()': function (done) {
358          this.runTest(done, function (el1, wrappedSpy) {
359            bean.on(el1, 'fat.test.foo.ded fat.foo.test fat.ded', wrappedSpy)
360          })
361        }
362      , 'add()': function (done) {
363          this.runTest(done, function (el1, wrappedSpy) {
364            bean.add(el1, 'fat.test.foo.ded fat.foo.test fat.ded', wrappedSpy)
365          })
366        }
367    }
368})
Full Screen

wrap-method.js

Source: wrap-method.js Github

copy
1"use strict";
2
3var getPropertyDescriptor = require("./get-property-descriptor");
4var valueToString = require("./value-to-string");
5
6var hasOwn = Object.prototype.hasOwnProperty;
7
8function isFunction(obj) {
9    return typeof obj === "function" || !!(obj && obj.constructor && obj.call && obj.apply);
10}
11
12function mirrorProperties(target, source) {
13    for (var prop in source) {
14        if (!hasOwn.call(target, prop)) {
15            target[prop] = source[prop];
16        }
17    }
18}
19
20// Cheap way to detect if we have ES5 support.
21var hasES5Support = "keys" in Object;
22
23module.exports = function wrapMethod(object, property, method) {
24    if (!object) {
25        throw new TypeError("Should wrap property of object");
26    }
27
28    if (typeof method !== "function" && typeof method !== "object") {
29        throw new TypeError("Method wrapper should be a function or a property descriptor");
30    }
31
32    function checkWrappedMethod(wrappedMethod) {
33        var error;
34
35        if (!isFunction(wrappedMethod)) {
36            error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
37                                valueToString(property) + " as function");
38        } else if (wrappedMethod.restore && wrappedMethod.restore.sinon) {
39            error = new TypeError("Attempted to wrap " + valueToString(property) + " which is already wrapped");
40        } else if (wrappedMethod.calledBefore) {
41            var verb = wrappedMethod.returns ? "stubbed" : "spied on";
42            error = new TypeError("Attempted to wrap " + valueToString(property) + " which is already " + verb);
43        }
44
45        if (error) {
46            if (wrappedMethod && wrappedMethod.stackTrace) {
47                error.stack += "\n--------------\n" + wrappedMethod.stackTrace;
48            }
49            throw error;
50        }
51    }
52
53    var error, wrappedMethod, i;
54
55    function simplePropertyAssignment() {
56        wrappedMethod = object[property];
57        checkWrappedMethod(wrappedMethod);
58        object[property] = method;
59        method.displayName = property;
60    }
61
62    // Firefox has a problem when using hasOwn.call on objects from other frames.
63    var owned = object.hasOwnProperty ? object.hasOwnProperty(property) : hasOwn.call(object, property);
64
65    if (hasES5Support) {
66        var methodDesc = (typeof method === "function") ? {value: method} : method;
67        var wrappedMethodDesc = getPropertyDescriptor(object, property);
68
69        if (!wrappedMethodDesc) {
70            error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
71                                property + " as function");
72        } else if (wrappedMethodDesc.restore && wrappedMethodDesc.restore.sinon) {
73            error = new TypeError("Attempted to wrap " + property + " which is already wrapped");
74        }
75        if (error) {
76            if (wrappedMethodDesc && wrappedMethodDesc.stackTrace) {
77                error.stack += "\n--------------\n" + wrappedMethodDesc.stackTrace;
78            }
79            throw error;
80        }
81
82        var types = Object.keys(methodDesc);
83        for (i = 0; i < types.length; i++) {
84            wrappedMethod = wrappedMethodDesc[types[i]];
85            checkWrappedMethod(wrappedMethod);
86        }
87
88        mirrorProperties(methodDesc, wrappedMethodDesc);
89        for (i = 0; i < types.length; i++) {
90            mirrorProperties(methodDesc[types[i]], wrappedMethodDesc[types[i]]);
91        }
92        Object.defineProperty(object, property, methodDesc);
93
94        // catch failing assignment
95        // this is the converse of the check in `.restore` below
96        if ( typeof method === "function" && object[property] !== method ) {
97            // correct any wrongdoings caused by the defineProperty call above,
98            // such as adding new items (if object was a Storage object)
99            delete object[property];
100            simplePropertyAssignment();
101        }
102    } else {
103        simplePropertyAssignment();
104    }
105
106    method.displayName = property;
107
108    // Set up a stack trace which can be used later to find what line of
109    // code the original method was created on.
110    method.stackTrace = (new Error("Stack Trace for original")).stack;
111
112    method.restore = function () {
113        // For prototype properties try to reset by delete first.
114        // If this fails (ex: localStorage on mobile safari) then force a reset
115        // via direct assignment.
116        if (!owned) {
117            // In some cases `delete` may throw an error
118            try {
119                delete object[property];
120            } catch (e) {} // eslint-disable-line no-empty
121            // For native code functions `delete` fails without throwing an error
122            // on Chrome < 43, PhantomJS, etc.
123        } else if (hasES5Support) {
124            Object.defineProperty(object, property, wrappedMethodDesc);
125        }
126
127        if (hasES5Support) {
128            var descriptor = getPropertyDescriptor(object, property);
129            if (descriptor && descriptor.value === method) {
130                object[property] = wrappedMethod;
131            }
132        }
133        else {
134        // Use strict equality comparison to check failures then force a reset
135        // via direct assignment.
136            if (object[property] === method) {
137                object[property] = wrappedMethod;
138            }
139        }
140    };
141
142    method.wrappedMethod = wrappedMethod;
143
144    method.restore.sinon = true;
145
146    if (!hasES5Support) {
147        mirrorProperties(method, wrappedMethod);
148    }
149
150    return method;
151};
152
Full Screen

wrapped-operation.js

Source: wrapped-operation.js Github

copy
1if (typeof ot === 'undefined') {
2  // Export for browsers
3  var ot = {};
4}
5
6ot.WrappedOperation = (function (global) {
7  'use strict';
8
9  // A WrappedOperation contains an operation and corresponing metadata.
10  function WrappedOperation (operation, meta) {
11    this.wrapped = operation;
12    this.meta    = meta;
13  }
14
15  WrappedOperation.prototype.apply = function () {
16    return this.wrapped.apply.apply(this.wrapped, arguments);
17  };
18
19  WrappedOperation.prototype.invert = function () {
20    var meta = this.meta;
21    return new WrappedOperation(
22      this.wrapped.invert.apply(this.wrapped, arguments),
23      meta && typeof meta === 'object' && typeof meta.invert === 'function' ?
24        meta.invert.apply(meta, arguments) : meta
25    );
26  };
27
28  // Copy all properties from source to target.
29  function copy (source, target) {
30    for (var key in source) {
31      if (source.hasOwnProperty(key)) {
32        target[key] = source[key];
33      }
34    }
35  }
36
37  function composeMeta (a, b) {
38    if (a && typeof a === 'object') {
39      if (typeof a.compose === 'function') { return a.compose(b); }
40      var meta = {};
41      copy(a, meta);
42      copy(b, meta);
43      return meta;
44    }
45    return b;
46  }
47
48  WrappedOperation.prototype.compose = function (other) {
49    return new WrappedOperation(
50      this.wrapped.compose(other.wrapped),
51      composeMeta(this.meta, other.meta)
52    );
53  };
54
55  function transformMeta (meta, operation) {
56    if (meta && typeof meta === 'object') {
57      if (typeof meta.transform === 'function') {
58        return meta.transform(operation);
59      }
60    }
61    return meta;
62  }
63
64  WrappedOperation.transform = function (a, b) {
65    var transform = a.wrapped.constructor.transform;
66    var pair = transform(a.wrapped, b.wrapped);
67    return [
68      new WrappedOperation(pair[0], transformMeta(a.meta, b.wrapped)),
69      new WrappedOperation(pair[1], transformMeta(b.meta, a.wrapped))
70    ];
71  };
72
73  return WrappedOperation;
74
75}(this));
76
77// Export for CommonJS
78if (typeof module === 'object') {
79  module.exports = ot.WrappedOperation;
80}
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)