You are here

protected function WebTestBase::clickLinkHelper in Drupal 8

Provides a helper for ::clickLink() and ::clickLinkPartialName().

Parameters

string|\Drupal\Component\Render\MarkupInterface $label: Text between the anchor tags, uses starts-with().

int $index: Link position counting from zero.

string $pattern: A pattern to use for the XPath.

Return value

bool|string Page contents on success, or FALSE on failure.

2 calls to WebTestBase::clickLinkHelper()
WebTestBase::clickLink in core/modules/simpletest/src/WebTestBase.php
Follows a link by complete name.
WebTestBase::clickLinkPartialName in core/modules/simpletest/src/WebTestBase.php
Follows a link by partial name.

File

core/modules/simpletest/src/WebTestBase.php, line 1731

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function clickLinkHelper($label, $index, $pattern) {

  // Cast MarkupInterface objects to string.
  $label = (string) $label;
  $url_before = $this
    ->getUrl();
  $urls = $this
    ->xpath($pattern, [
    ':label' => $label,
  ]);
  if (isset($urls[$index])) {
    $url_target = $this
      ->getAbsoluteUrl($urls[$index]['href']);
    $this
      ->pass(new FormattableMarkup('Clicked link %label (@url_target) from @url_before', [
      '%label' => $label,
      '@url_target' => $url_target,
      '@url_before' => $url_before,
    ]), 'Browser');
    return $this
      ->drupalGet($url_target);
  }
  $this
    ->fail(new FormattableMarkup('Link %label does not exist on @url_before', [
    '%label' => $label,
    '@url_before' => $url_before,
  ]), 'Browser');
  return FALSE;
}