Home Reference Source

lib/input/Touchscreen.js

  1. const Keyboard = require('./Keyboard')
  2.  
  3. class Touchscreen {
  4. /**
  5. * @param {Chrome|CRIConnection|CDPSession|Object} client
  6. * @param {?Keyboard} [keyboard]
  7. */
  8. constructor (client, keyboard) {
  9. this._client = client
  10. this._keyboard = keyboard != null ? keyboard : new Keyboard(client)
  11. }
  12.  
  13. /**
  14. * @param {number} x
  15. * @param {number} y
  16. */
  17. async tap (x, y) {
  18. // Touches appear to be lost during the first frame after navigation.
  19. // This waits a frame before sending the tap.
  20. // @see https://crbug.com/613219
  21. await this._client.send('Runtime.evaluate', {
  22. expression:
  23. 'new Promise(x => requestAnimationFrame(() => requestAnimationFrame(x)))',
  24. awaitPromise: true
  25. })
  26.  
  27. const touchPoints = [{ x: Math.round(x), y: Math.round(y) }]
  28. await this._client.send('Input.dispatchTouchEvent', {
  29. type: 'touchStart',
  30. touchPoints,
  31. modifiers: this._keyboard._modifiers
  32. })
  33. await this._client.send('Input.dispatchTouchEvent', {
  34. type: 'touchEnd',
  35. touchPoints: [],
  36. modifiers: this._keyboard._modifiers
  37. })
  38. }
  39. }
  40.  
  41. module.exports = Touchscreen