blockingproxy.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const http = require("http");
  4. const angular_wait_barrier_1 = require("./angular_wait_barrier");
  5. const highlight_delay_barrier_1 = require("./highlight_delay_barrier");
  6. const simple_webdriver_client_1 = require("./simple_webdriver_client");
  7. const webdriver_proxy_1 = require("./webdriver_proxy");
  8. exports.BP_PREFIX = 'bpproxy';
  9. /**
  10. * The stability proxy is an http server responsible for intercepting
  11. * JSON webdriver commands. It keeps track of whether the page under test
  12. * needs to wait for page stability, and initiates a wait if so.
  13. */
  14. class BlockingProxy {
  15. constructor(seleniumAddress, highlightDelay = null) {
  16. this.server = http.createServer(this.requestListener.bind(this));
  17. this.proxy = new webdriver_proxy_1.WebDriverProxy(seleniumAddress);
  18. let client = new simple_webdriver_client_1.SimpleWebDriverClient(seleniumAddress);
  19. this.waitBarrier = new angular_wait_barrier_1.AngularWaitBarrier(client);
  20. this.highlightBarrier = new highlight_delay_barrier_1.HighlightDelayBarrier(client, highlightDelay);
  21. this.proxy.addBarrier(this.waitBarrier);
  22. this.proxy.addBarrier(this.highlightBarrier);
  23. }
  24. /**
  25. * This command is for the proxy server, not to be forwarded to Selenium.
  26. */
  27. static isProxyCommand(commandPath) {
  28. return (commandPath.split('/')[1] === exports.BP_PREFIX);
  29. }
  30. /**
  31. * Turn on WebDriver logging.
  32. *
  33. * @param logDir The directory to create logs in.
  34. */
  35. enableLogging(logDir) {
  36. this.waitBarrier.enableLogging(logDir);
  37. }
  38. /**
  39. * Override the logger instance. Only used for testing.
  40. */
  41. setLogger(logger) {
  42. this.waitBarrier.setLogger(logger);
  43. }
  44. /**
  45. * Change the parameters used by the wait function.
  46. */
  47. setWaitParams(rootEl) {
  48. this.waitBarrier.setRootSelector(rootEl);
  49. }
  50. handleProxyCommand(message, data, response) {
  51. let command = message.url.split('/')[2];
  52. switch (command) {
  53. case 'waitEnabled':
  54. if (message.method === 'GET') {
  55. response.writeHead(200);
  56. response.write(JSON.stringify({ value: this.waitBarrier.enabled }));
  57. response.end();
  58. }
  59. else if (message.method === 'POST') {
  60. response.writeHead(200);
  61. this.waitBarrier.enabled = JSON.parse(data).value;
  62. response.end();
  63. }
  64. else {
  65. response.writeHead(405);
  66. response.write('Invalid method');
  67. response.end();
  68. }
  69. break;
  70. case 'waitParams':
  71. if (message.method === 'GET') {
  72. response.writeHead(200);
  73. response.write(JSON.stringify({ rootSelector: this.waitBarrier.rootSelector }));
  74. response.end();
  75. }
  76. else if (message.method === 'POST') {
  77. response.writeHead(200);
  78. this.waitBarrier.rootSelector = JSON.parse(data).rootSelector;
  79. response.end();
  80. }
  81. else {
  82. response.writeHead(405);
  83. response.write('Invalid method');
  84. response.end();
  85. }
  86. break;
  87. default:
  88. response.writeHead(404);
  89. response.write('Unknown stabilizer proxy command');
  90. response.end();
  91. }
  92. }
  93. requestListener(originalRequest, response) {
  94. if (BlockingProxy.isProxyCommand(originalRequest.url)) {
  95. let commandData = '';
  96. originalRequest.on('data', (d) => {
  97. commandData += d;
  98. });
  99. originalRequest.on('end', () => {
  100. this.handleProxyCommand(originalRequest, commandData, response);
  101. });
  102. return;
  103. }
  104. // OK to ignore the promise returned by this.
  105. this.proxy.handleRequest(originalRequest, response);
  106. }
  107. listen(port) {
  108. this.server.listen(port);
  109. let actualPort = this.server.address().port;
  110. return actualPort;
  111. }
  112. quit() {
  113. return new Promise((resolve) => {
  114. this.server.close(resolve);
  115. });
  116. }
  117. }
  118. exports.BlockingProxy = BlockingProxy;
  119. //# sourceMappingURL=blockingproxy.js.map