Skip to content

Commit 414dfdb

Browse files
committed
Add option 'errorIfStatusIsNot200'.
1 parent 9f29e96 commit 414dfdb

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ the callback in a call to webshot.
175175
<td>Wait for the web page to signal to webshot when to take the photo
176176
using <code>window.callPhantom('takeShot');</code></td>
177177
</tr>
178+
<tr>
179+
<th>errorIfStatusIsNot200</th>
180+
<td>false</td>
181+
<td>If the loaded page has a non-200 status code, don't take a screenshot, cause an error instead.</td>
182+
</tr>
178183
</tbody>
179184
</table>
180185

lib/options.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exports.phantom = {
2020
, siteType: 'url'
2121
, renderDelay: 0
2222
, quality: 75
23+
, errorIfStatusIsNot200: false
2324
};
2425

2526
// Options that are just passed to the phantom page object

lib/webshot.js

+6
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ function spawnPhantom(site, path, streaming, options, cb) {
232232
s.emit('data', new Buffer(''+data, 'base64'));
233233
});
234234

235+
phantomProc.stderr.on('data', function(data) {
236+
clearTimeout(timeoutID);
237+
cb(new Error(data));
238+
this.kill();
239+
});
240+
235241
phantomProc.on('exit', function() {
236242
s.emit('end');
237243
this.kill();

lib/webshot.phantom.js

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ var path = system.args.length == 4 ? null : system.args[2];
99
var streaming = ((system.args.length == 4 ? system.args[2] : system.args[3]) === 'true');
1010
var options = JSON.parse(system.args.length == 4 ? system.args[3] : system.args[4]);
1111

12+
var failToStderr = function(message) {
13+
system.stderr.write(message);
14+
page.close();
15+
phantom.exit(1);
16+
};
17+
1218
page.viewportSize = {
1319
width: options.windowSize.width
1420
, height: options.windowSize.height
@@ -17,6 +23,16 @@ page.viewportSize = {
1723
// Capture JS errors and ignore them
1824
page.onError = function(msg, trace) {};
1925

26+
if (options.errorIfStatusIsNot200) {
27+
page.onResourceReceived = function(response) {
28+
// If request to the page is not 200 status, fail.
29+
if (response.url === site && response.status !== 200) {
30+
failToStderr('Status must be 200; is ' + response.status);
31+
return;
32+
}
33+
};
34+
}
35+
2036
// Register user-provided callbacks
2137
optUtils.phantomCallback.forEach(function(cbName) {
2238
var cb = options[cbName];

test/tests.js

+29
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,35 @@ describe('Handling miscellaneous options', function() {
409409
});
410410
});
411411
});
412+
413+
it('screenshots a non-200 page status if errorIfStatusIsNot200 not set', function(done) {
414+
var options = {
415+
};
416+
417+
this.timeout(20000);
418+
419+
webshot('google.com/404-page-for-webshot-tests', testPNG, options, function(err) {
420+
fs.exists(testPNG, function(exists) {
421+
exists.should.equal(true);
422+
done();
423+
});
424+
});
425+
});
426+
427+
it('errors on non-200 page status if errorIfStatusIsNot200 set', function(done) {
428+
var options = {
429+
errorIfStatusIsNot200: true
430+
};
431+
432+
this.timeout(20000);
433+
434+
webshot('google.com/404-page-for-webshot-tests', testPNG, options, function(err) {
435+
fs.exists(testPNG, function(exists) {
436+
exists.should.equal(false);
437+
done();
438+
});
439+
});
440+
});
412441
});
413442

414443
afterEach(function(done) {

0 commit comments

Comments
 (0)