playwright.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/node
  2. "use strict";
  3. const yargs = require('yargs');
  4. const uuid = 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. .command('firefox', 'Starts a playwright instance of firefox', {
  14. firefox: {
  15. description: 'Start a firefox instance',
  16. alias: 'f',
  17. type: 'boolean',
  18. }
  19. })
  20. .command('chrome', 'Starts a playwright instance of chrome', {
  21. chrome: {
  22. description: 'Start a chrome instance',
  23. alias: 'c',
  24. type: 'boolean',
  25. }
  26. })
  27. .command('webkit', 'Starts a playwright instance of webkit', {
  28. webkit: {
  29. description: 'Start a webkit instance',
  30. alias: 'w',
  31. type: 'boolean',
  32. }
  33. })
  34. .option('debug', {
  35. alias: 'd',
  36. description: 'Print additional debugging messages',
  37. type: 'boolean',
  38. })
  39. .option('port', {
  40. alias: 'p',
  41. description: 'Run on specified port',
  42. type: 'number',
  43. })
  44. .option('visible', {
  45. alias: 'v',
  46. description: 'Run with headless mode off',
  47. type: 'boolean',
  48. })
  49. .help()
  50. .alias('help', 'h')
  51. .argv;
  52. const app = express();
  53. const port = argv.port || 6969;
  54. var objects = {};
  55. app.use(express.json())
  56. app.get('/session', async (req, res) => {
  57. if (argv._.includes('firefox')) {
  58. objects.browser = await firefox.launch( { "headless" : !argv.visible } );
  59. }
  60. if (argv._.includes('chrome')) {
  61. objects.browser = await chromium.launch( { "headless" : !argv.visible } );
  62. }
  63. if (argv._.includes('webkit')) {
  64. objects.browser = await webkit.launch( { "headless" : !argv.visible } );
  65. }
  66. if (!objects.browser) {
  67. console.log('no browser selected, begone');
  68. process.exit(1);
  69. }
  70. if (argv.debug) {
  71. console.log('Browser Ready for use');
  72. }
  73. res.json({ error: false, message: 'Browser started successfully.' });
  74. });
  75. app.post('/command', async (req, res) => {
  76. var payload = req.body;
  77. var type = payload.type;
  78. var object = payload.object;
  79. var command = payload.command;
  80. var args = payload.args || [];
  81. var result = {};
  82. if (objects[object] && spec[type] && spec[type].members[command]) {
  83. try {
  84. const res = await objects[object][command](...args);
  85. result = { error : false, message : res };
  86. if (res._guid) {
  87. objects[res._guid] = res;
  88. }
  89. } catch (e) {
  90. result = { error : true, message : e.message };
  91. }
  92. } else {
  93. result = { error : true, message : "No such object, or " + command + " is not a globally recognized command for puppeteer" };
  94. }
  95. res.json(result);
  96. });
  97. app.get('/shutdown', async (req, res) => {
  98. if (objects.browser) {
  99. await objects.browser.close();
  100. }
  101. res.json( { error: false, message : "Sent kill signal to browser" });
  102. process.exit(0);
  103. });
  104. //Modulino
  105. if (require.main === module) {
  106. app.listen( port, () => {
  107. if (argv.debug) {
  108. console.log(`Listening on port ${port}`);
  109. }
  110. });
  111. }