playwright.js 4.4 KB

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