Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add evaluate() function #181

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3e68fbe
Add evaluate() function
darron1217 Apr 13, 2018
572e8da
Fix styleci error
darron1217 Apr 13, 2018
de3c887
Fix createEvaluationCommand logic
darron1217 Apr 13, 2018
9ec8cb4
Fix test errors (after adding type)
darron1217 Apr 13, 2018
51d6d1f
Remove test using_image_package + Add screenshot type test
darron1217 Apr 13, 2018
6a4cdfa
Restore original test script
darron1217 Apr 16, 2018
bbab732
Simplify browser.js code
darron1217 Apr 16, 2018
694f1f8
Add support for base64 result on evalute() function
darron1217 Apr 16, 2018
55671cf
Remove base64_decode on evaluate result
darron1217 Apr 16, 2018
d33189d
Implement getOutput() on browser.js
darron1217 Apr 16, 2018
a070843
Add evaluate() test
darron1217 Apr 18, 2018
47a6b6a
Fix test error caused by PR #180 (#184)
darron1217 Apr 18, 2018
f04a950
Fix evaluate() test error
darron1217 Apr 18, 2018
baa7042
Add evaluate() function
darron1217 Apr 13, 2018
183d0b5
Fix styleci error
darron1217 Apr 13, 2018
b758e9c
Fix createEvaluationCommand logic
darron1217 Apr 13, 2018
20bb805
Restore original test script
darron1217 Apr 16, 2018
77b1e2f
Simplify browser.js code
darron1217 Apr 16, 2018
0bc942e
Add support for base64 result on evalute() function
darron1217 Apr 16, 2018
f9ed104
Remove base64_decode on evaluate result
darron1217 Apr 16, 2018
8d5b6e9
Implement getOutput() on browser.js
darron1217 Apr 16, 2018
a0b5e65
Add evaluate() test
darron1217 Apr 18, 2018
0441da1
Fix evaluate() test error
darron1217 Apr 18, 2018
51e614e
Merge branch 'func-evaluate' of https://github.com/darron1217/browser…
darron1217 Apr 18, 2018
90642b9
Resolve conflict
darron1217 Apr 18, 2018
c1da6e7
Fix styleci error
darron1217 Apr 13, 2018
5ce1663
Fix createEvaluationCommand logic
darron1217 Apr 13, 2018
817414f
Test merge
darron1217 Apr 18, 2018
e890c4f
Remove diff string
darron1217 Apr 18, 2018
451d0ce
Simplify browser.js code
darron1217 Apr 16, 2018
c51da57
Add support for base64 result on evalute() function
darron1217 Apr 16, 2018
1003b5e
Remove base64_decode on evaluate result
darron1217 Apr 16, 2018
32438f6
Implement getOutput() on browser.js
darron1217 Apr 16, 2018
bc963d3
Fix evaluate() test error
darron1217 Apr 18, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -386,6 +386,16 @@ Browsershot also can get the body of an html page after JavaScript has been exec
Browsershot::url('https://example.com')->bodyHtml(); // returns the html of the body
```

### Evaluate

Browsershot can get the evaluation of an html page:

```php
Browsershot::url('https://example.com')
->deviceScaleFactor(2)
->evaluate("window.devicePixelRatio"); // returns 2
```

### Misc

#### Setting an arbitrary option
Expand Down
18 changes: 16 additions & 2 deletions bin/browser.js
Expand Up @@ -2,6 +2,20 @@ const puppeteer = require('puppeteer');

const request = JSON.parse(process.argv[2]);

const getOutput = async (page, request) => {
let output;

if (request.action == 'evaluate') {
output = await page.evaluate(request.options.pageFunction);

return output;
}

output = await page[request.action](request.options);

return output.toString('base64');
};

const callChrome = async () => {
let browser;
let page;
Expand Down Expand Up @@ -73,10 +87,10 @@ const callChrome = async () => {
request.options.clip = await element.boundingBox();
}

output = await page[request.action](request.options);
output = await getOutput(page, request);

if (!request.options.path) {
console.log(output.toString('base64'));
console.log(output);
}

await browser.close();
Expand Down
20 changes: 19 additions & 1 deletion src/Browsershot.php
Expand Up @@ -414,6 +414,13 @@ public function savePdf(string $targetPath)
}
}

public function evaluate(string $pageFunction): string
{
$command = $this->createEvaluateCommand($pageFunction);

return $this->callBrowser($command);
}

public function applyManipulations(string $imagePath)
{
Image::load($imagePath)
Expand Down Expand Up @@ -471,6 +478,17 @@ public function createPdfCommand($targetPath = null): array
return $command;
}

public function createEvaluateCommand(string $pageFunction): array
{
$url = $this->html ? $this->createTemporaryHtmlFile() : $this->url;

$options = [
'pageFunction' => $pageFunction,
];

return $this->createCommand($url, 'evaluate', $options);
}

protected function getOptionArgs(): array
{
$args = [];
Expand Down Expand Up @@ -537,7 +555,7 @@ protected function callBrowser(array $command)
$process->run();

if ($process->isSuccessful()) {
return $process->getOutput();
return rtrim($process->getOutput());
}

if ($process->getExitCode() === 2) {
Expand Down
9 changes: 9 additions & 0 deletions tests/BrowsershotTest.php
Expand Up @@ -814,4 +814,13 @@ public function it_can_click_on_the_page()
],
], $command);
}

/** @test */
public function it_can_evaluate_page()
{
$result = Browsershot::url('https://example.com')
->evaluate('1 + 1');

$this->assertEquals('2', $result);
}
}