File tree Expand file tree Collapse file tree 3 files changed +41
-1
lines changed Expand file tree Collapse file tree 3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -508,6 +508,15 @@ Browsershot::url('https://example.com')
508
508
...
509
509
```
510
510
511
+ #### Setting Cookies
512
+
513
+ To add cookies to HTTP headers, pass an array containing cookie key/value pairs to ` useCookies ` method:
514
+
515
+ ``` php
516
+ Browsershot::url('https://example.com')
517
+ ->useCookies(['Cookie-Key' => 'Cookie-Value'])
518
+ ...
519
+ ```
511
520
512
521
#### Clicking on the page
513
522
Original file line number Diff line number Diff line change @@ -107,6 +107,13 @@ public function setChromePath(string $executablePath)
107
107
return $ this ;
108
108
}
109
109
110
+ public function useCookies (array $ cookies )
111
+ {
112
+ $ this ->setExtraHttpHeaders (['Cookie ' => http_build_query ($ cookies , null , '; ' )]);
113
+
114
+ return $ this ;
115
+ }
116
+
110
117
public function setExtraHttpHeaders (array $ extraHTTPHeaders )
111
118
{
112
119
$ this ->setOption ('extraHTTPHeaders ' , $ extraHTTPHeaders );
@@ -586,7 +593,6 @@ protected function getFullCommand(array $command)
586
593
.$ nodeBinary .' '
587
594
.escapeshellarg ($ binPath ).' '
588
595
.escapeshellarg (json_encode ($ command ));
589
-
590
596
}
591
597
592
598
protected function getNodePathCommand (string $ nodeBinary ): string
Original file line number Diff line number Diff line change @@ -778,6 +778,31 @@ public function it_can_send_extra_http_headers()
778
778
], $ command );
779
779
}
780
780
781
+ /**
782
+ * @test
783
+ */
784
+ public function it_can_send_cookie_in_http_headers ()
785
+ {
786
+ $ command = Browsershot::url ('https://example.com ' )
787
+ ->useCookies (['theme ' => 'light ' , 'sessionToken ' => 'abc123 ' ])
788
+ ->createScreenshotCommand ('screenshot.png ' );
789
+
790
+ $ this ->assertEquals ([
791
+ 'url ' => 'https://example.com ' ,
792
+ 'action ' => 'screenshot ' ,
793
+ 'options ' => [
794
+ 'extraHTTPHeaders ' => ['Cookie ' => 'theme=light; sessionToken=abc123 ' ],
795
+ 'path ' => 'screenshot.png ' ,
796
+ 'viewport ' => [
797
+ 'width ' => 800 ,
798
+ 'height ' => 600 ,
799
+ ],
800
+ 'args ' => [],
801
+ 'type ' => 'png ' ,
802
+ ],
803
+ ], $ command );
804
+ }
805
+
781
806
/** @test */
782
807
public function it_can_click_on_the_page ()
783
808
{
You can’t perform that action at this time.
0 commit comments