playwright_server 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. const path = require('path');
  9. // Defines our interface
  10. let sharedir = require.resolve('playwright'); // api.json should be shipped with playwright itself
  11. var theFile = path.dirname(sharedir) + '/api.json';
  12. let rawdata = fs.readFileSync(theFile);
  13. let spec = JSON.parse(rawdata);
  14. const argv = yargs
  15. .option('debug', {
  16. alias: 'd',
  17. description: 'Print additional debugging messages',
  18. type: 'boolean',
  19. })
  20. .option('port', {
  21. alias: 'p',
  22. description: 'Run on specified port',
  23. type: 'number',
  24. })
  25. .help()
  26. .alias('help', 'h')
  27. .argv;
  28. const app = express();
  29. const port = argv.port || 6969;
  30. var objects = {};
  31. var browsers = { 'firefox' : firefox, 'chrome' : chromium, 'webkit' : webkit };
  32. //Stash for users to put data in
  33. var userdata = {};
  34. app.use(express.json())
  35. app.post('/session', async (req, res) => {
  36. var payload = req.body;
  37. var type = payload.type;
  38. var args = payload.args || [];
  39. var result;
  40. if ( type && browsers[type] ) {
  41. try {
  42. var browser = await browsers[type].launch(...args);
  43. objects[browser._guid] = browser;
  44. result = { error : false, message : browser };
  45. } catch (e) {
  46. result = { error : true, message : e.message};
  47. }
  48. } else {
  49. result = { error : true, message : "Please select a supported browser" };
  50. }
  51. res.json(result);
  52. });
  53. app.post('/command', async (req, res) => {
  54. var payload = req.body;
  55. var type = payload.type;
  56. var object = payload.object;
  57. var command = payload.command;
  58. var args = payload.args || [];
  59. var result = {};
  60. if (argv.debug) {
  61. console.log(type,object,command,args);
  62. }
  63. // XXX this would be cleaner if the mouse() and keyboard() methods just returned a Mouse and Keyboard object
  64. var subject = objects[object];
  65. if (subject) {
  66. if (type == 'Mouse') {
  67. subject = objects[object].mouse;
  68. } else if (type == 'Keyboard' ) {
  69. subject = objects[object].keyboard;
  70. }
  71. }
  72. if (subject && spec[type] && spec[type].members[command]) {
  73. try {
  74. //XXX We have to do a bit of 'special' handling for scripts
  75. // This has implications for the type of scripts you can use
  76. if (command == 'evaluate' || command == 'evaluateHandle') {
  77. var toEval = args.shift();
  78. const fun = new Function (toEval);
  79. args = [
  80. fun,
  81. ...args
  82. ];
  83. }
  84. const res = await subject[command](...args);
  85. result = { error : false, message : res };
  86. if (Array.isArray(res)) {
  87. for (var r of res) {
  88. objects[r._guid] = r;
  89. }
  90. }
  91. // XXX videos are special, we have to magic up a guid etc for them
  92. if (command == 'video' && res) {
  93. res._guid = 'Video@' + uuidv4();
  94. res._type = 'Video';
  95. }
  96. // XXX So are FileChooser object unfortunately
  97. if (args[0] == 'filechooser' && res) {
  98. res._guid = 'FileChooser@' + uuidv4();
  99. res._type = 'FileChooser';
  100. }
  101. if (res && res._guid) {
  102. objects[res._guid] = res;
  103. }
  104. } catch (e) {
  105. result = { error : true, message : e.message };
  106. }
  107. // Allow creation of event listeners if we can actually wait for them
  108. } else if (command == 'on' && subject && spec[type].members.waitForEvent ) {
  109. try {
  110. var evt = args.shift();
  111. const cb = new Function (args.shift());
  112. subject.on(evt,cb);
  113. result = { error : false, message : "Listener set up" };
  114. } catch (e) {
  115. result = { error : true, message : e.message };
  116. }
  117. } else {
  118. result = { error : true, message : "No such object, or " + command + " is not a globally recognized command for puppeteer" };
  119. }
  120. res.json(result);
  121. });
  122. app.get('/shutdown', async (req, res) => {
  123. res.json( { error: false, message : "Sent kill signal to browser" });
  124. process.exit(0);
  125. });
  126. //Modulino
  127. if (require.main === module) {
  128. app.listen( port, () => {
  129. if (argv.debug) {
  130. console.log(`Listening on port ${port}`);
  131. }
  132. });
  133. }