playwright.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 browser;
  55. var pages = {};
  56. var responses = {};
  57. app.use(express.json())
  58. app.get('/session', async (req, res) => {
  59. if (argv._.includes('firefox')) {
  60. browser = await firefox.launch( { "headless" : !argv.visible } );
  61. }
  62. if (argv._.includes('chrome')) {
  63. browser = await chromium.launch( { "headless" : !argv.visible } );
  64. }
  65. if (argv._.includes('webkit')) {
  66. browser = await webkit.launch( { "headless" : !argv.visible } );
  67. }
  68. if (!browser) {
  69. console.log('no browser selected, begone');
  70. process.exit(1);
  71. }
  72. pages.default = await browser.newPage();
  73. if (argv.debug) {
  74. console.log('Browser Ready for use');
  75. }
  76. res.json({ error: false, message: 'Browser started successfully.' });
  77. });
  78. app.post('/command', async (req, res) => {
  79. var payload = req.body;
  80. var page = payload.page;
  81. var result = payload.result;
  82. var command = payload.command;
  83. var args = payload.args || [];
  84. var result = {};
  85. if (pages[page] && spec.Page.members[command]) {
  86. // Operate on the provided page
  87. const res = await pages[page][command](...args);
  88. result = { error : false, message : res };
  89. } else if ( responses[result] && spec.Result.members[command]) {
  90. const res = await responses[result][command]
  91. result = { error : false, message : res };
  92. } else if ( spec.Browser.members[command] || spec.BrowserContext.members[command] ) {
  93. const res = await browser[command](...args);
  94. //File things away for our client modules to interact with
  95. if (command == 'newPage') {
  96. pages[res._guid] = res;
  97. }
  98. if (res._type === 'Response') {
  99. responses[res._guid] = res;
  100. }
  101. result = { error : false, message : res };
  102. } else {
  103. result = { error : true, message : "No such page, or " + command + " is not a globally recognized command for puppeteer" };
  104. }
  105. res.json(result);
  106. });
  107. app.get('/shutdown', async (req, res) => {
  108. if (browser) {
  109. await browser.close();
  110. }
  111. res.json( { error: false, message : "Sent kill signal to browser" });
  112. process.exit(0);
  113. });
  114. //Modulino
  115. if (require.main === module) {
  116. app.listen( port, () => {
  117. if (argv.debug) {
  118. console.log(`Listening on port ${port}`);
  119. }
  120. });
  121. }