playwright_server 5.0 KB

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