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.

utils.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getSessionError = exports.getEnvironmentVars = exports.CustomRequestError = exports.getErrorFromResponseBody = exports.getPrototype = exports.isSuccessfulResponse = exports.startWebDriverSession = void 0;
  7. const lodash_merge_1 = __importDefault(require("lodash.merge"));
  8. const logger_1 = __importDefault(require("@wdio/logger"));
  9. const protocols_1 = require("@wdio/protocols");
  10. const request_1 = __importDefault(require("./request"));
  11. const command_1 = __importDefault(require("./command"));
  12. const log = logger_1.default('webdriver');
  13. const BROWSER_DRIVER_ERRORS = [
  14. 'unknown command: wd/hub/session',
  15. 'HTTP method not allowed',
  16. "'POST /wd/hub/session' was not found.",
  17. 'Command not found'
  18. ];
  19. async function startWebDriverSession(params) {
  20. const [w3cCaps, jsonwpCaps] = params.capabilities && params.capabilities.alwaysMatch
  21. ? [params.capabilities, params.capabilities.alwaysMatch]
  22. : [{ alwaysMatch: params.capabilities, firstMatch: [{}] }, params.capabilities];
  23. const sessionRequest = new request_1.default('POST', '/session', {
  24. capabilities: w3cCaps,
  25. desiredCapabilities: jsonwpCaps
  26. });
  27. let response;
  28. try {
  29. response = await sessionRequest.makeRequest(params);
  30. }
  31. catch (err) {
  32. log.error(err);
  33. const message = exports.getSessionError(err, params);
  34. throw new Error('Failed to create session.\n' + message);
  35. }
  36. const sessionId = response.value.sessionId || response.sessionId;
  37. params.requestedCapabilities = params.capabilities;
  38. params.capabilities = response.value.capabilities || response.value;
  39. return { sessionId, capabilities: params.capabilities };
  40. }
  41. exports.startWebDriverSession = startWebDriverSession;
  42. function isSuccessfulResponse(statusCode, body) {
  43. if (!body || typeof body.value === 'undefined') {
  44. log.debug('request failed due to missing body');
  45. return false;
  46. }
  47. if (body.status === 7 && body.value && body.value.message &&
  48. (body.value.message.toLowerCase().startsWith('no such element') ||
  49. body.value.message === 'An element could not be located on the page using the given search parameters.' ||
  50. body.value.message.toLowerCase().startsWith('unable to find element'))) {
  51. return true;
  52. }
  53. if (body.status && body.status !== 0) {
  54. log.debug(`request failed due to status ${body.status}`);
  55. return false;
  56. }
  57. const hasErrorResponse = body.value && (body.value.error || body.value.stackTrace || body.value.stacktrace);
  58. if (statusCode === 200 && !hasErrorResponse) {
  59. return true;
  60. }
  61. if (statusCode === 404 && body.value && body.value.error === 'no such element') {
  62. return true;
  63. }
  64. if (hasErrorResponse) {
  65. log.debug('request failed due to response error:', body.value.error);
  66. return false;
  67. }
  68. return true;
  69. }
  70. exports.isSuccessfulResponse = isSuccessfulResponse;
  71. function getPrototype({ isW3C, isChrome, isMobile, isSauce, isSeleniumStandalone }) {
  72. const prototype = {};
  73. const ProtocolCommands = lodash_merge_1.default(isMobile
  74. ? lodash_merge_1.default({}, protocols_1.JsonWProtocol, protocols_1.WebDriverProtocol)
  75. : isW3C ? protocols_1.WebDriverProtocol : protocols_1.JsonWProtocol, isMobile ? lodash_merge_1.default({}, protocols_1.MJsonWProtocol, protocols_1.AppiumProtocol) : {}, isChrome ? protocols_1.ChromiumProtocol : {}, isSauce ? protocols_1.SauceLabsProtocol : {}, isSeleniumStandalone ? protocols_1.SeleniumProtocol : {});
  76. for (const [endpoint, methods] of Object.entries(ProtocolCommands)) {
  77. for (const [method, commandData] of Object.entries(methods)) {
  78. prototype[commandData.command] = { value: command_1.default(method, endpoint, commandData, isSeleniumStandalone) };
  79. }
  80. }
  81. return prototype;
  82. }
  83. exports.getPrototype = getPrototype;
  84. function getErrorFromResponseBody(body) {
  85. if (!body) {
  86. return new Error('Response has empty body');
  87. }
  88. if (typeof body === 'string' && body.length) {
  89. return new Error(body);
  90. }
  91. if (typeof body !== 'object' || (!body.value && !body.error)) {
  92. return new Error('unknown error');
  93. }
  94. return new CustomRequestError(body);
  95. }
  96. exports.getErrorFromResponseBody = getErrorFromResponseBody;
  97. class CustomRequestError extends Error {
  98. constructor(body) {
  99. const errorObj = body.value || body;
  100. super(errorObj.message || errorObj.class || 'unknown error');
  101. if (errorObj.error) {
  102. this.name = errorObj.error;
  103. }
  104. else if (errorObj.message && errorObj.message.includes('stale element reference')) {
  105. this.name = 'stale element reference';
  106. }
  107. }
  108. }
  109. exports.CustomRequestError = CustomRequestError;
  110. function getEnvironmentVars({ isW3C, isMobile, isIOS, isAndroid, isChrome, isSauce, isSeleniumStandalone }) {
  111. return {
  112. isW3C: { value: isW3C },
  113. isMobile: { value: isMobile },
  114. isIOS: { value: isIOS },
  115. isAndroid: { value: isAndroid },
  116. isChrome: { value: isChrome },
  117. isSauce: { value: isSauce },
  118. isSeleniumStandalone: { value: isSeleniumStandalone }
  119. };
  120. }
  121. exports.getEnvironmentVars = getEnvironmentVars;
  122. exports.getSessionError = (err, params = {}) => {
  123. if (err.code === 'ECONNREFUSED') {
  124. return `Unable to connect to "${params.protocol}://${params.hostname}:${params.port}${params.path}", make sure browser driver is running on that address.` +
  125. '\nIf you use services like chromedriver see initialiseServices logs above or in wdio.log file as the service might had problems to start the driver.';
  126. }
  127. if (err.message === 'unhandled request') {
  128. return 'The browser driver couldn\'t start the session. Make sure you have set the "path" correctly!';
  129. }
  130. if (!err.message) {
  131. return 'See wdio.* logs for more information.';
  132. }
  133. if (err.message.includes('Whoops! The URL specified routes to this help page.')) {
  134. return "It seems you are running a Selenium Standalone server and point to a wrong path. Please set `path: '/wd/hub'` in your wdio.conf.js!";
  135. }
  136. if (BROWSER_DRIVER_ERRORS.some(m => err && err.message && err.message.includes(m))) {
  137. return "Make sure to set `path: '/'` in your wdio.conf.js!";
  138. }
  139. if (err.message.includes('Bad Request - Invalid Hostname') && err.message.includes('HTTP Error 400')) {
  140. return "Run edge driver on 127.0.0.1 instead of localhost, ex: --host=127.0.0.1, or set `hostname: 'localhost'` in your wdio.conf.js";
  141. }
  142. const w3cCapMessage = '\nMake sure to add vendor prefix like "goog:", "appium:", "moz:", etc to non W3C capabilities.' +
  143. '\nSee more https://www.w3.org/TR/webdriver/#capabilities';
  144. if (err.message.includes('Illegal key values seen in w3c capabilities')) {
  145. return err.message + w3cCapMessage;
  146. }
  147. if (err.message === 'Response has empty body') {
  148. return 'Make sure to connect to valid hostname:port or the port is not in use.' +
  149. '\nIf you use a grid server ' + w3cCapMessage;
  150. }
  151. return err.message;
  152. };