Created
March 28, 2019 18:32
-
-
Save jsoverson/6d3bbb8d8d7db37e1beacaccb295d801 to your computer and use it in GitHub Desktop.
Example of automating VS Code with puppeteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const childProcess = require('child_process'); | |
const puppeteer = require('puppeteer'); | |
const request = require('request-promise-native'); | |
var delay = require('timeout-as-promise'); | |
function spawn(port) { | |
return childProcess.spawn( | |
// '/Applications/Visual\ Studio\ Code\ -\ Insiders.app/Contents/MacOS/Electron', | |
'/Applications/Visual Studio Code.app/Contents/MacOS/Electron', | |
[ | |
`--remote-debugging-port=${port || 9229}`, | |
'--user-data-dir=/tmp/arsietnr', // arbitrary not-mine datadir to get the welcome screen | |
'--enable-logging', | |
], | |
{ | |
detached: true, | |
env: process.env, | |
stido: ['pipe', 'pipe', 'pipe'] | |
} | |
); | |
} | |
async function main(){ | |
const port = 29378; | |
const proc = spawn(port); | |
await delay(2000); | |
const resp = await request(`http://127.0.0.1:${port}/json/list`); | |
const devToolsPages = JSON.parse(resp); | |
const endpoint = devToolsPages.find(p => !p.title.match(/^sharedProcess/)); | |
const browser = await puppeteer.connect({ | |
browserWSEndpoint: endpoint.webSocketDebuggerUrl, | |
defaultViewport: null, // used to bypass Chrome viewport issue, doesn't work w/ VS code. | |
slowMo: 50 | |
}) | |
await delay(1000); | |
const page = (await browser.pages())[0]; | |
await page.click('[href="command:workbench.action.files.newUntitledFile"]'); | |
await page.type('.monaco-editor', 'Woo! I am automating Visual Studio Code with puppeteer!\n'); | |
await page.type('.monaco-editor', 'This would be a super cool way of generating foolproof demos.'); | |
setTimeout(() => proc.kill(), 1000); | |
} | |
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "electron-puppeteer", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"puppeteer": "^1.1.0", | |
"request": "^2.88.0", | |
"request-promise-native": "^1.0.5", | |
"timeout-as-promise": "^1.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment