function HttpKernelUiHelperTrait::clickLink

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/HttpKernelUiHelperTrait.php \Drupal\Tests\HttpKernelUiHelperTrait::clickLink()

Follows a link by complete name.

Will click the first link found with this link text unless $index is specified.

If the link is not found, an assertion will fail, halting the test.

Parameters

string|\Stringable $label: Text between the anchor tags.

int $index: (optional) The index number for cases where multiple links have the same text. Defaults to 0.

File

core/tests/Drupal/Tests/HttpKernelUiHelperTrait.php, line 90

Class

HttpKernelUiHelperTrait
Provides UI helper methods using the HTTP kernel to make requests.

Namespace

Drupal\Tests

Code

protected function clickLink(string|\Stringable $label, int $index = 0) : void {
  $label = (string) $label;
  $links = $this->getSession()
    ->getPage()
    ->findAll('named', [
    'link',
    $label,
  ]);
  $this->assertArrayHasKey($index, $links, 'The link ' . $label . ' was not found on the page.');
  // Use static::drupalGet() rather than the click() method on the element,
  // because that will not produce HTML debug output.
  $this->drupalGet($links[$index]->getAttribute('href'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.