Skip to content
Open
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
18 changes: 17 additions & 1 deletion lib/screencap/raster.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// - maxRenderWait [optional] - default 10000 - the maximum time to wait before taking the screenshot, regardless of whether resources are waiting to be loaded
// - cutoffWait [optional] - default null - the maximum time to wait before cutting everything off and failing...this helps if there is a page taking a long time to load
// - top, left, width, height [optional] - dimensions to use to screenshot a specific area of the screen
// - auth [optional] - Basic auth (ie. 'username:password') option if required by URL
//
// == Important notice when providing height ==
//
Expand All @@ -28,7 +29,9 @@ var page = new WebPage(),
mask = null,
forcedRenderTimeout,
renderTimeout,
cutoffTimeout;
cutoffTimeout,
userName,
password;

//
// Functions
Expand Down Expand Up @@ -58,6 +61,11 @@ function pickupNamedArguments() {
if(args.resourceWait) { resourceWait = args.resourceWait; }
if(args.maxRenderWait) { maxRenderWait = args.maxRenderWait; }
if(args.cutoffWait) { cutoffWait = args.cutoffWait; }
if(args.auth) {
pair = args.auth.split(/:/);
Copy link

Choose a reason for hiding this comment

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

Might be nicer to just have separate username and password fields as this will have issues if either contains a ":"

userName = pair[0];
password = pair[1];
}
}

function setupMask() {
Expand Down Expand Up @@ -108,8 +116,16 @@ function evaluateWithArgs(func) {
return page.evaluate(fn);
}

function setBasicAuthentication() {
if (args.auth) {
page.settings.userName = userName;
page.settings.password = password;
}
}

function takeScreenshot() {
cutoffExecution();
setBasicAuthentication()
page.open(args.url, function(status) {
if(status !== 'success') {
console.log('Unable to load: ' + args.url);
Expand Down
6 changes: 6 additions & 0 deletions spec/fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@
screenshot = Screencap::Fetcher.new('http://google.com?1=2&3=4').fetch(:output => TMP_DIRECTORY + 'ampersand.jpg', :width => 800)
FastImage.size(screenshot)[0].should == 800
end

it 'should support basic auth' do
url = 'http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?' + "0.#{rand(99999999)}" # hosted example requires fresh seed
screenshot = Screencap::Fetcher.new(url).fetch(:output => TMP_DIRECTORY + 'httpwatch.jpg', :auth => 'httpwatch:screencap', :width => 300, height: 60)
FastImage.size(screenshot)[0].should == 300
Copy link

Choose a reason for hiding this comment

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

Will this fail if auth is invalid? Seems like it might screenshot an error page?

end
end