playwright.js 4.4 KB

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