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 | 'use strict';
|
34 |
|
35 | const fs = require('fs');
|
36 |
|
37 | const Bluebird = require('bluebird');
|
38 | const findOpenPort = require('find-open-port');
|
39 | const _ = require('lodash');
|
40 | const debug = require('debug')('testium-core:processes');
|
41 |
|
42 | const spawnServers = require('./spawn-server');
|
43 |
|
44 | const Phantom = require('./processes/phantom');
|
45 | const Proxy = require('./processes/proxy');
|
46 | const App = require('./processes/application');
|
47 | const Selenium = require('./processes/selenium');
|
48 | const ChromeDriver = require('./processes/chromedriver');
|
49 |
|
50 | const CHROME = 'chrome';
|
51 |
|
52 | function isChromeInDocker(browserName) {
|
53 | if (browserName !== CHROME) {
|
54 | return Bluebird.reject();
|
55 | }
|
56 |
|
57 | const fileName = '/.dockerenv';
|
58 | return new Bluebird((resolve, reject) => {
|
59 | fs.stat(fileName, (err, stats) => {
|
60 | return err ? reject(err) : resolve(stats);
|
61 | });
|
62 | });
|
63 | }
|
64 |
|
65 | function mergeChromeOptions(baseOpts, configOpts) {
|
66 | const reg = /^([-\w]+)=?([\.,\/\w]*)/;
|
67 |
|
68 | const objOpts = baseOpts.concat(configOpts).reduce((acc, val) => {
|
69 | const match = reg.exec(val);
|
70 | acc[match[1]] = match[2];
|
71 | return acc;
|
72 | }, {});
|
73 |
|
74 | return Object.keys(objOpts).reduce((acc, key) => {
|
75 | const option = objOpts[key] !== '' ? `${key}=${objOpts[key]}` : key;
|
76 | return acc.concat([option]);
|
77 | }, []);
|
78 | }
|
79 |
|
80 | function unrefAll(procs) {
|
81 |
|
82 | _.each(procs, proc => {
|
83 | proc.rawProcess.unref();
|
84 | });
|
85 | return procs;
|
86 | }
|
87 |
|
88 | function launchAllProcesses(config) {
|
89 | const launchApp = config.getBool('launch', false);
|
90 |
|
91 | function launchWithAppPort(appPort) {
|
92 | config.set('app.port', appPort);
|
93 |
|
94 | const browserName = config.get('browser');
|
95 | config.set('desiredCapabilities.browserName', browserName);
|
96 |
|
97 | const servers = [Proxy];
|
98 |
|
99 | if (launchApp) {
|
100 | debug('Launching app');
|
101 | servers.push(App);
|
102 | } else {
|
103 | debug('Using already running application on port %d', appPort);
|
104 | }
|
105 |
|
106 | const seleniumUrl = config.get('selenium.serverUrl', false);
|
107 | if (!seleniumUrl) {
|
108 | debug('Will launch webdriver server for %j', browserName);
|
109 | servers.push(
|
110 | {
|
111 | phantomjs: Phantom,
|
112 | chrome: ChromeDriver,
|
113 | }[browserName] || Selenium
|
114 | );
|
115 | } else {
|
116 | debug('Using existing selenium server', seleniumUrl);
|
117 | }
|
118 |
|
119 | if (browserName === CHROME) {
|
120 | const chromePath = config.get('chrome.command', null);
|
121 | if (chromePath) {
|
122 | config.set('desiredCapabilities.chromeOptions.binary', chromePath);
|
123 | }
|
124 |
|
125 | let args = [
|
126 | '--disable-application-cache',
|
127 | '--media-cache-size=1',
|
128 | '--disk-cache-size=1',
|
129 | '--disk-cache-dir=/dev/null',
|
130 | '--disable-cache',
|
131 | '--disable-desktop-notifications',
|
132 | ];
|
133 |
|
134 | const headlessCfg = config.get('chrome.headless', !process.env.DISPLAY);
|
135 | if (headlessCfg && headlessCfg !== 'false') args.push('--headless');
|
136 |
|
137 | if (config.has('desiredCapabilities.chromeOptions.args')) {
|
138 | if (
|
139 | config.get('desiredCapabilities.chromeOptions.mergeArgs', false) ===
|
140 | true
|
141 | ) {
|
142 | args = mergeChromeOptions(
|
143 | args,
|
144 | config.get('desiredCapabilities.chromeOptions.args')
|
145 | );
|
146 | } else {
|
147 | args = config.get('desiredCapabilities.chromeOptions.args');
|
148 | }
|
149 | }
|
150 |
|
151 | if (process.getuid() === 0) args.push('--no-sandbox');
|
152 |
|
153 | return isChromeInDocker(browserName)
|
154 | .then(() => {
|
155 | debug('Running in docker env');
|
156 | args.push('--disable-dev-shm-usage');
|
157 | })
|
158 | .catch(() => {
|
159 | debug('Not running in docker env');
|
160 | })
|
161 | .then(() => {
|
162 | config.set('desiredCapabilities.chromeOptions.args', args);
|
163 | return spawnServers(config, servers);
|
164 | })
|
165 | .then(unrefAll);
|
166 | }
|
167 |
|
168 | return spawnServers(config, servers).then(unrefAll);
|
169 | }
|
170 |
|
171 |
|
172 | const appPort = config.get('app.port', launchApp ? 0 : -1);
|
173 | if (appPort === 0) {
|
174 | return findOpenPort().then(launchWithAppPort);
|
175 | }
|
176 | return launchWithAppPort(appPort);
|
177 | }
|
178 | module.exports = launchAllProcesses;
|