Skip to content

Commit

Permalink
[1.10] Add setScriptExecution page method (#541)
Browse files Browse the repository at this point in the history
Co-authored-by: Enrico Dias <[email protected]>
  • Loading branch information
osbre and enricodias authored Sep 4, 2023
1 parent f9fcb66 commit 521f7f8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ public function assertNotClosed(): void
}

/**
* Set user agent for the current page.
* Set timezone for the current page.
*
* @see https://source.chromium.org/chromium/chromium/deps/icu.git/+/faee8bc70570192d82d2978a71e2a615788597d1:source/data/misc/metaZones.txt | ICU’s metaZones.txt
*
Expand Down Expand Up @@ -1068,4 +1068,21 @@ public function setUserAgent(string $userAgent)

return new ResponseWaiter($response);
}

/**
* Disable or enable JavaScript execution for the current page.
*
* @throws CommunicationException
*/
public function setScriptExecution(bool $enabled): ResponseWaiter
{
// ensure target is not closed
$this->assertNotClosed();

$response = $this->getSession()->sendMessage(
new Message('Emulation.setScriptExecutionDisabled', ['value' => !$enabled])
);

return new ResponseWaiter($response);
}
}
24 changes: 24 additions & 0 deletions tests/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,28 @@ public function testFindTarget(): void
$target = $browser->findTarget('page', 'bigLayout.html');
self::assertSame('bigLayout.html', $target->getTargetInfo('title'));
}

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

$browser = $factory->createBrowser();
$page = $browser->createPage();

$page->setScriptExecution(false);
$page->navigate($this->sitePath('javascript.html'))->waitForNavigation();

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

$page->setScriptExecution(true);
$page->navigate($this->sitePath('javascript.html'))->waitForNavigation();

self::assertEquals(
'javascript enabled',
$page->evaluate('document.body.innerText')->getReturnValue()
);
}
}
11 changes: 11 additions & 0 deletions tests/resources/static-web/javascript.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>javascript - test</title>
</head>
<body>
<noscript>javascript disabled</noscript>
<script>
document.write('javascript enabled');
</script>
</body>
</html>

0 comments on commit 521f7f8

Please sign in to comment.