function BrowserTestBaseTest::testFieldAssertsForTextfields

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testFieldAssertsForTextfields()
  2. 8.9.x core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testFieldAssertsForTextfields()
  3. 11.x core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testFieldAssertsForTextfields()

Tests field asserts using textfields.

File

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

Class

BrowserTestBaseTest
Tests BrowserTestBase functionality.

Namespace

Drupal\FunctionalTests

Code

public function testFieldAssertsForTextfields() : void {
    $this->drupalGet('test-field-xpath');
    // *** 1. fieldNotExists().
    $this->assertSession()
        ->fieldNotExists('invalid_name_and_id');
    // Test that the assertion fails correctly when searching by name.
    try {
        $this->assertSession()
            ->fieldNotExists('name');
        $this->fail('The "name" field was not found based on name.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // Test that the assertion fails correctly when searching by id.
    try {
        $this->assertSession()
            ->fieldNotExists('edit-name');
        $this->fail('The "name" field was not found based on id.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // *** 2. fieldExists().
    $this->assertSession()
        ->fieldExists('name');
    $this->assertSession()
        ->fieldExists('edit-name');
    // Test that the assertion fails correctly if the field does not exist.
    try {
        $this->assertSession()
            ->fieldExists('invalid_name_and_id');
        $this->fail('The "invalid_name_and_id" field was found.');
    } catch (ElementNotFoundException $e) {
        // Expected exception; just continue testing.
    }
    // *** 3. assertNoFieldById().
    $this->assertSession()
        ->fieldValueNotEquals('name', 'not the value');
    $this->assertSession()
        ->fieldNotExists('notexisting');
    // Test that the assertion fails correctly if no value is passed in.
    try {
        $this->assertSession()
            ->fieldNotExists('edit-description');
        $this->fail('The "description" field, with no value was not found.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // Test that the assertion fails correctly if a NULL value is passed in.
    try {
        $this->assertSession()
            ->fieldNotExists('name', NULL);
        $this->fail('The "name" field was not found.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // *** 4. assertFieldById().
    $this->assertSession()
        ->fieldExists('edit-name');
    $this->assertSession()
        ->fieldValueEquals('edit-name', 'Test name');
    $this->assertSession()
        ->fieldExists('edit-description');
    $this->assertSession()
        ->fieldValueEquals('edit-description', '');
    // Test that the assertion fails correctly if no value is passed in.
    try {
        $this->assertSession()
            ->fieldValueNotEquals('edit-name', '');
    } catch (ExpectationFailedException $e) {
        // Expected exception; just continue testing.
    }
    // Test that the assertion fails correctly if the wrong value is passed in.
    try {
        $this->assertSession()
            ->fieldValueNotEquals('edit-name', 'not the value');
    } catch (ExpectationFailedException $e) {
        // Expected exception; just continue testing.
    }
    // *** 5. fieldValueNotEquals().
    $this->assertSession()
        ->fieldValueNotEquals('name', 'not the value');
    // Test that the assertion fails correctly if given the right value.
    try {
        $this->assertSession()
            ->fieldValueNotEquals('name', 'Test name');
        $this->fail('fieldValueNotEquals failed to throw an exception.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // *** 6. fieldValueEquals().
    $this->assertSession()
        ->fieldValueEquals('name', 'Test name');
    $this->assertSession()
        ->fieldValueEquals('description', '');
    // Test that the assertion fails correctly if given the wrong value.
    try {
        $this->assertSession()
            ->fieldValueEquals('name', 'not the value');
        $this->fail('fieldValueEquals failed to throw an exception.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // Test that text areas can contain new lines.
    $this->assertSession()
        ->fieldValueEquals('edit-test-textarea-with-newline', "Test text with\nnewline");
}

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