Skip to content
Snippets Groups Projects

Draft: Issue #3338664: Automated Axe tests in PHPUnit

Adds Axe testing capability to PHPUnit with support for axe.run() options and the reporting of errors thrown by Axe.

Closes #3338664

Merge request reports

Members who can merge are allowed to add commits.
Code Quality is loading
Test summary results are being parsed
Metrics reports are loading
Ready to merge by members who can write to the target branch.
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • 32 /**
    33 * A user with permission to access all admin pages & functionality.
    34 *
    35 * @var \Drupal\user\UserInterface
    36 */
    37 protected UserInterface $user;
    38
    39 /**
    40 * {@inheritdoc}
    41 */
    42 protected function setUp(): void {
    43 parent::setUp();
    44
    45 // Create an admin user and log in.
    46 $this->user = $this->drupalCreateUser(admin: TRUE);
    47 $this->drupalLogin($this->user);
  • Michael Strelan
  • Michael Strelan
  • Michael Strelan
  • Michael Strelan
  • 167 $exception->getMessage()
    168 );
    169 // If above assertion passes, it was an unexpected error.
    170 // Re-throw so PHPUnit can handle it.
    171 throw $exception;
    172 }
    173 }
    174
    175 /**
    176 * Data provider.
    177 *
    178 * @return \Generator
    179 * Test scenarios.
    180 */
    181 public static function providerAxeCoreTestTraitWithOptions(): \Generator {
    182 // The global $base_url isn't available here for some reason.
    • The data provider is evaluated before any test setup, so phpunit doesn't know about the Drupal site. It might be best to put a placeholder in the data provider and replace it in the test case, in case for some reason the env var doesn't match the global.

    • Please register or sign in to reply
  • 1 <?php
    2
    3 declare(strict_types=1);
    4
    5 namespace Drupal\Tests;
    6
    7 /**
    8 * Provides methods to run axe-core tests in the WebDriver.
    9 *
    10 * This trait is meant to be used only by Functional Javascript test classes.
  • 31 * 'rules' => [
    32 * 'region' => ['enabled' => false]
    33 * ]
    34 * ]
    35 * ```.
    36 *
    37 * @throws \Behat\Mink\Exception\DriverException
    38 * @throws \Behat\Mink\Exception\UnsupportedDriverActionException
    39 *
    40 * @see https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#options-parameter
    41 */
    42 protected function executeAxe(?array $options = NULL): void {
    43 $session = $this->getSession();
    44
    45 // Load axe-core script.
    46 $axe_src = file_get_contents(DRUPAL_ROOT . '/core/node_modules/axe-core/axe.min.js');
  • 56 // Analyze page.
    57 axe
    58 .run('body', {$options_json})
    59 .then((results) => {
    60 window.axe_results = results;
    61 })
    62 .catch((e) => {
    63 if (window.axe_errors) {
    64 window.axe_errors.push(e.message);
    65 } else {
    66 window.axe_errors = [e.message];
    67 }
    68 });
    69 JS);
    70
    71 $session->wait(1000, 'window.axe_results !== undefined || window.axe_errors !== undefined');
  • Kent Richards marked this merge request as draft

    marked this merge request as draft

  • Kent Richards added 1 commit

    added 1 commit

    • a97fabe5 - Apply 1 suggestion(s) to 1 file(s)

    Compare with previous version

  • Kent Richards added 1 commit

    added 1 commit

    • cbc45635 - Apply 8 suggestion(s) to 4 file(s)

    Compare with previous version

  • Kent Richards added 6 commits

    added 6 commits

    • a409f9ca - Fix more cases of nullable type syntax
    • d10d35d8 - Use array as default parameter value
    • 28d5f756 - Convert to axe execution to async/await
    • 1a72bb91 - Expect single Axe error instead of multiple
    • 82cedd16 - Add comment
    • e71d9cb4 - Remove $user properties

    Compare with previous version

  • 48 * @param array|null $browser_options
    49 * (optional) Associative array of browser options.
    50 */
    51 #[DataProvider('providerTestAdminPages')]
    52 public function testAdminPages(string $uri, array $axe_options = [], ?array $browser_options = NULL): void {
    53 $this->drupalGet($uri);
    54 if (isset($browser_options['window_size'])) {
    55 $this->getSession()->resizeWindow($browser_options['window_size']['width'], $browser_options['window_size']['height']);
    56 }
    57 $this->executeAxe($axe_options);
    58 }
    59
    60 /**
    61 * Data provider for testAdminPages.
    62 *
    63 * @return array
  • 33 *
    34 * @param string $uri
    35 * The path to be tested.
    36 * @param array|null $options
    37 * (optional) Associative array of Axe options.
    38 */
    39 #[DataProvider('providerTestAnonymousPages')]
    40 public function testAnonymousPages(string $uri, array $options = []): void {
    41 $this->drupalGet($uri);
    42 $this->executeAxe($options);
    43 }
    44
    45 /**
    46 * Data provider for testAnonymousPages.
    47 *
    48 * @return array
    Please register or sign in to reply
    Loading