Skip to content

Commit 5961e95

Browse files
committed
screenshot clip + several bugfixes [ci skip]
1 parent 42c88be commit 5961e95

File tree

6 files changed

+68
-23
lines changed

6 files changed

+68
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ to crawl websites... and almost everything that you can do with chrome as a huma
6161
// get page title
6262
$pageTitle = $page->evaluate('document.title')->getReturnValue();
6363

64-
// screenshot - Say "Cheese"!
64+
// screenshot - Say "Cheese"! 😄
6565
$page->screenshot()->saveToFile('/foo/bar.png');
6666

6767
// bye

src/Communication/ResponseReader.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace HeadlessChromium\Communication;
77

88
use HeadlessChromium\Exception\NoResponseAvailable;
9+
use HeadlessChromium\Utils;
10+
use HeadlessChromium\Exception\OperationTimedOut;
911

1012
class ResponseReader
1113
{
@@ -75,7 +77,7 @@ public function getConnection(): Connection
7577
public function getResponse(): Response
7678
{
7779
if (!$this->response) {
78-
throw new NoResponseAvailable('Response is not available');
80+
throw new NoResponseAvailable('Response is not available. Try to use the method waitForResponse instead.');
7981
}
8082

8183
return $this->response;
@@ -84,26 +86,37 @@ public function getResponse(): Response
8486
/**
8587
* Wait for a response
8688
* @param int $timeout time to wait for a response (milliseconds)
87-
* @return Response|null the response or null if no responses were found before the given timeout is reached
89+
* @return Response
90+
*
91+
* @throws NoResponseAvailable
92+
* @throws OperationTimedOut
8893
*/
89-
public function waitForResponse(int $timeout = null)
94+
public function waitForResponse(int $timeout = null): Response
9095
{
91-
9296
if ($this->hasResponse()) {
9397
return $this->getResponse();
9498
}
9599

96100
// default 2000ms
97101
$timeout = $timeout ?? 2000;
98102

99-
// 10 microseconds between each iteration
100-
$tryDelay = 10;
103+
// TODO replace with Utils::tryWithTimeout
104+
return Utils::tryWithTimeout($timeout * 1000, $this->waitForResponseGenerator());
105+
}
106+
107+
/**
108+
* To be used in waitForResponse method
109+
* @return \Generator|Response
110+
* @throws NoResponseAvailable
111+
* @internal
112+
*/
113+
private function waitForResponseGenerator()
114+
{
115+
while (true) {
101116

102-
// time to wait for the response
103-
$waitUntil = microtime(true) + $timeout / 1000;
117+
// 50 microseconds between each iteration
118+
$tryDelay = 50;
104119

105-
// TODO replace with Utils::tryWithTimeout
106-
do {
107120
// read available response
108121
$hasResponse = $this->checkForResponse();
109122

@@ -113,10 +126,8 @@ public function waitForResponse(int $timeout = null)
113126
}
114127

115128
// wait before next check
116-
usleep($tryDelay);
117-
} while (microtime(true) < $waitUntil);
118-
119-
return null;
129+
yield $tryDelay;
130+
}
120131
}
121132

122133
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* @license see LICENSE
4+
*/
5+
6+
namespace HeadlessChromium\Exception;
7+
8+
9+
class JavascriptException extends \Exception
10+
{
11+
12+
}

src/Page.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ public function screenshot(array $options = []): PageScreenshot
182182
$screenshotOptions['quality'] = $options['quality'];
183183
}
184184

185+
// TODO document clip
186+
// TODO validate clip/use viewport object
187+
if (array_key_exists('clip', $options)) {
188+
$screenshotOptions['clip'] = $options['clip'];
189+
}
190+
185191
// request screen shot
186192
$responseReader = $this->getSession()
187193
->sendMessage(new Message('Page.captureScreenshot', $screenshotOptions));

src/PageEvaluation.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use HeadlessChromium\Communication\Response;
99
use HeadlessChromium\Communication\ResponseReader;
1010
use HeadlessChromium\Exception\EvaluationFailed;
11+
use HeadlessChromium\Exception\JavascriptException;
1112

1213
/**
1314
* Used to read data from page evaluation response
@@ -41,13 +42,20 @@ public function __construct(ResponseReader $responseReader)
4142
*/
4243
public function waitForResponse()
4344
{
44-
$response = $this->responseReader->waitForResponse();
45+
$this->response = $this->responseReader->waitForResponse();
4546

4647
if (!$this->response->isSuccessful()) {
4748
throw new EvaluationFailed('Could not evaluate the script in the page.');
4849
}
4950

50-
$this->response = $response;
51+
$result = $this->response->getResultData('result');
52+
53+
$resultSubType = $result['subtype'] ?? null;
54+
55+
if ($resultSubType == 'error') {
56+
// TODO dump javascript trace
57+
throw new JavascriptException('Error during javascript evaluation: ' . $result['description']);
58+
}
5159

5260
return $this;
5361
}
@@ -63,7 +71,7 @@ public function getReturnValue()
6371
$this->waitForResponse();
6472
}
6573

66-
return $this->response->getResultData('value');
74+
return $this->response->getResultData('result')['value'] ?? null;
6775
}
6876

6977
/**
@@ -77,6 +85,6 @@ public function getReturnType()
7785
$this->waitForResponse();
7886
}
7987

80-
return $this->response->getResultData('type');
88+
return $this->response->getResultData('result')['type'] ?? null;
8189
}
8290
}

src/PageScreenshot.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,18 @@ public function saveToFile(string $path)
7171
}
7272

7373
// save screenshot
74-
if (!is_writable($path)) {
75-
throw new FilesystemException(
76-
sprintf('The file %s is not writable.', $path)
77-
);
74+
if (file_exists($path)) {
75+
if (!is_writable($path)) {
76+
throw new FilesystemException(
77+
sprintf('The file %s is not writable.', $path)
78+
);
79+
}
80+
} else {
81+
if (!touch($path)) {
82+
throw new FilesystemException(
83+
sprintf('The file %s could not be created.', $path)
84+
);
85+
}
7886
}
7987

8088
$file = fopen($path, 'wb');

0 commit comments

Comments
 (0)