Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Here are the options available for the browser factory:
| `connectionDelay` | `0` | Delay to apply between each operation for debugging purposes |
| `customFlags` | none | An array of flags to pass to the command line. Eg: `['--option1', '--option2=someValue']` |
| `debugLogger` | `null` | A string (e.g "php://stdout"), or resource, or PSR-3 logger instance to print debug messages |
| `disableJavascript` | `false` | Disable JavaScript execution on every page created by the browser |
| `disableNotifications` | `false` | Disable browser notifications |
| `enableImages` | `true` | Toggles loading of images |
| `envVariables` | none | An array of environment variables to pass to the process (example DISPLAY variable) |
Expand Down
20 changes: 20 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class Browser
*/
protected $pagePreScript;

/**
* Whether JavaScript execution must be disabled on every new page.
*
* @var bool
*/
protected $pageScriptExecutionDisabled = false;

public function __construct(Connection $connection)
{
$this->connection = $connection;
Expand Down Expand Up @@ -103,6 +110,14 @@ public function setPagePreScript(?string $script = null): void
$this->pagePreScript = $script;
}

/**
* Disable or enable JavaScript execution for every new page created by this browser.
*/
public function setPageScriptExecutionDisabled(bool $disabled = true): void
{
$this->pageScriptExecutionDisabled = $disabled;
}

/**
* Closes the browser.
*
Expand Down Expand Up @@ -258,6 +273,11 @@ public function getPage($targetId)
$page->addPreScript($this->pagePreScript);
}

// disable javascript execution if requested at the browser level
if ($this->pageScriptExecutionDisabled) {
$page->setScriptExecution(false)->await();
}

$this->pages[$targetId] = $page;

return $page;
Expand Down
5 changes: 5 additions & 0 deletions src/Browser/BrowserProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public function start($binary, $options): void

// create browser instance
$this->browser = new ProcessAwareBrowser($connection, $this);

// disable javascript execution for new pages if requested
if (\array_key_exists('disableJavascript', $options) && (true === $options['disableJavascript'])) {
$this->browser->setPageScriptExecutionDisabled(true);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/BrowserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BrowserFactory
* - connectionDelay: Delay to apply between each operation for debugging purposes (default: none)
* - customFlags: An array of flags to pass to the command line.
* - debugLogger: A string (e.g "php://stdout"), or resource, or PSR-3 logger instance to print debug messages (default: none)
* - disableJavascript: Disable JavaScript execution on every page created by the browser (default: false)
* - disableNotifications: Disable browser notifications (default: false)
* - enableImages: Toggles loading of images (default: true)
* - envVariables: An array of environment variables to pass to the process (example DISPLAY variable)
Expand Down
32 changes: 32 additions & 0 deletions tests/BrowserFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,38 @@ public function testOptions(): void
self::assertSame([], $factory->getOptions());
}

public function testDisableJavascriptOption(): void
{
$factory = new BrowserFactory();

$browser = $factory->createBrowser([
'disableJavascript' => true,
]);

$page = $browser->createPage();
$page->navigate(self::sitePath('javascript.html'))->waitForNavigation();

self::assertSame(
'javascript disabled',
$page->evaluate('document.body.innerText')->getReturnValue()
);
}

public function testDisableJavascriptOptionDefault(): void
{
$factory = new BrowserFactory();

$browser = $factory->createBrowser();

$page = $browser->createPage();
$page->navigate(self::sitePath('javascript.html'))->waitForNavigation();

self::assertSame(
'javascript enabled',
$page->evaluate('document.body.innerText')->getReturnValue()
);
}

public function testConnectToBrowser(): void
{
// create a browser
Expand Down