You are here

public function BrowserTestBaseTest::testXpathAsserts in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testXpathAsserts()

Tests legacy field asserts which use xpath directly.

File

core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php, line 266

Class

BrowserTestBaseTest
Tests BrowserTestBase functionality.

Namespace

Drupal\FunctionalTests

Code

public function testXpathAsserts() {
  $this
    ->drupalGet('test-field-xpath');
  $this
    ->assertFieldsByValue($this
    ->xpath("//h1[@class = 'page-title']"), NULL);
  $this
    ->assertFieldsByValue($this
    ->xpath('//table/tbody/tr[2]/td[1]'), 'one');
  $this
    ->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one');
  $this
    ->assertFieldsByValue($this
    ->xpath("//input[@id = 'edit-name']"), 'Test name');
  $this
    ->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name');
  $this
    ->assertFieldsByValue($this
    ->xpath("//select[@id = 'edit-options']"), '2');
  $this
    ->assertFieldByXPath("//select[@id = 'edit-options']", '2');
  $this
    ->assertNoFieldByXPath('//notexisting');
  $this
    ->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');

  // Test that the assertion fails correctly.
  try {
    $this
      ->assertFieldByXPath("//input[@id = 'notexisting']");
    $this
      ->fail('The "notexisting" field was found.');
  } catch (ExpectationFailedException $e) {

    // Expected exception; just continue testing.
  }
  try {
    $this
      ->assertNoFieldByXPath("//input[@id = 'edit-name']");
    $this
      ->fail('The "edit-name" field was not found.');
  } catch (ExpectationException $e) {

    // Expected exception; just continue testing.
  }
  try {
    $this
      ->assertFieldsByValue($this
      ->xpath("//input[@id = 'edit-name']"), 'not the value');
    $this
      ->fail('The "edit-name" field is found with the value "not the value".');
  } catch (ExpectationFailedException $e) {

    // Expected exception; just continue testing.
  }
}