-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
puppeteer sample (Headless Chrome) #625
Changes from 9 commits
a6b9c28
f40b442
9708edc
ecf1f20
6c05d34
9cae70e
8638c40
d478a66
1d00f38
a610c3f
4fc6f91
833f322
cf566d6
2a584ed
e7616bb
dcc642e
c80a5f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Headless Chrome on Google App Engine | ||
|
||
This sample application demonstrates how to use Headless Chrome via the [Puppeteer](https://developers.google.com/web/tools/puppeteer/) module to take screenshots of webpages on [Google App Engine](https://cloud.google.com/appengine) Node.js [standard environment](https://cloud.google.com/appengine/docs/standard/nodejs). | ||
|
||
## Running locally | ||
|
||
* Install dependencies with `npm install` (Headless Chrome will take some time to download), | ||
* Start the application locally using `npm start`. | ||
|
||
## Deploy to App Engine | ||
|
||
* Install the [Google Cloud SDK](https://cloud.google.com/sdk/) admn create a Google Cloud project | ||
* Run `gcloud app deploy` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2018 Google LLC | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
// [START full_sample] | ||
const express = require('express'); | ||
const puppeteer = require('puppeteer'); | ||
const app = express(); | ||
|
||
app.use(async (req, res) => { | ||
const url = req.query.url; | ||
|
||
if (!url) { | ||
return res.send('Please provide URL as GET parameter, for example: <a href="/?url=https://example.com">?url=https://example.com</a>'); | ||
} | ||
|
||
// [START browser] | ||
const browser = await puppeteer.launch({ | ||
args: ['--no-sandbox', '--disable-setuid-sandbox'] | ||
}); | ||
// [END browser] | ||
const page = await browser.newPage(); | ||
await page.goto(url); | ||
const imageBuffer = await page.screenshot(); | ||
await browser.close(); | ||
|
||
res.set('Content-Type', 'image/png'); | ||
res.send(imageBuffer); | ||
}); | ||
|
||
const server = app.listen(process.env.PORT || 8080, err => { | ||
if (err) return console.error(err); | ||
const port = server.address().port; | ||
console.info(`App listening on port ${port}`); | ||
}); | ||
// [END full_sample] | ||
|
||
module.exports = app; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2018, Google LLC | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License | ||
|
||
# [START app_yaml] | ||
runtime: nodejs8 | ||
instance_class: F4_1G | ||
# [END app_yaml] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "headless-chrome-sample", | ||
"engines": { | ||
"node": "8.x" | ||
}, | ||
"version": "1.0.0", | ||
"description": "An example of taking screenshot with Puppeteer (Headless Chrome) in Node.js on Google App Engine.", | ||
"scripts": { | ||
"start": "node app.js", | ||
"system-test": "repo-tools test app", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only have one test but we're using it both as system-test and as unit-test 😉 It should be run as one or the other. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure I understand your comment. Can you be more specific about what I should do? We have 2 different kind of tests runnign here. both of them are run when running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But there's only one test file with one test. Where's the second test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please compare to the other samples in this repository, I tried to mimic the architecture as much as possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See for example https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/pubsub That also has these two tests. |
||
"unit-test": "ava --verbose test/*.test.js", | ||
"lint": "repo-tools lint", | ||
"pretest": "npm run lint", | ||
"test": "npm run unit-test && npm run system-test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
}, | ||
"author": { | ||
"name": "Google LLC" | ||
}, | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"express": "^4.16.3", | ||
"puppeteer": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"@google-cloud/nodejs-repo-tools": "2.2.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we're not using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
"ava": "^0.25.0", | ||
"semistandard": "^12.0.1" | ||
}, | ||
"cloud-repo-tools": { | ||
"test": { | ||
"app": { | ||
"msg": "Please provide URL" | ||
} | ||
}, | ||
"requiresKeyFile": true, | ||
"requiresProjectId": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2017, Google, Inc. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// NOTE: | ||
// This app can only be fully tested when deployed, because | ||
// Pub/Sub requires a live endpoint URL to hit. Nevertheless, | ||
// these tests mock it and partially test it locally. | ||
|
||
'use strict'; | ||
|
||
const test = require(`ava`); | ||
const path = require(`path`); | ||
const utils = require(`@google-cloud/nodejs-repo-tools`); | ||
|
||
const cwd = path.join(__dirname, `../`); | ||
const requestObj = utils.getRequest({ cwd: cwd }); | ||
|
||
test.serial.cb(`should return a screenshot`, t => { | ||
requestObj | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, this test does not test the sample. I'd expect something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does tests and the test passes. I think the magic happens with |
||
.get(`/?url=https://example.com`) | ||
.send() | ||
.expect(200) | ||
.expect((response) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the extra There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
t.is(response.type, `image/png`); | ||
t.true(response.body instanceof Buffer); | ||
t.true(response.body.length > 0); | ||
}) | ||
.end(t.end); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the prerequisites are missing here. Do we not link to the
gcloud
"Before you get started" section in these samples?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No we do not.
But I am happy to add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Refer to the appengine/README.md file for more instructions on running and deploying this app" is the sentence most examples in the repo use. Can we do the same? Then we also have the section on credentials.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I just pushed a change that point to the main README