better Casper.capture()
Showing
1 changed file
with
15 additions
and
9 deletions
... | @@ -106,27 +106,33 @@ | ... | @@ -106,27 +106,33 @@ |
106 | /** | 106 | /** |
107 | * Proxy method for WebPage#render. Adds a clipRect parameter for | 107 | * Proxy method for WebPage#render. Adds a clipRect parameter for |
108 | * automatically set page clipRect setting values and sets it back once | 108 | * automatically set page clipRect setting values and sets it back once |
109 | * done. | 109 | * done. If the cliprect parameter is omitted, the full page viewport |
110 | * area will be rendered. | ||
110 | * | 111 | * |
111 | * @param String targetFile A target filename | 112 | * @param String targetFile A target filename |
112 | * @param mixed clipRect An optional clipRect object | 113 | * @param mixed clipRect An optional clipRect object (optional) |
113 | * @return Casper | 114 | * @return Casper |
114 | */ | 115 | */ |
115 | capture: function(targetFile, clipRect) { | 116 | capture: function(targetFile, clipRect) { |
116 | if (typeof clipRect !== "object") { | 117 | var previousClipRect; |
117 | throw new Error("ClipRect must be an object instance."); | ||
118 | } | ||
119 | var previousClipRect = this.page.clipRect; | ||
120 | if (clipRect) { | 118 | if (clipRect) { |
119 | if (typeof clipRect !== "object") { | ||
120 | throw new Error("clipRect must be an Object instance."); | ||
121 | } | ||
122 | previousClipRect = this.page.clipRect; | ||
121 | this.page.clipRect = clipRect; | 123 | this.page.clipRect = clipRect; |
122 | this.log('Capturing page to ' + targetFile + ' with clipRect' + JSON.stringify(clipRect), "debug"); | 124 | this.log('Capturing page to ' + targetFile + ' with clipRect' + JSON.stringify(clipRect), "debug"); |
123 | } else { | 125 | } else { |
124 | this.log('Capturing page to ' + targetFile, "debug"); | 126 | this.log('Capturing page to ' + targetFile, "debug"); |
125 | } | 127 | } |
126 | if (!this.page.render(targetFile)) { | 128 | try { |
127 | this.log('Failed to capture screenshot as ' + targetFile, "error"); | 129 | this.page.render(targetFile); |
130 | } catch (e) { | ||
131 | this.log('Failed to capture screenshot as ' + targetFile + ': ' + e, "error"); | ||
132 | } | ||
133 | if (previousClipRect) { | ||
134 | this.page.clipRect = previousClipRect; | ||
128 | } | 135 | } |
129 | this.page.clipRect = previousClipRect; | ||
130 | return this; | 136 | return this; |
131 | }, | 137 | }, |
132 | 138 | ... | ... |
-
Please register or sign in to post a comment