Skip to content

Commit 1e8718e

Browse files
Jean-Berudunglas
andauthored
feat: limit animations when possible (#651)
* feat: add PANTHER_REDUCED_MOTION to limit animations * fix legacy array "moz:firefoxOptions" * review * tests and fix * Update ChromeManager.php --------- Co-authored-by: Kévin Dunglas <[email protected]>
1 parent 8449573 commit 1e8718e

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.2.0
5+
-----
6+
7+
* Add a `PANTHER_NO_REDUCED_MOTION` environment variable to instruct the website to disable the reduction of non-essential movement
8+
49
2.1.2
510
-----
611

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ The following environment variables can be set to change some Panther's behavior
366366
* `PANTHER_ERROR_SCREENSHOT_DIR`: to set a base directory for your failure/error screenshots (e.g. `./var/error-screenshots`)
367367
* `PANTHER_DEVTOOLS`: to toggle the browser's dev tools (default `enabled`, useful to debug)
368368
* `PANTHER_ERROR_SCREENSHOT_ATTACH`: to add screenshots mentioned above to test output in junit attachment format
369+
* `PANTHER_NO_REDUCED_MOTION`: to instruct the website to disable the reduction of non-essential movement
369370

370371
### Changing the Hostname and Port of the Built-in Web Server
371372

src/ProcessManager/ChromeManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ private function getDefaultArguments(): array
115115
$args[] = '--no-sandbox';
116116
}
117117

118+
// Prefer reduced motion, see https://developer.mozilla.org/docs/Web/CSS/@media/prefers-reduced-motion
119+
if (!filter_var($_SERVER['PANTHER_NO_REDUCED_MOTION'] ?? false, \FILTER_VALIDATE_BOOLEAN)) {
120+
$args[] = '--force-prefers-reduced-motion';
121+
}
122+
118123
// Add custom arguments with PANTHER_CHROME_ARGUMENTS
119124
if ($_SERVER['PANTHER_CHROME_ARGUMENTS'] ?? false) {
120125
$arguments = explode(' ', $_SERVER['PANTHER_CHROME_ARGUMENTS']);

src/ProcessManager/FirefoxManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Symfony\Component\Panther\ProcessManager;
1515

16+
use Facebook\WebDriver\Firefox\FirefoxOptions;
1617
use Facebook\WebDriver\Remote\DesiredCapabilities;
1718
use Facebook\WebDriver\Remote\RemoteWebDriver;
1819
use Facebook\WebDriver\WebDriver;
@@ -64,6 +65,15 @@ public function start(): WebDriver
6465
$capabilities = DesiredCapabilities::firefox();
6566
$capabilities->setCapability('moz:firefoxOptions', $firefoxOptions);
6667

68+
// Prefer reduced motion, see https://developer.mozilla.org/fr/docs/Web/CSS/@media/prefers-reduced-motion
69+
if (!filter_var($_SERVER['PANTHER_NO_REDUCED_MOTION'] ?? false, \FILTER_VALIDATE_BOOLEAN)) {
70+
/** @var FirefoxOptions|array $firefoxOptions */
71+
$firefoxOptions = $capabilities->getCapability('moz:firefoxOptions') ?? [];
72+
$firefoxOptions = $firefoxOptions instanceof FirefoxOptions ? $firefoxOptions->toArray() : $firefoxOptions;
73+
$firefoxOptions['prefs']['ui.prefersReducedMotion'] = 1;
74+
$capabilities->setCapability('moz:firefoxOptions', $firefoxOptions);
75+
}
76+
6777
foreach ($this->options['capabilities'] as $capability => $value) {
6878
$capabilities->setCapability($capability, $value);
6979
}

tests/ClientTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Symfony\Component\Panther\Tests;
1515

16+
use Facebook\WebDriver\Exception\ElementClickInterceptedException;
1617
use Facebook\WebDriver\Exception\InvalidSelectorException;
1718
use Facebook\WebDriver\Exception\StaleElementReferenceException;
1819
use Facebook\WebDriver\Exception\TimeoutException;
@@ -577,4 +578,36 @@ public function testCreateHttpBrowserClientWithInvalidHttpClientOptions(): void
577578
'http_client_options' => 'bad http client option data type',
578579
]);
579580
}
581+
582+
/**
583+
* @dataProvider providePrefersReducedMotion
584+
*/
585+
public function testPrefersReducedMotion(string $browser): void
586+
{
587+
$client = self::createPantherClient(['browser' => $browser]);
588+
$client->request('GET', '/prefers-reduced-motion.html');
589+
590+
$client->clickLink('Click me!');
591+
$this->assertStringEndsWith('#clicked', $client->getCurrentURL());
592+
}
593+
594+
/**
595+
* @dataProvider providePrefersReducedMotion
596+
*/
597+
public function testPrefersReducedMotionDisabled(string $browser): void
598+
{
599+
$this->expectException(ElementClickInterceptedException::class);
600+
601+
$_SERVER['PANTHER_NO_REDUCED_MOTION'] = true;
602+
$client = self::createPantherClient(['browser' => $browser]);
603+
$client->request('GET', '/prefers-reduced-motion.html');
604+
605+
$client->clickLink('Click me!');
606+
}
607+
608+
public function providePrefersReducedMotion(): iterable
609+
{
610+
yield 'Chrome' => [PantherTestCase::CHROME];
611+
yield 'Firefox' => [PantherTestCase::FIREFOX];
612+
}
580613
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>WaitFor</title>
6+
<style>
7+
#overlay {
8+
position: absolute;
9+
width: 100%;
10+
height: 100%;
11+
background-color: rgba(0, 0, 0, 0.5);
12+
animation: animation 5s forwards;
13+
}
14+
15+
@keyframes animation {
16+
from {
17+
opacity: 1;
18+
}
19+
to {
20+
opacity: 0;
21+
visibility: hidden;
22+
}
23+
}
24+
25+
@media (prefers-reduced-motion: reduce) {
26+
#overlay {
27+
animation: none;
28+
opacity: 0;
29+
visibility: hidden;
30+
}
31+
}
32+
</style>
33+
</head>
34+
<body>
35+
<div id="overlay"></div>
36+
<a href="#clicked">Click me!</a>
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)