|
16 | 16 | use HeadlessChromium\PageUtils\PageEvaluation; |
17 | 17 | use HeadlessChromium\PageUtils\PageNavigation; |
18 | 18 | use HeadlessChromium\PageUtils\PageScreenshot; |
| 19 | +use HeadlessChromium\PageUtils\ResponseWaiter; |
19 | 20 |
|
20 | 21 | class Page |
21 | 22 | { |
@@ -278,6 +279,60 @@ public function screenshot(array $options = []): PageScreenshot |
278 | 279 | return new PageScreenshot($responseReader); |
279 | 280 | } |
280 | 281 |
|
| 282 | + /** |
| 283 | + * Allows to change viewport size, enabling mobile mode, or changing the scale factor |
| 284 | + * |
| 285 | + * usage: |
| 286 | + * |
| 287 | + * ``` |
| 288 | + * $page->setDeviceMetricsOverride |
| 289 | + * ``` |
| 290 | + * @param $overrides |
| 291 | + * @throws CommunicationException |
| 292 | + * @throws NoResponseAvailable |
| 293 | + * |
| 294 | + * @return ResponseWaiter |
| 295 | + * |
| 296 | + */ |
| 297 | + public function setDeviceMetricsOverride(array $overrides) |
| 298 | + { |
| 299 | + if (!array_key_exists('width', $overrides)) { |
| 300 | + $overrides['width'] = 0; |
| 301 | + } |
| 302 | + if (!array_key_exists('height', $overrides)) { |
| 303 | + $overrides['height'] = 0; |
| 304 | + } |
| 305 | + if (!array_key_exists('deviceScaleFactor', $overrides)) { |
| 306 | + $overrides['deviceScaleFactor'] = 0; |
| 307 | + } |
| 308 | + if (!array_key_exists('mobile', $overrides)) { |
| 309 | + $overrides['mobile'] = false; |
| 310 | + } |
| 311 | + |
| 312 | + $this->assertNotClosed(); |
| 313 | + return new ResponseWaiter($this->getSession()->sendMessage( |
| 314 | + new Message('Emulation.setDeviceMetricsOverride', $overrides) |
| 315 | + )); |
| 316 | + } |
| 317 | + |
| 318 | + /** |
| 319 | + * Set viewport size |
| 320 | + * |
| 321 | + * @param int $width |
| 322 | + * @param int $height |
| 323 | + * @throws CommunicationException |
| 324 | + * @throws NoResponseAvailable |
| 325 | + * |
| 326 | + * @return ResponseWaiter |
| 327 | + */ |
| 328 | + public function setViewport(int $width, int $height) |
| 329 | + { |
| 330 | + return $this->setDeviceMetricsOverride([ |
| 331 | + 'width' => $width, |
| 332 | + 'height' => $height |
| 333 | + ]); |
| 334 | + } |
| 335 | + |
281 | 336 | /** |
282 | 337 | * Request to close the page |
283 | 338 | * @throws CommunicationException |
|
0 commit comments