Skip to content

Commit

Permalink
LukeIS on behalf of YabinKang: have iPhoneDriver take a screenshot of…
Browse files Browse the repository at this point in the history
… the entire page, not just what's in view. (Awesome!) Fixes Issue 4529

r17793
  • Loading branch information
lukeis committed Sep 12, 2012
1 parent c559814 commit 7af4737
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
6 changes: 6 additions & 0 deletions iphone/src/objc/WebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
int numPendingPageLoads_;

NSString *lastJSResult_;

// Get a screenshot of the page we've loaded
UIImage *screenshot_;

NSURLRequestCachePolicy cachePolicy_;

Expand Down Expand Up @@ -90,6 +93,9 @@
// Set geolocation
- (void)setLocation:(NSDictionary *)dict;

// get ss
- (void)getFullPageScreenShot;

// Check if browser connection is alive
- (NSNumber *)isBrowserOnline;

Expand Down
50 changes: 38 additions & 12 deletions iphone/src/objc/WebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ - (void)viewDidLoad {
}

lastJSResult_ = nil;
screenshot_ = nil;

// Creating a new session if auto-create is enabled
if ([[RootViewController sharedInstance] isAutoCreateSession]) {
Expand Down Expand Up @@ -98,6 +99,7 @@ - (void)didReceiveMemoryWarning {
- (void)dealloc {
[[self webView] setDelegate:nil];
[lastJSResult_ release];
[screenshot_ release];
[super dealloc];
}

Expand Down Expand Up @@ -473,18 +475,8 @@ - (NSString *)source {

// Takes a screenshot.
- (UIImage *)screenshot {
UIGraphicsBeginImageContext([[self webView] bounds].size);
[[self webView].layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// dump the screenshot into a file for debugging
//NSString *path = [[[NSSearchPathForDirectoriesInDomains
// (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
// stringByAppendingPathComponent:@"screenshot.png"] retain];
//[UIImagePNGRepresentation(viewImage) writeToFile:path atomically:YES];

return viewImage;
[self performSelectorOnMainThread:@selector(getFullPageScreenShot) withObject:nil waitUntilDone:YES];
return screenshot_;
}

- (NSString *)URL {
Expand Down Expand Up @@ -569,6 +561,40 @@ - (void)setLocation:(NSDictionary *)dict {
[locStorage setAltitude:[altitude doubleValue]];
}

// get the full page screenshot.
- (void)getFullPageScreenShot {
// stop the webview
[self.webView setDelegate:nil];
[self.webView stopLoading];

// keep the original window size.
CGRect oriFrame = self.webView.frame;
CGRect oriBounds = self.webView.bounds;

// get the page render actual size
CGRect tmpFrame = self.webView.frame;
tmpFrame.size.height = 1;
self.webView.frame = tmpFrame;
CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero];
tmpFrame.size = fittingSize;
self.webView.frame = tmpFrame;

// render the page and take the screenshot
UIGraphicsBeginImageContext(fittingSize);
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
[[self webView].layer renderInContext:resizedContext];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//reset webview back to original size.
self.webView.bounds = oriBounds;
self.webView.frame = oriFrame;

// retain the screenshot for webdriver
[screenshot_ release];
screenshot_ = [viewImage retain];
}

// Finds out if browser connection is alive
- (NSNumber *)isBrowserOnline {
BOOL onlineState = [[self jsEval:@"navigator.onLine"] isEqualToString:@"true"];
Expand Down

0 comments on commit 7af4737

Please sign in to comment.