Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. const Accessibility = require('./accessibility');
  2. const Api = require('./api');
  3. const ChromeDriver = require('./chrome-driver');
  4. const DevNull = require('dev-null');
  5. const fs = require('fs');
  6. const path = require('path');
  7. const WebDriver = require('webdriverio');
  8. function Application(options) {
  9. options = options || {};
  10. this.host = options.host || '127.0.0.1';
  11. this.port = parseInt(options.port, 10) || 9515;
  12. this.quitTimeout = parseInt(options.quitTimeout, 10) || 1000;
  13. this.startTimeout = parseInt(options.startTimeout, 10) || 5000;
  14. this.waitTimeout = parseInt(options.waitTimeout, 10) || 5000;
  15. this.connectionRetryCount = parseInt(options.connectionRetryCount, 10) || 10;
  16. this.connectionRetryTimeout =
  17. parseInt(options.connectionRetryTimeout, 10) || 30000;
  18. this.nodePath = options.nodePath || process.execPath;
  19. this.path = options.path;
  20. this.args = options.args || [];
  21. this.chromeDriverArgs = options.chromeDriverArgs || [];
  22. this.env = options.env || {};
  23. this.workingDirectory = options.cwd || process.cwd();
  24. this.debuggerAddress = options.debuggerAddress;
  25. this.chromeDriverLogPath = options.chromeDriverLogPath;
  26. this.webdriverLogPath = options.webdriverLogPath;
  27. this.webdriverOptions = options.webdriverOptions || {};
  28. this.requireName = options.requireName || 'require';
  29. this.api = new Api(this, this.requireName);
  30. this.setupPromiseness();
  31. }
  32. Application.prototype.setupPromiseness = function () {
  33. const self = this;
  34. self.transferPromiseness = function (target, promise) {
  35. self.api.transferPromiseness(target, promise);
  36. };
  37. };
  38. Application.prototype.start = function () {
  39. const self = this;
  40. return self
  41. .exists()
  42. .then(function () {
  43. return self.startChromeDriver();
  44. })
  45. .then(function () {
  46. return self.createClient();
  47. })
  48. .then(function () {
  49. return self.api.initialize();
  50. })
  51. .then(function () {
  52. return self.client.setTimeouts(
  53. self.waitTimeout,
  54. self.waitTimeout,
  55. self.waitTimeout
  56. );
  57. })
  58. .then(function () {
  59. self.running = true;
  60. })
  61. .then(function () {
  62. return self;
  63. });
  64. };
  65. Application.prototype.stop = function () {
  66. const self = this;
  67. if (!self.isRunning()) {
  68. return Promise.reject(Error('Application not running'));
  69. }
  70. return new Promise(function (resolve, reject) {
  71. const endClient = function () {
  72. setTimeout(function () {
  73. self.chromeDriver.stop();
  74. self.running = false;
  75. resolve(self);
  76. }, self.quitTimeout);
  77. };
  78. if (self.api.nodeIntegration) {
  79. self.client.electron.remote.app.quit().then(endClient, reject);
  80. } else {
  81. self.client
  82. .windowByIndex(0)
  83. .then(function () {
  84. self.client.closeWindow();
  85. })
  86. .then(endClient, reject);
  87. }
  88. });
  89. };
  90. Application.prototype.restart = function () {
  91. const self = this;
  92. return self.stop().then(function () {
  93. return self.start();
  94. });
  95. };
  96. Application.prototype.isRunning = function () {
  97. return this.running;
  98. };
  99. Application.prototype.getSettings = function () {
  100. return {
  101. host: this.host,
  102. port: this.port,
  103. quitTimeout: this.quitTimeout,
  104. startTimeout: this.startTimeout,
  105. waitTimeout: this.waitTimeout,
  106. connectionRetryCount: this.connectionRetryCount,
  107. connectionRetryTimeout: this.connectionRetryTimeout,
  108. nodePath: this.nodePath,
  109. path: this.path,
  110. args: this.args,
  111. chromeDriverArgs: this.chromeDriverArgs,
  112. env: this.env,
  113. workingDirectory: this.workingDirectory,
  114. debuggerAddress: this.debuggerAddress,
  115. chromeDriverLogPath: this.chromeDriverLogPath,
  116. webdriverLogPath: this.webdriverLogPath,
  117. webdriverOptions: this.webdriverOptions,
  118. requireName: this.requireName
  119. };
  120. };
  121. Application.prototype.exists = function () {
  122. const self = this;
  123. return new Promise(function (resolve, reject) {
  124. // Binary path is ignored by ChromeDriver if debuggerAddress is set
  125. if (self.debuggerAddress) return resolve();
  126. if (typeof self.path !== 'string') {
  127. return reject(Error('Application path must be a string'));
  128. }
  129. fs.stat(self.path, function (error, stat) {
  130. if (error) return reject(error);
  131. if (stat.isFile()) return resolve();
  132. reject(Error('Application path specified is not a file: ' + self.path));
  133. });
  134. });
  135. };
  136. Application.prototype.startChromeDriver = function () {
  137. this.chromeDriver = new ChromeDriver(
  138. this.host,
  139. this.port,
  140. this.nodePath,
  141. this.startTimeout,
  142. this.workingDirectory,
  143. this.chromeDriverLogPath
  144. );
  145. return this.chromeDriver.start();
  146. };
  147. Application.prototype.createClient = function () {
  148. const self = this;
  149. return new Promise(function (resolve, reject) {
  150. const args = [];
  151. args.push('spectron-path=' + self.path);
  152. self.args.forEach(function (arg, index) {
  153. args.push('spectron-arg' + index + '=' + arg);
  154. });
  155. for (const name in self.env) {
  156. if (Object.prototype.hasOwnProperty.call(self.env, name)) {
  157. args.push('spectron-env-' + name + '=' + self.env[name]);
  158. }
  159. }
  160. self.chromeDriverArgs.forEach(function (arg) {
  161. args.push(arg);
  162. });
  163. const isWin = process.platform === 'win32';
  164. const launcherPath = path.join(
  165. __dirname,
  166. isWin ? 'launcher.bat' : 'launcher.js'
  167. );
  168. if (process.env.APPVEYOR) {
  169. args.push('no-sandbox');
  170. }
  171. const options = {
  172. hostname: self.host,
  173. port: self.port,
  174. waitforTimeout: self.waitTimeout,
  175. connectionRetryCount: self.connectionRetryCount,
  176. connectionRetryTimeout: self.connectionRetryTimeout,
  177. logLevel: 'silent',
  178. capabilities: {
  179. 'goog:chromeOptions': {
  180. binary: launcherPath,
  181. args: args,
  182. debuggerAddress: self.debuggerAddress,
  183. windowTypes: ['app', 'webview']
  184. }
  185. },
  186. logOutput: DevNull()
  187. };
  188. if (self.webdriverLogPath) {
  189. options.outputDir = self.webdriverLogPath;
  190. options.logLevel = 'trace';
  191. }
  192. Object.assign(options, self.webdriverOptions);
  193. self.client = WebDriver.remote(options).then(function (remote) {
  194. self.client = remote;
  195. self.addCommands();
  196. resolve();
  197. }, reject);
  198. });
  199. };
  200. Application.prototype.addCommands = function () {
  201. this.client.addCommand(
  202. 'waitUntilTextExists',
  203. function (selector, text, timeout) {
  204. const self = this;
  205. return self
  206. .waitUntil(async function () {
  207. const elem = await self.$(selector);
  208. const exists = await elem.isExisting();
  209. if (!exists) {
  210. return false;
  211. }
  212. const selectorText = await elem.getText();
  213. return Array.isArray(selectorText)
  214. ? selectorText.some((s) => s.includes(text))
  215. : selectorText.includes(text);
  216. }, timeout)
  217. .then(
  218. function () {},
  219. function (error) {
  220. error.message = 'waitUntilTextExists ' + error.message;
  221. throw error;
  222. }
  223. );
  224. }
  225. );
  226. this.client.addCommand('waitUntilWindowLoaded', function (timeout) {
  227. const self = this;
  228. return self
  229. .waitUntil(function () {
  230. return self.webContents.isLoading().then(function (loading) {
  231. return !loading;
  232. });
  233. }, timeout)
  234. .then(
  235. function () {},
  236. function (error) {
  237. error.message = 'waitUntilWindowLoaded ' + error.message;
  238. throw error;
  239. }
  240. );
  241. });
  242. this.client.addCommand('getWindowCount', function () {
  243. return this.getWindowHandles().then(function (handles) {
  244. return handles.length;
  245. });
  246. });
  247. this.client.addCommand('windowByIndex', function (index) {
  248. const self = this;
  249. return self.getWindowHandles().then(function (handles) {
  250. return self.switchToWindow(handles[index]);
  251. });
  252. });
  253. this.client.addCommand('getSelectedText', function () {
  254. return this.execute(function () {
  255. return window.getSelection().toString();
  256. });
  257. });
  258. this.client.addCommand('getRenderProcessLogs', function () {
  259. return this.getLogs('browser');
  260. });
  261. const self = this;
  262. this.client.addCommand('getMainProcessLogs', function () {
  263. const logs = self.chromeDriver.getLogs();
  264. self.chromeDriver.clearLogs();
  265. return logs;
  266. });
  267. Accessibility.addCommand(this.client, this.requireName);
  268. };
  269. module.exports = Application;