playwright.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. //XXX this is probably a race but I don't care yet
  58. (async () => {
  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. pages.default.goto('http://google.com');
  74. if (argv.debug) {
  75. console.log('Browser Ready for use');
  76. }
  77. })();
  78. app.use(express.json())
  79. app.get('/command', async (req, res) => {
  80. var payload = req.query;
  81. var page = payload.page;
  82. var result = payload.result;
  83. var command = payload.command;
  84. var args = payload.args || [];
  85. var result = {};
  86. if (typeof args !== 'Array') {
  87. args = [args];
  88. }
  89. if (pages[page] && spec.Page.members[command]) {
  90. // Operate on the provided page
  91. const res = await pages[page][command](...args);
  92. result = { error : false, message : res };
  93. } else if ( responses[result] && spec.Result.members[command]) {
  94. const res = await responses[result][command]
  95. result = { error : false, message : res };
  96. } else if ( spec.Browser.members[command] || spec.BrowserContext.members[command] ) {
  97. const res = await browser[command](...args);
  98. //File things away for our client modules to interact with
  99. if (command == 'newPage') {
  100. pages[res._guid] = res;
  101. }
  102. if (res._type === 'Response') {
  103. responses[res._guid] = res;
  104. }
  105. result = { error : false, message : res };
  106. } else {
  107. result = { error : true, message : "No such page, or " + command + " is not a globally recognized command for puppeteer" };
  108. }
  109. res.json(result);
  110. });
  111. app.get('/shutdown', async (req, res) => {
  112. await browser.close();
  113. res.json( { error: false, message : "Sent kill signal to browser" });
  114. process.exit(0);
  115. });
  116. //Modulino
  117. if (require.main === module) {
  118. app.listen( port, () => {
  119. if (argv.debug) {
  120. console.log(`Listening on port ${port}`);
  121. }
  122. });
  123. }