@@ -139,6 +139,15 @@ public function hasLifecycleEvent(string $event): bool
139139
140140 /**
141141 *
142+ * Example:
143+ *
144+ * ```php
145+ * $page->screenshot()->saveToFile();
146+ * ```
147+ *
148+ * @param array $options
149+ * @return PageScreenshot
150+ * @throws CommunicationException
142151 */
143152 public function screenshot (array $ options = []): PageScreenshot
144153 {
@@ -185,13 +194,27 @@ public function screenshot(array $options = []): PageScreenshot
185194 $ screenshotOptions ['quality ' ] = $ options ['quality ' ];
186195 }
187196
188- // TODO document clip
189- // TODO validate clip/use viewport object
197+ // clip
190198 if (array_key_exists ('clip ' , $ options )) {
191- $ screenshotOptions ['clip ' ] = $ options ['clip ' ];
199+
200+ // make sure it's a Clip instance
201+ if (!($ options ['clip ' ] instanceof Clip)) {
202+ throw new \InvalidArgumentException (
203+ sprintf ('Invalid options "clip" for page screenshot, it must be a %s instance. ' , Clip::class)
204+ );
205+ }
206+
207+ // add to params
208+ $ screenshotOptions ['clip ' ] = [
209+ 'x ' => $ options ['clip ' ]->getX (),
210+ 'y ' => $ options ['clip ' ]->getY (),
211+ 'width ' => $ options ['clip ' ]->getWidth (),
212+ 'height ' => $ options ['clip ' ]->getHeight (),
213+ 'scale ' => $ options ['clip ' ]->getScale ()
214+ ];
192215 }
193216
194- // request screen shot
217+ // request screenshot
195218 $ responseReader = $ this ->getSession ()
196219 ->sendMessage (new Message ('Page.captureScreenshot ' , $ screenshotOptions ));
197220
0 commit comments