1 | import logger from '@wdio/logger';
|
2 | import WebDriver, { DEFAULTS } from 'webdriver';
|
3 | import { validateConfig } from '@wdio/config';
|
4 | import { wrapCommand } from '@wdio/utils';
|
5 | import MultiRemote from './multiremote.js';
|
6 | import SevereServiceErrorImport from './utils/SevereServiceError.js';
|
7 | import detectBackend from './utils/detectBackend.js';
|
8 | import { getProtocolDriver } from './utils/driver.js';
|
9 | import { WDIO_DEFAULTS, SupportedAutomationProtocols, Key as KeyConstant } from './constants.js';
|
10 | import { getPrototype, addLocatorStrategyHandler, isStub } from './utils/index.js';
|
11 | export * from './types.js';
|
12 | export const Key = KeyConstant;
|
13 | export const SevereServiceError = SevereServiceErrorImport;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export const remote = async function (params, remoteModifier) {
|
28 | logger.setLogLevelsConfig(params.logLevels, params.logLevel);
|
29 | const keysToKeep = Object.keys(process.env.WDIO_WORKER_ID ? params : DEFAULTS);
|
30 | const config = validateConfig(WDIO_DEFAULTS, params, keysToKeep);
|
31 | const modifier = (client, options) => {
|
32 | |
33 |
|
34 |
|
35 |
|
36 | Object.assign(options, Object.entries(config)
|
37 | .reduce((a, [k, v]) => (typeof v === 'undefined' ? a : { ...a, [k]: v }), {}));
|
38 | if (typeof remoteModifier === 'function') {
|
39 | client = remoteModifier(client, options);
|
40 | }
|
41 | return client;
|
42 | };
|
43 | const { Driver, options } = await getProtocolDriver({ ...params, ...config });
|
44 | const prototype = getPrototype('browser');
|
45 | const instance = await Driver.newSession(options, modifier, prototype, wrapCommand);
|
46 | |
47 |
|
48 |
|
49 | if (params.framework && !isStub(params.automationProtocol)) {
|
50 | const origAddCommand = instance.addCommand.bind(instance);
|
51 | instance.addCommand = (name, fn, attachToElement) => (origAddCommand(name, fn, attachToElement));
|
52 | const origOverwriteCommand = instance.overwriteCommand.bind(instance);
|
53 | instance.overwriteCommand = (name, fn, attachToElement) => (origOverwriteCommand(name, fn, attachToElement));
|
54 | }
|
55 | instance.addLocatorStrategy = addLocatorStrategyHandler(instance);
|
56 | return instance;
|
57 | };
|
58 | export const attach = async function (attachOptions) {
|
59 | |
60 |
|
61 |
|
62 | const params = {
|
63 | automationProtocol: SupportedAutomationProtocols.webdriver,
|
64 | ...attachOptions,
|
65 | ...detectBackend(attachOptions.options),
|
66 | requestedCapabilities: attachOptions.requestedCapabilities
|
67 | };
|
68 | const prototype = getPrototype('browser');
|
69 | const { Driver } = await getProtocolDriver(params);
|
70 | const driver = Driver.attachToSession(params, undefined, prototype, wrapCommand);
|
71 | driver.addLocatorStrategy = addLocatorStrategyHandler(driver);
|
72 | return driver;
|
73 | };
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | export const multiremote = async function (params, { automationProtocol } = {}) {
|
94 | const multibrowser = new MultiRemote();
|
95 | const browserNames = Object.keys(params);
|
96 | |
97 |
|
98 |
|
99 | await Promise.all(browserNames.map(async (browserName) => {
|
100 | const instance = await remote(params[browserName]);
|
101 | return multibrowser.addInstance(browserName, instance);
|
102 | }));
|
103 | |
104 |
|
105 |
|
106 | const prototype = getPrototype('browser');
|
107 | const sessionParams = isStub(automationProtocol) ? undefined : {
|
108 | sessionId: '',
|
109 | isW3C: multibrowser.instances[browserNames[0]].isW3C,
|
110 | logLevel: multibrowser.instances[browserNames[0]].options.logLevel
|
111 | };
|
112 | const ProtocolDriver = automationProtocol && isStub(automationProtocol)
|
113 | ? (await import(automationProtocol)).default
|
114 | : WebDriver;
|
115 | const driver = ProtocolDriver.attachToSession(sessionParams, multibrowser.modifier.bind(multibrowser), prototype, wrapCommand);
|
116 | |
117 |
|
118 |
|
119 |
|
120 | if (!isStub(automationProtocol)) {
|
121 | const origAddCommand = driver.addCommand.bind(driver);
|
122 | driver.addCommand = (name, fn, attachToElement) => {
|
123 | return origAddCommand(name, fn, attachToElement, Object.getPrototypeOf(multibrowser.baseInstance), multibrowser.instances);
|
124 | };
|
125 | const origOverwriteCommand = driver.overwriteCommand.bind(driver);
|
126 | driver.overwriteCommand = (name, fn, attachToElement) => {
|
127 | return origOverwriteCommand(name, fn, attachToElement, Object.getPrototypeOf(multibrowser.baseInstance), multibrowser.instances);
|
128 | };
|
129 | }
|
130 | driver.addLocatorStrategy = addLocatorStrategyHandler(driver);
|
131 | return driver;
|
132 | };
|