highlight_delay_barrier.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. Object.defineProperty(exports, "__esModule", { value: true });
  11. const webdriver_commands_1 = require("./webdriver_commands");
  12. const HIGHLIGHT_COMMAND = [webdriver_commands_1.CommandName.ElementClick, webdriver_commands_1.CommandName.ElementSendKeys, webdriver_commands_1.CommandName.ElementClear];
  13. let clientScripts = require('./client_scripts/highlight.js');
  14. /**
  15. * A barrier that delays forwarding WebDriver commands that can affect the app (ie, clicks or
  16. * sending text) for a fixed amount of time. During the delay, the element that's the target
  17. * of the command will be highlighted by drawing a transparent div on top of it.
  18. */
  19. class HighlightDelayBarrier {
  20. constructor(client, delay) {
  21. this.client = client;
  22. this.delay = delay;
  23. }
  24. isHighlightCommand(command) {
  25. return HIGHLIGHT_COMMAND.indexOf(command.commandName) !== -1;
  26. }
  27. highlightData(top, left, width, height) {
  28. return JSON.stringify({
  29. script: 'return (' + clientScripts.HIGHLIGHT_FN + ').apply(null, arguments);',
  30. args: [top, left, width, height]
  31. });
  32. }
  33. removeHighlightData() {
  34. return JSON.stringify({
  35. script: 'return (' + clientScripts.REMOVE_HIGHLIGHT_FN + ').apply(null, arguments);',
  36. args: []
  37. });
  38. }
  39. // Simple promise-based sleep so we can use async/await
  40. sleep(delay) {
  41. return new Promise((resolve) => {
  42. setTimeout(() => {
  43. resolve();
  44. }, delay);
  45. });
  46. }
  47. onCommand(command) {
  48. return __awaiter(this, void 0, void 0, function* () {
  49. if (!this.isHighlightCommand(command) || !this.delay) {
  50. return;
  51. }
  52. const sessId = command.sessionId;
  53. const el = command.getParam('elementId');
  54. // The W3C spec does have a 'getRect', but the standalone server doesn't support it yet.
  55. const loc = yield this.client.getLocation(sessId, el);
  56. const size = yield this.client.getSize(sessId, el);
  57. // Set the highlight
  58. yield this.client.execute(sessId, this.highlightData(loc['y'], loc['x'], size['width'], size['height']));
  59. // Wait
  60. yield this.sleep(this.delay);
  61. // Clear the highlight
  62. yield this.client.execute(sessId, this.removeHighlightData());
  63. });
  64. }
  65. }
  66. exports.HighlightDelayBarrier = HighlightDelayBarrier;
  67. //# sourceMappingURL=highlight_delay_barrier.js.map