diff -u b/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php b/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php --- b/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php +++ b/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php @@ -35,6 +35,18 @@ '#default_value' => 'Test name', ]; + $form['checkbox_enabled'] = [ + '#type' => 'checkbox', + '#title' => 'Checkbox enabled', + '#default_value' => TRUE, + ]; + + $form['checkbox_disabled'] = [ + '#type' => 'checkbox', + '#title' => 'Checkbox disabled', + '#default_value' => FALSE, + ]; + $form['description'] = [ '#type' => 'textfield', '#title' => 'Description', diff -u b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php --- b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -534,20 +534,13 @@ * (optional) A message to display with the assertion. Do not translate * messages with t(). * - * @throws \Behat\Mink\Exception\ExpectationException - * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->xpath() instead and check the values directly in the test. */ protected function assertFieldByXPath($xpath, $value = NULL, $message = '') { - try { - $fields = $this->xpath($xpath); + $fields = $this->xpath($xpath); - $this->assertFieldsByValue($fields, $value, $message); - } - catch (\PHPUnit_Framework_ExpectationFailedException $e) { - throw new ExpectationException($e->getMessage(), $this->getSession()->getDriver()); - } + $this->assertFieldsByValue($fields, $value, $message); } /** @@ -568,25 +561,30 @@ * Use $this->xpath() instead and assert that the result is empty. */ protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') { - try { - $fields = $this->xpath($xpath); + $fields = $this->xpath($xpath); - // If value specified then check array for match. - $found = TRUE; + if (!empty($fields)) { if (isset($value)) { $found = FALSE; - if ($fields) { - foreach ($fields as $field) { - if ($field->getAttribute('value') == $value) { - $found = TRUE; - } - } + try { + $this->assertFieldsByValue($fields, $value); + $found = TRUE; + } + catch (\Exception $e) { + } + + if ($found) { + throw new ExpectationException(new FormattableMarkup('The field resulting from :xpath was found with the provided value :value.', [ + ':xpath' => $xpath, + ':value' => $value, + ]), $this->getSession()->getDriver()); } } - $this->assertFalse($fields && $found, $message); - } - catch (\PHPUnit_Framework_ExpectationFailedException $e) { - throw new ExpectationException($e->getMessage(), $this->getSession()->getDriver()); + else { + throw new ExpectationException(new FormattableMarkup('The field resulting from :xpath was found.', [ + ':xpath' => $xpath, + ]), $this->getSession()->getDriver()); + } } } @@ -613,11 +611,11 @@ $found = FALSE; if ($fields) { foreach ($fields as $field) { - if ($field->getAttribute('value') == $value) { - // Input element with correct value. - $found = TRUE; + if ($field->getAttribute('type') == 'checkbox') { + // Checkbox with the correct checked state. + $found = $field->isChecked() == $value; } - elseif ($field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) { + elseif ($field->getTagName() == 'select' && $field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) { // Select element with an option. $found = TRUE; } @@ -625,6 +623,10 @@ // Text area with correct text. $found = TRUE; } + elseif ($field->getAttribute('value') == $value) { + // Input element with correct value. + $found = TRUE; + } } } } diff -u b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php --- b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -197,6 +197,32 @@ $this->pass('The "name" field, with no value was found.'); } + $this->assertFieldByName('checkbox_enabled', TRUE); + $this->assertFieldByName('checkbox_disabled', FALSE); + + $this->assertNoFieldByName('checkbox_enabled', FALSE); + $this->assertNoFieldByName('checkbox_disabled', TRUE); + + try { + $this->assertFieldByName('checkbox_enabled', FALSE); + $this->fail('The checked "checkbox_enableRd" field was not found.'); + + } + catch (\PHPUnit_Framework_ExpectationFailedException $e) { + $this->pass('The checked "checkbox_enabled" field was found.'); + } + + try { + $this->assertNoFieldByName('checkbox_enabled', TRUE); + $this->fail('The checked "checkbox_enabled" field was not found.'); + } + catch (ExpectationException $e) { + $this->pass('The checked "checkbox_enabled" field was found.'); + } + catch (\PHPUnit_Framework_ExpectationFailedException $e) { + $this->pass('The checked "checkbox_enabled" field was found.'); + } + $this->assertNoFieldByXPath('//notexisting'); $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value'); @@ -233,7 +259,7 @@ $this->assertFieldById('edit-name'); $this->fail('The "edit-name" field with no value was found.'); } - catch (ExpectationException $e) { + catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass('The "edit-name" field with no value was not found.'); } @@ -242,7 +268,7 @@ $this->assertFieldById('edit-name', 'not the value'); $this->fail('The "name" field was found.'); } - catch (ExpectationException $e) { + catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass('The "name" field was not found.'); } @@ -298,7 +324,7 @@ $this->assertFieldById('Save', NULL); $this->fail('The field with id of "Save" was found.'); } - catch (ExpectationException $e) { + catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass($e->getMessage()); }