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

feat: add PANTHER_REDUCED_MOTION to limit animations #651

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.2.0
-----

* Add a `PANTHER_NO_REDUCED_MOTION` environment variable to instruct the website to disable the reduction of non-essential movement

2.1.2
-----

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

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

Expand Down
5 changes: 5 additions & 0 deletions src/ProcessManager/ChromeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ private function getDefaultArguments(): array
$args[] = '--no-sandbox';
}

// Prefer reduced motion, see https://developer.mozilla.org/fr/docs/Web/CSS/@media/prefers-reduced-motion
dunglas marked this conversation as resolved.
Show resolved Hide resolved
if (!filter_var($_SERVER['PANTHER_NO_REDUCED_MOTION'] ?? false, \FILTER_VALIDATE_BOOLEAN)) {
$args[] = '--force-prefers-reduced-motion';
}

// Add custom arguments with PANTHER_CHROME_ARGUMENTS
if ($_SERVER['PANTHER_CHROME_ARGUMENTS'] ?? false) {
$arguments = explode(' ', $_SERVER['PANTHER_CHROME_ARGUMENTS']);
Expand Down
10 changes: 10 additions & 0 deletions src/ProcessManager/FirefoxManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Symfony\Component\Panther\ProcessManager;

use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriver;
Expand Down Expand Up @@ -64,6 +65,15 @@ public function start(): WebDriver
$capabilities = DesiredCapabilities::firefox();
$capabilities->setCapability('moz:firefoxOptions', $firefoxOptions);

// Prefer reduced motion, see https://developer.mozilla.org/fr/docs/Web/CSS/@media/prefers-reduced-motion
if (!filter_var($_SERVER['PANTHER_NO_REDUCED_MOTION'] ?? false, \FILTER_VALIDATE_BOOLEAN)) {
/** @var FirefoxOptions|array $firefoxOptions */
$firefoxOptions = $capabilities->getCapability('moz:firefoxOptions') ?? [];
$firefoxOptions = $firefoxOptions instanceof FirefoxOptions ? $firefoxOptions->toArray() : $firefoxOptions;
$firefoxOptions['prefs']['ui.prefersReducedMotion'] = 1;
$capabilities->setCapability('moz:firefoxOptions', $firefoxOptions);
}

foreach ($this->options['capabilities'] as $capability => $value) {
$capabilities->setCapability($capability, $value);
}
Expand Down
33 changes: 33 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Symfony\Component\Panther\Tests;

use Facebook\WebDriver\Exception\ElementClickInterceptedException;
use Facebook\WebDriver\Exception\InvalidSelectorException;
use Facebook\WebDriver\Exception\StaleElementReferenceException;
use Facebook\WebDriver\Exception\TimeoutException;
Expand Down Expand Up @@ -577,4 +578,36 @@ public function testCreateHttpBrowserClientWithInvalidHttpClientOptions(): void
'http_client_options' => 'bad http client option data type',
]);
}

/**
* @dataProvider providePrefersReducedMotion
*/
public function testPrefersReducedMotion(string $browser): void
{
$client = self::createPantherClient(['browser' => $browser]);
$client->request('GET', '/prefers-reduced-motion.html');

$client->clickLink('Click me!');
$this->assertStringEndsWith('#clicked', $client->getCurrentURL());
}

/**
* @dataProvider providePrefersReducedMotion
*/
public function testPrefersReducedMotionDisabled(string $browser): void
{
$this->expectException(ElementClickInterceptedException::class);

$_SERVER['PANTHER_NO_REDUCED_MOTION'] = true;
$client = self::createPantherClient(['browser' => $browser]);
$client->request('GET', '/prefers-reduced-motion.html');

$client->clickLink('Click me!');
}

public function providePrefersReducedMotion(): iterable
{
yield 'Chrome' => [PantherTestCase::CHROME];
yield 'Firefox' => [PantherTestCase::FIREFOX];
}
}
38 changes: 38 additions & 0 deletions tests/fixtures/prefers-reduced-motion.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WaitFor</title>
<style>
#overlay {
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
animation: animation 5s forwards;
}

@keyframes animation {
from {
opacity: 1;
}
to {
opacity: 0;
visibility: hidden;
}
}

@media (prefers-reduced-motion: reduce) {
#overlay {
animation: none;
opacity: 0;
visibility: hidden;
}
}
</style>
</head>
<body>
<div id="overlay"></div>
<a href="#clicked">Click me!</a>
</body>
</html>
Loading