playwright.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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('port', {
  35. alias: 'p',
  36. description: 'Run on specified port',
  37. type: 'number',
  38. })
  39. .option('visible', {
  40. alias: 'v',
  41. description: 'Run with headless mode off',
  42. type: 'boolean',
  43. })
  44. .help()
  45. .alias('help', 'h')
  46. .argv;
  47. const app = express();
  48. const port = argv.port || 6969;
  49. var browser;
  50. var pages = {};
  51. var responses = {};
  52. //XXX this is probably a race but I don't care yet
  53. (async () => {
  54. if (argv._.includes('firefox')) {
  55. browser = await firefox.launch( { "headless" : !argv.visible } );
  56. }
  57. if (argv._.includes('chrome')) {
  58. browser = await chromium.launch( { "headless" : !argv.visible } );
  59. }
  60. if (argv._.includes('webkit')) {
  61. browser = await webkit.launch( { "headless" : !argv.visible } );
  62. }
  63. if (!browser) {
  64. console.log(argv);
  65. console.log('no browser selected, begone');
  66. process.exit(1);
  67. }
  68. pages.default = await browser.newPage();
  69. pages.default.goto('http://google.com');
  70. console.log('Browser Ready for use');
  71. })();
  72. app.use(express.json())
  73. app.get('/command', async (req, res) => {
  74. var payload = req.query;
  75. var page = payload.page;
  76. var result = payload.result;
  77. var command = payload.command;
  78. var args = payload.args || [];
  79. var result = {};
  80. if (typeof args !== 'Array') {
  81. args = [args];
  82. }
  83. if (pages[page] && spec.Page.members[command]) {
  84. // Operate on the provided page
  85. const res = await pages[page][command](...args);
  86. result = { error : false, message : res };
  87. } else if ( results[result] && spec.Result.members[command]) {
  88. const res = await results[result][command]
  89. result = { error : false, message : res };
  90. } else if ( spec.Browser.members[command] || spec.BrowserContext.members[command] ) {
  91. const res = await browser[command](...args);
  92. //File things away for our client modules to interact with
  93. if (command == 'newPage') {
  94. pages[res._guid] = res;
  95. }
  96. if (res._type === 'Response') {
  97. responses[res._guid] = res;
  98. }
  99. result = { error : false, message : res };
  100. } else {
  101. result = { error : true, message : "No such page, or " + command + " is not a globally recognized command for puppeteer" };
  102. }
  103. res.json(result);
  104. });
  105. app.get('/shutdown', async (req, res) => {
  106. console.log('shutting down...');
  107. await browser.close();
  108. console.log('done');
  109. res.send("Sent kill signal to browser");
  110. process.exit(0);
  111. });
  112. //Modulino
  113. if (require.main === module) {
  114. app.listen( port, () => {
  115. console.log(`Listening on port ${port}`);
  116. });
  117. }