File tree Expand file tree Collapse file tree 4 files changed +45
-4
lines changed Expand file tree Collapse file tree 4 files changed +45
-4
lines changed Original file line number Diff line number Diff line change 4
4
5
5
namespace HeadlessChromium \Dom ;
6
6
7
+ use HeadlessChromium \Clip ;
7
8
use HeadlessChromium \Communication \Message ;
8
9
use HeadlessChromium \Communication \Response ;
9
10
use HeadlessChromium \Exception \DomException ;
@@ -214,4 +215,20 @@ public function assertNotError(Response $response): void
214
215
throw new DOMException ($ response ->getErrorMessage ());
215
216
}
216
217
}
218
+
219
+ public function getClip (): ?Clip
220
+ {
221
+ $ position = $ this ->getPosition ();
222
+
223
+ if (!$ position ) {
224
+ return null ;
225
+ }
226
+
227
+ return new Clip (
228
+ $ position ->getX (),
229
+ $ position ->getY (),
230
+ $ position ->getWidth (),
231
+ $ position ->getHeight (),
232
+ );
233
+ }
217
234
}
Original file line number Diff line number Diff line change @@ -44,14 +44,14 @@ public function __construct(array $points)
44
44
$ this ->width = $ rightBottomX - $ leftBottomX ;
45
45
}
46
46
47
- public function getX (): int
47
+ public function getX (): float
48
48
{
49
- return (int ) $ this ->x ;
49
+ return (float ) $ this ->x ;
50
50
}
51
51
52
- public function getY (): int
52
+ public function getY (): float
53
53
{
54
- return (int ) $ this ->y ;
54
+ return (float ) $ this ->y ;
55
55
}
56
56
57
57
public function getWidth (): int
Original file line number Diff line number Diff line change 17
17
use HeadlessChromium \Cookies \Cookie ;
18
18
use HeadlessChromium \Cookies \CookiesCollection ;
19
19
use HeadlessChromium \Dom \Dom ;
20
+ use HeadlessChromium \Dom \Node ;
20
21
use HeadlessChromium \Dom \Selector \CssSelector ;
21
22
use HeadlessChromium \Dom \Selector \Selector ;
22
23
use HeadlessChromium \Exception \CommunicationException ;
@@ -673,6 +674,13 @@ public function screenshot(array $options = []): PageScreenshot
673
674
return new PageScreenshot ($ responseReader );
674
675
}
675
676
677
+ public function screenshotElement (Node $ node ): PageScreenshot
678
+ {
679
+ return $ this ->screenshot ([
680
+ 'clip ' => $ node ->getClip (),
681
+ ]);
682
+ }
683
+
676
684
/**
677
685
* Generate a PDF
678
686
* Usage:.
Original file line number Diff line number Diff line change @@ -507,4 +507,20 @@ public function testSetScriptExecution(): void
507
507
$ page ->evaluate ('document.body.innerText ' )->getReturnValue ()
508
508
);
509
509
}
510
+
511
+ public function testElementScreenshot (): void
512
+ {
513
+ $ factory = new BrowserFactory ();
514
+
515
+ $ browser = $ factory ->createBrowser ();
516
+ $ page = $ browser ->createPage ();
517
+
518
+ $ page ->navigate ($ this ->sitePath ('domForm.html ' ))->waitForNavigation ();
519
+
520
+ $ element = $ page ->dom ()->querySelector ('#myform ' );
521
+ $ screenshot = $ page ->screenshotElement ($ element );
522
+
523
+ self ::assertNotEmpty ($ screenshot ->getBase64 ());
524
+ self ::assertGreaterThan (4000 , \strlen ($ screenshot ->getBase64 ()));
525
+ }
510
526
}
You can’t perform that action at this time.
0 commit comments