playwright_server 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // On newer versions of playwright, instead of using hash tables they just lazily output arrays, so we have to convert this
  14. let spec = JSON.parse(rawdata);
  15. function arr2hash (arr,primary_key) {
  16. var inside_out = {};
  17. for (var item of arr) {
  18. inside_out[item.name] = item;
  19. }
  20. return inside_out;
  21. }
  22. var fix_it=false;
  23. if (spec instanceof Array) {
  24. fix_it = true;
  25. spec = arr2hash(spec,'name');
  26. }
  27. // Establish argument order for callers, and correct spec array-ification
  28. for (var classname of Object.keys(spec)) {
  29. if (spec[classname].members instanceof Array) {
  30. spec[classname].members = arr2hash(spec[classname].members,'name');
  31. }
  32. for (var method of Object.keys(spec[classname].members)) {
  33. var order = 0;
  34. if (spec[classname].members[method].args instanceof Array) {
  35. spec[classname].members[method].args = arr2hash(spec[classname].members[method].args,'name');
  36. }
  37. for (var arg of Object.keys(spec[classname].members[method].args) ) {
  38. spec[classname].members[method].args[arg].order = order++;
  39. }
  40. }
  41. }
  42. //XXX spec is wrong here unfortunately
  43. if (fix_it) {
  44. for (var className of ['Page','Frame']) {
  45. spec[className].members.$$ = spec[className].members.querySelectorAll;
  46. spec[className].members.$ = spec[className].members.querySelector;
  47. spec[className].members.$$eval = spec[className].members.evalOnSelectorAll;
  48. spec[className].members.$eval = spec[className].members.evalOnSelector;
  49. }
  50. }
  51. const argv = yargs
  52. .option('debug', {
  53. alias: 'd',
  54. description: 'Print additional debugging messages',
  55. type: 'boolean',
  56. })
  57. .option('port', {
  58. alias: 'p',
  59. description: 'Run on specified port',
  60. type: 'number',
  61. })
  62. .help()
  63. .alias('help', 'h')
  64. .argv;
  65. const app = express();
  66. const port = argv.port || 6969;
  67. var objects = {};
  68. var browsers = { 'firefox' : firefox, 'chrome' : chromium, 'webkit' : webkit };
  69. //Stash for users to put data in
  70. var userdata = {};
  71. app.use(express.json())
  72. app.get('/spec', async (req, res) => {
  73. res.json( { error : false, message : spec } );
  74. });
  75. app.post('/session', async (req, res) => {
  76. var payload = req.body;
  77. var type = payload.type;
  78. var args = payload.args || [];
  79. var result;
  80. if ( type && browsers[type] ) {
  81. try {
  82. var browser = await browsers[type].launch(...args);
  83. objects[browser._guid] = browser;
  84. result = { error : false, message : browser };
  85. } catch (e) {
  86. result = { error : true, message : e.message};
  87. }
  88. } else {
  89. result = { error : true, message : "Please select a supported browser" };
  90. }
  91. res.json(result);
  92. });
  93. app.post('/command', async (req, res) => {
  94. var payload = req.body;
  95. var type = payload.type;
  96. var object = payload.object;
  97. var command = payload.command;
  98. var args = payload.args || [];
  99. var result = {};
  100. if (argv.debug) {
  101. console.log(type,object,command,args);
  102. }
  103. // XXX this would be cleaner if the mouse() and keyboard() methods just returned a Mouse and Keyboard object
  104. var subject = objects[object];
  105. if (subject) {
  106. if (type == 'Mouse') {
  107. subject = objects[object].mouse;
  108. } else if (type == 'Keyboard' ) {
  109. subject = objects[object].keyboard;
  110. }
  111. }
  112. if (subject && spec[type] && spec[type].members[command]) {
  113. try {
  114. //XXX We have to do a bit of 'special' handling for scripts
  115. // This has implications for the type of scripts you can use
  116. if (command == 'evaluate' || command == 'evaluateHandle') {
  117. var toEval = args.shift();
  118. const fun = new Function (toEval);
  119. args = [
  120. fun,
  121. ...args
  122. ];
  123. }
  124. const res = await subject[command](...args);
  125. result = { error : false, message : res };
  126. if (Array.isArray(res)) {
  127. for (var r of res) {
  128. objects[r._guid] = r;
  129. }
  130. }
  131. // XXX videos are special, we have to magic up a guid etc for them
  132. if (command == 'video' && res) {
  133. res._guid = 'Video@' + uuidv4();
  134. res._type = 'Video';
  135. }
  136. // XXX So are FileChooser object unfortunately
  137. if (args[0] == 'filechooser' && res) {
  138. res._guid = 'FileChooser@' + uuidv4();
  139. res._type = 'FileChooser';
  140. }
  141. if (res && res._guid) {
  142. objects[res._guid] = res;
  143. }
  144. } catch (e) {
  145. result = { error : true, message : e.message };
  146. }
  147. // Allow creation of event listeners if we can actually wait for them
  148. } else if (command == 'on' && subject && spec[type].members.waitForEvent ) {
  149. try {
  150. var evt = args.shift();
  151. const cb = new Function (args.shift());
  152. subject.on(evt,cb);
  153. result = { error : false, message : "Listener set up" };
  154. } catch (e) {
  155. result = { error : true, message : e.message };
  156. }
  157. } else {
  158. result = { error : true, message : "No such object, or " + command + " is not a globally recognized command for Playwright" };
  159. }
  160. res.json(result);
  161. });
  162. app.get('/shutdown', async (req, res) => {
  163. res.json( { error: false, message : "Sent kill signal to browser" });
  164. process.exit(0);
  165. });
  166. //Modulino
  167. if (require.main === module) {
  168. app.listen( port, () => {
  169. if (argv.debug) {
  170. console.log(`Listening on port ${port}`);
  171. }
  172. });
  173. }