-
Notifications
You must be signed in to change notification settings - Fork 14
/
service.ts
173 lines (148 loc) · 5.63 KB
/
service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Capabilities, Options, Services } from '@wdio/types';
import { Browser } from 'webdriverio';
import { isCI } from 'ci-info';
import { log } from './utils';
function getMacExecutableName(appName: string) {
// https://github.com/electron-userland/electron-builder/blob/master/packages/app-builder-lib/src/macPackager.ts#L390
if (appName.endsWith(' Helper')) {
return appName.replace(' Helper', '');
}
return appName;
}
function getBinaryPath(distPath: string, appName: string) {
const SupportedPlatform = {
darwin: 'darwin',
linux: 'linux',
win32: 'win32',
};
const { platform } = process;
if (!Object.values(SupportedPlatform).includes(platform)) {
throw new Error(`Unsupported platform: ${platform}`);
}
const pathMap = {
darwin: `mac/${appName}.app/Contents/MacOS/${getMacExecutableName(appName)}`,
linux: `linux-unpacked/${appName}`,
win32: `win-unpacked/${appName}.exe`,
};
const electronPath = pathMap[platform as keyof typeof SupportedPlatform];
return `${distPath}/${electronPath}`;
}
async function callApi(bridgePropName: string, args: unknown[], done: (result: unknown) => void) {
if (window.wdioElectron === undefined) {
throw new Error(`ContextBridge not available for invocation of "${bridgePropName}" API`);
}
if (window.wdioElectron[bridgePropName] === undefined) {
throw new Error(`"${bridgePropName}" API not found on ContextBridge`);
}
done(await window.wdioElectron[bridgePropName].invoke(...args));
}
type ElectronWorkerOptions = {
appPath?: string;
appName?: string;
binaryPath?: string;
customApiBrowserCommand?: string;
appArgs?: string[];
};
type ApiCommand = { name: string; bridgeProp: string };
type WebDriverClient = Browser<'async'>;
type WebdriverClientFunc = (this: WebDriverClient, ...args: unknown[]) => Promise<unknown>;
export default class ElectronWorkerService implements Services.ServiceInstance {
constructor(options: Services.ServiceOption) {
const apiCommands = [
{ name: '', bridgeProp: 'custom' },
{ name: 'electronApp', bridgeProp: 'app' },
{ name: 'electronMainProcess', bridgeProp: 'mainProcess' },
{ name: 'electronBrowserWindow', bridgeProp: 'browserWindow' },
];
const {
appPath,
appName,
appArgs,
binaryPath,
customApiBrowserCommand = 'electronAPI',
} = options as ElectronWorkerOptions;
const validPathOpts = binaryPath !== undefined || (appPath !== undefined && appName !== undefined);
if (!validPathOpts) {
const invalidPathOptsError = new Error('You must provide appPath and appName values, or a binaryPath value');
log.error(invalidPathOptsError);
throw invalidPathOptsError;
}
const customCommandCollision = apiCommands.find(
(command) => command.name === customApiBrowserCommand,
) as ApiCommand;
if (customCommandCollision) {
const customCommandCollisionError = new Error(
`The command "${customCommandCollision.name}" is reserved, please provide a different value for customApiBrowserCommand`,
);
log.error(customCommandCollisionError);
throw customCommandCollisionError;
} else {
apiCommands[0].name = customApiBrowserCommand;
}
this.options = {
appPath,
appName,
appArgs,
binaryPath,
};
this.apiCommands = apiCommands;
}
public options;
public apiCommands;
beforeSession(_config: Omit<Options.Testrunner, 'capabilities'>, capabilities: Capabilities.Capabilities): void {
const chromeArgs = [];
if (isCI) {
chromeArgs.push('window-size=1280,800');
chromeArgs.push('blink-settings=imagesEnabled=false');
chromeArgs.push('enable-automation');
chromeArgs.push('disable-infobars');
chromeArgs.push('disable-extensions');
if (process.platform !== 'win32') {
chromeArgs.push('no-sandbox');
chromeArgs.push('disable-gpu');
chromeArgs.push('disable-dev-shm-usage');
chromeArgs.push('disable-setuid-sandbox');
}
}
const { appPath, appName, appArgs, binaryPath } = this.options;
if (appArgs) {
chromeArgs.push(...appArgs);
}
const chromeOptions = {
binary: binaryPath || getBinaryPath(appPath as string, appName as string),
args: chromeArgs,
windowTypes: ['app', 'webview'],
};
const isMultiremote =
typeof capabilities === 'object' &&
!Array.isArray(capabilities) &&
Object.keys(capabilities).length > 0 &&
Object.values(capabilities).every((cap) => typeof cap === 'object');
const isElectron = (cap: Capabilities.Capabilities) => cap?.browserName?.toLowerCase() === 'electron';
if (isMultiremote) {
log.debug('setting up multiremote');
Object.values(capabilities).forEach((cap: { capabilities: Capabilities.Capabilities }) => {
if (isElectron(cap.capabilities)) {
cap.capabilities.browserName = 'chrome';
cap.capabilities['goog:chromeOptions'] = chromeOptions;
}
});
} else {
capabilities.browserName = 'chrome';
capabilities['goog:chromeOptions'] = chromeOptions;
}
log.debug('setting browser capabilities', capabilities);
}
before(_capabilities: Capabilities.Capabilities, _specs: string[], browser: WebDriverClient): void {
this.apiCommands.forEach(({ name, bridgeProp }) => {
log.debug('adding api command for ', name);
browser.addCommand(name, async (...args: unknown[]) => {
try {
return await (browser.executeAsync as WebdriverClientFunc)(callApi, bridgeProp, args);
} catch (e) {
throw new Error(`${name} error: ${(e as Error).message}`);
}
});
});
}
}