UNPKG

5.07 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _lodash = _interopRequireDefault(require("lodash.zip"));
9
10var _lodash2 = _interopRequireDefault(require("lodash.clonedeep"));
11
12var _utils = require("@wdio/utils");
13
14var _middlewares = require("./middlewares");
15
16var _utils2 = require("./utils");
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
21
22function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
23
24function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
25
26class MultiRemote {
27 constructor() {
28 this.instances = {};
29 }
30
31 async addInstance(browserName, client) {
32 this.instances[browserName] = await client;
33 return this.instances[browserName];
34 }
35
36 modifier(wrapperClient) {
37 const propertiesObject = {};
38 propertiesObject.commandList = {
39 value: wrapperClient.commandList
40 };
41 propertiesObject.options = {
42 value: wrapperClient.options
43 };
44
45 for (const commandName of wrapperClient.commandList) {
46 propertiesObject[commandName] = {
47 value: this.commandWrapper(commandName),
48 configurable: true
49 };
50 }
51
52 propertiesObject['__propertiesObject__'] = {
53 value: propertiesObject
54 };
55 this.baseInstance = new MultiRemoteDriver(this.instances, propertiesObject);
56 const client = Object.create(this.baseInstance, propertiesObject);
57
58 for (const [identifier, instance] of Object.entries(this.instances)) {
59 client[identifier] = instance;
60 }
61
62 return client;
63 }
64
65 static elementWrapper(instances, result, propertiesObject) {
66 const prototype = _objectSpread({}, propertiesObject, {}, (0, _lodash2.default)((0, _utils2.getPrototype)('element')), {
67 scope: 'element'
68 });
69
70 const element = (0, _utils.webdriverMonad)({}, client => {
71 for (const [i, identifier] of Object.entries(Object.keys(instances))) {
72 client[identifier] = result[i];
73 }
74
75 client.instances = Object.keys(instances);
76 delete client.sessionId;
77 return client;
78 }, prototype);
79 return element(this.sessionId, (0, _middlewares.multiremoteHandler)(_utils.wrapCommand));
80 }
81
82 commandWrapper(commandName) {
83 const instances = this.instances;
84 return (0, _utils.wrapCommand)(commandName, async function (...args) {
85 const result = await Promise.all(Object.entries(instances).map(([, instance]) => instance[commandName](...args)));
86
87 if (commandName === '$') {
88 return MultiRemote.elementWrapper(instances, result, this.__propertiesObject__);
89 } else if (commandName === '$$') {
90 const zippedResult = (0, _lodash.default)(...result);
91 return zippedResult.map(singleResult => MultiRemote.elementWrapper(instances, singleResult, this.__propertiesObject__));
92 }
93
94 return result;
95 });
96 }
97
98}
99
100exports.default = MultiRemote;
101
102class MultiRemoteDriver {
103 constructor(instances, propertiesObject) {
104 this.instances = Object.keys(instances);
105 this.isMultiremote = true;
106 this.__propertiesObject__ = propertiesObject;
107 }
108
109 on(...args) {
110 this.instances.forEach(instanceName => this[instanceName].on(...args));
111 }
112
113 once(...args) {
114 this.instances.forEach(instanceName => this[instanceName].once(...args));
115 }
116
117 emit(...args) {
118 this.instances.forEach(instanceName => this[instanceName].emit(...args));
119 }
120
121 eventNames(...args) {
122 this.instances.forEach(instanceName => this[instanceName].eventNames(...args));
123 }
124
125 getMaxListeners() {
126 this.instances.forEach(instanceName => this[instanceName].getMaxListeners());
127 }
128
129 listenerCount(...args) {
130 this.instances.forEach(instanceName => this[instanceName].listenerCount(...args));
131 }
132
133 listeners(...args) {
134 this.instances.forEach(instanceName => this[instanceName].listeners(...args));
135 }
136
137 removeListener(...args) {
138 this.instances.forEach(instanceName => this[instanceName].removeListener(...args));
139 }
140
141 removeAllListeners(...args) {
142 this.instances.forEach(instanceName => this[instanceName].removeAllListeners(...args));
143 }
144
145}
\No newline at end of file