Skip to content
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

Merged
merged 17 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions appengine/headless-chrome/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 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).
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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


## Running and deploying

Refer to the [appengine/README.md](../README.md) file for instructions on running and deploying.
51 changes: 51 additions & 0 deletions appengine/headless-chrome/app.js
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;
17 changes: 17 additions & 0 deletions appengine/headless-chrome/app.standard.yaml
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]
42 changes: 42 additions & 0 deletions appengine/headless-chrome/package.json
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",
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 nom run test

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
The test run with system-test is defined in package.json in cloud-repo-tools

Copy link
Contributor Author

@steren steren Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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.3.0",
"ava": "^0.25.0",
"semistandard": "^12.0.1"
},
"cloud-repo-tools": {
"test": {
"app": {
"msg": "Please provide URL"
}
},
"requiresKeyFile": true,
"requiresProjectId": true
}
}
39 changes: 39 additions & 0 deletions appengine/headless-chrome/test/app.test.js
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
.get(`/?url=https://example.com`)
.send()
.expect(200)
.expect(response => {
t.is(response.type, `image/png`);
t.true(response.body instanceof Buffer);
t.true(response.body.length > 0);
})
.end(t.end);
});