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 5 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
9 changes: 7 additions & 2 deletions bin/browser.js
Expand Up @@ -73,10 +73,15 @@ const callChrome = async () => {
request.options.clip = await element.boundingBox();
}

output = await page[request.action](request.options);
if(request.action == 'evaluate') {
output = await page.evaluate(request.options.pageFunction);
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor this, so we don't need an else statement?

First thought: let's introduce a getOutput function. You can use an early return in that function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain more about getOutput?

You mean request.action == 'getOutput'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed explicit base64 encoding code.

But I can't make it more simpler because passing request.options can't handle evaluate() function which needs string argument

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That else statement can be avoided by introducing a new function getOutput(request) that returns the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented it on d33189d
Is it the function you said?

output = await page[request.action](request.options);
output = output.toString('base64')
}

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
42 changes: 28 additions & 14 deletions tests/BrowsershotTest.php
Expand Up @@ -162,20 +162,6 @@ public function it_can_handle_a_permissions_error()
->save($targetPath);
}

/** @test */
public function it_can_use_the_methods_of_the_image_package()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this test because method named format() is overrided. So we cannot access to spatie/image package's function.
(I guess format() function is implemented for setting pdf format)

{
$targetPath = __DIR__.'/temp/testScreenshot.jpg';

Browsershot::url('https://example.com')
->format('jpg')
->save($targetPath);

$this->assertFileExists($targetPath);

$this->assertMimeType('image/jpeg', $targetPath);
}

/** @test */
public function it_can_create_a_command_to_generate_a_screenshot()
{
Expand All @@ -201,6 +187,7 @@ public function it_can_create_a_command_to_generate_a_screenshot()
'height' => 1080,
],
'args' => [],
'type' => 'png',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is 'png' added in every test? Can this be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It related to #180

This change is not related with this PR. Should I remove it?
(I just tried to remove Travis Error)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it

],
], $command);
}
Expand Down Expand Up @@ -228,6 +215,7 @@ public function it_can_create_a_command_to_generate_a_screenshot_and_omit_the_ba
'height' => 1080,
],
'args' => [],
'type' => 'png',
],
], $command);
}
Expand Down Expand Up @@ -451,6 +439,7 @@ public function it_can_use_given_user_agent()
],
'userAgent' => 'my_special_snowflake',
'args' => [],
'type' => 'png',
],
], $command);
}
Expand All @@ -473,6 +462,7 @@ public function it_can_set_emulate_media_option()
],
'emulateMedia' => 'screen',
'args' => [],
'type' => 'png',
],
], $command);
}
Expand All @@ -495,6 +485,7 @@ public function it_can_set_emulate_media_option_to_null()
],
'emulateMedia' => null,
'args' => [],
'type' => 'png',
],
], $command);
}
Expand Down Expand Up @@ -566,6 +557,7 @@ public function it_can_run_without_sandbox()
'args' => [
'--no-sandbox',
],
'type' => 'png',
],
], $command);
}
Expand All @@ -588,6 +580,7 @@ public function it_can_dismiss_dialogs()
'height' => 600,
],
'args' => [],
'type' => 'png',
],
], $command);
}
Expand All @@ -610,6 +603,7 @@ public function it_can_ignore_https_errors()
'height' => 600,
],
'args' => [],
'type' => 'png',
],
], $command);
}
Expand All @@ -631,6 +625,7 @@ public function it_can_use_a_proxy_server()
'height' => 600,
],
'args' => ['--proxy-server=1.2.3.4:8080'],
'type' => 'png',
],
], $command);
}
Expand Down Expand Up @@ -658,6 +653,7 @@ public function it_can_set_arbitrary_options()
'baz' => 200,
],
'args' => [],
'type' => 'png',
],
], $command);
}
Expand Down Expand Up @@ -735,6 +731,7 @@ public function it_can_wait_until_network_idle()
'height' => 600,
],
'args' => [],
'type' => 'png',
],
], $command);

Expand All @@ -753,6 +750,7 @@ public function it_can_wait_until_network_idle()
'height' => 600,
],
'args' => [],
'type' => 'png',
],
], $command);
}
Expand All @@ -775,6 +773,7 @@ public function it_can_send_extra_http_headers()
'height' => 600,
],
'args' => [],
'type' => 'png',
],
], $command);
}
Expand Down Expand Up @@ -811,7 +810,22 @@ public function it_can_click_on_the_page()
'height' => 600,
],
'args' => [],
'type' => 'png',
],
], $command);
}

/** @test */
public function it_can_set_type_of_screenshot()
{
$targetPath = __DIR__.'/temp/testScreenshot.jpg';

Browsershot::url('https://example.com')
->setScreenshotType('jpeg')
->save($targetPath);

$this->assertFileExists($targetPath);

$this->assertMimeType('image/jpeg', $targetPath);
}
}