playwright.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/node
  2. "use strict";
  3. const yargs = require('yargs');
  4. const { v4 : uuidv4 } = require('uuid');
  5. const express = require('express');
  6. const { chromium, firefox, webkit, devices } = require('playwright');
  7. const fs = require('fs');
  8. // Defines our interface
  9. let rawdata = fs.readFileSync('api.json');
  10. let spec = JSON.parse(rawdata);
  11. //TODO support device commands
  12. const argv = yargs
  13. .option('debug', {
  14. alias: 'd',
  15. description: 'Print additional debugging messages',
  16. type: 'boolean',
  17. })
  18. .option('port', {
  19. alias: 'p',
  20. description: 'Run on specified port',
  21. type: 'number',
  22. })
  23. .help()
  24. .alias('help', 'h')
  25. .argv;
  26. const app = express();
  27. const port = argv.port || 6969;
  28. var objects = {};
  29. var browsers = { 'firefox' : firefox, 'chrome' : chromium, 'webkit' : webkit };
  30. app.use(express.json())
  31. app.post('/session', async (req, res) => {
  32. var payload = req.body;
  33. var type = payload.type;
  34. var args = payload.args || [];
  35. var result;
  36. if ( type && browsers[type] ) {
  37. try {
  38. var browser = await firefox.launch(...args);
  39. objects[browser._guid] = browser;
  40. result = { error : false, message : browser };
  41. } catch (e) {
  42. result = { error : true, message : e.message};
  43. }
  44. } else {
  45. result = { error : true, message : "Please select a supported browser" };
  46. }
  47. res.json(result);
  48. });
  49. app.post('/command', async (req, res) => {
  50. var payload = req.body;
  51. var type = payload.type;
  52. var object = payload.object;
  53. var command = payload.command;
  54. var args = payload.args || [];
  55. var result = {};
  56. if (argv.debug) {
  57. console.log(type,object,command,args);
  58. }
  59. // XXX this would be cleaner if the mouse() and keyboard() methods just returned a Mouse and Keyboard object
  60. var subject = objects[object];
  61. if (subject) {
  62. if (type == 'Mouse') {
  63. subject = objects[object].mouse;
  64. } else if (type == 'Keyboard' ) {
  65. subject = objects[object].keyboard;
  66. }
  67. }
  68. if (subject && spec[type] && spec[type].members[command]) {
  69. try {
  70. const res = await subject[command](...args);
  71. result = { error : false, message : res };
  72. if (Array.isArray(res)) {
  73. for (var r of res) {
  74. objects[r._guid] = r;
  75. }
  76. }
  77. // XXX videos are special, we have to magic up a guid etc for them
  78. if (command == 'video' && res) {
  79. res._guid = 'Video@' + uuidv4();
  80. res._type = 'Video';
  81. }
  82. if (res && res._guid) {
  83. objects[res._guid] = res;
  84. }
  85. } catch (e) {
  86. result = { error : true, message : e.message };
  87. }
  88. } else {
  89. result = { error : true, message : "No such object, or " + command + " is not a globally recognized command for puppeteer" };
  90. }
  91. res.json(result);
  92. });
  93. app.get('/shutdown', async (req, res) => {
  94. res.json( { error: false, message : "Sent kill signal to browser" });
  95. process.exit(0);
  96. });
  97. //Modulino
  98. if (require.main === module) {
  99. app.listen( port, () => {
  100. if (argv.debug) {
  101. console.log(`Listening on port ${port}`);
  102. }
  103. });
  104. }