diff --git a/core/modules/simpletest/src/WebAssert.php b/core/modules/simpletest/src/WebAssert.php index 063473b..bc7302f 100644 --- a/core/modules/simpletest/src/WebAssert.php +++ b/core/modules/simpletest/src/WebAssert.php @@ -2,6 +2,10 @@ namespace Drupal\simpletest; +use Behat\Mink\Element\Element; +use Behat\Mink\Element\ElementInterface; +use Behat\Mink\Exception\ElementHtmlException; +use Behat\Mink\Exception\ExpectationException; use Behat\Mink\WebAssert as MinkWebAssert; use Behat\Mink\Element\TraversableElement; use Behat\Mink\Exception\ElementNotFoundException; @@ -52,16 +56,149 @@ public function buttonExists($button, TraversableElement $container = NULL) { */ public function selectExists($select, TraversableElement $container = NULL) { $container = $container ?: $this->session->getPage(); - $node = $container->find('named', array( - 'select', - $this->session->getSelectorsHandler()->xpathLiteral($select), - )); - + $node = $container->find('named', ['select', $select]); if ($node === NULL) { throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select); } - return $node; } + /** + * Asserts that the given field is visible. + * + * @param string $locator + * One of id|name|label|value for the field. + * @param \Behat\Mink\Element\TraversableElement $container + * (optional) The document to check against. Defaults to the current page. + * + * @throws \Behat\Mink\Exception\ElementNotFoundException + * When the element doesn't exist. + */ + public function assertFieldVisible($locator, TraversableElement $container = NULL) { + $container = $container ?: $this->session->getPage(); + $field = $container->find('named', ['field', $locator]); + if ($field === NULL) { + throw new ElementNotFoundException($this->session->getDriver(), 'form field', 'id|name|label|value|placeholder', $locator); + } + $this->assert($field->isVisible(), sprintf('Field "%s" is not visible.', $locator)); + } + + /** + * Asserts that the given field is not visible. + * + * @param string $locator + * One of id|name|label|value for the field. + * @param \Behat\Mink\Element\TraversableElement $container + * (optional) The document to check against. Defaults to the current page. + * + * @throws \Behat\Mink\Exception\ElementNotFoundException + * When the element doesn't exist. + */ + public function assertFieldNotVisible($locator, TraversableElement $container = NULL) { + $container = $container ?: $this->session->getPage(); + $field = $container->find('named', ['field', $locator]); + if ($field === NULL) { + throw new ElementNotFoundException($this->session->getDriver(), 'form field', 'id|name|label|value|placeholder', $locator); + } + $this->assert(!$field->isVisible(), sprintf('Field "%s" is visible.', $locator)); + } + /** + * Asserts that the specific element is visible on the current page. + * + * @param string $selector_type + * The element selector type (css, xpath). + * @param string|array $selector + * The element selector. + * @param ElementInterface $container + * The document to check against. + * + * @throws \Behat\Mink\Exception\ElementNotFoundException + * When the element doesn't exist. + */ + public function assertElementVisible($selector_type, $selector, ElementInterface $container = NULL) { + $container = $container ?: $this->session->getPage(); + $node = $container->find($selector_type, $selector); + if ($node === NULL) { + if (is_array($selector)) { + $selector = implode(' ', $selector); + } + throw new ElementNotFoundException($this->session->getDriver(), 'element', $selector_type, $selector); + } + $message = sprintf( + 'Element "%s" is not visible.', + $this->getMatchingElementRepresentation($selector_type, $selector) + ); + $this->assertElement($node->isVisible(), $message, $node); + } + + /** + * Asserts that the specific element is not visible on the current page. + * + * @param string $selector_type + * The element selector type (css, xpath). + * @param string|array $selector + * The element selector. + * @param ElementInterface $container + * The document to check against. + * + * @throws \Behat\Mink\Exception\ElementNotFoundException + * When the element doesn't exist. + */ + public function assertElementNotVisible($selector_type, $selector, ElementInterface $container = NULL) { + $container = $container ?: $this->session->getPage(); + $node = $container->find($selector_type, $selector); + if ($node === NULL) { + if (is_array($selector)) { + $selector = implode(' ', $selector); + } + throw new ElementNotFoundException($this->session->getDriver(), 'element', $selector_type, $selector); + } + $message = sprintf( + 'Element "%s" is visible.', + $this->getMatchingElementRepresentation($selector_type, $selector) + ); + $this->assertElement(!$node->isVisible(), $message, $node); + } + + /** + * {@inheritdoc} + */ + protected function assert($condition, $message) { + if ($condition) { + return; + } + + throw new ExpectationException($message, $this->session->getDriver()); + } + + /** + * {@inheritdoc} + */ + protected function assertElement($condition, $message, Element $element) { + if ($condition) { + return; + } + + throw new ElementHtmlException($message, $this->session->getDriver(), $element); + } + + /** + * {@inheritdoc} + */ + protected function getMatchingElementRepresentation($selector_type, $selector, $plural = FALSE) { + $pluralization = $plural ? 's' : ''; + + if (in_array($selector_type, ['named', 'named_exact', 'named_partial']) + && is_array($selector) && 2 === count($selector) + ) { + return sprintf('%s%s matching locator "%s"', $selector[0], $pluralization, $selector[1]); + } + + if (is_array($selector)) { + $selector = implode(' ', $selector); + } + + return sprintf('element%s matching %s "%s"', $pluralization, $selector_type, $selector); + } + } diff --git a/core/modules/system/tests/modules/form_test/src/Form/JavascriptStatesForm.php b/core/modules/system/tests/modules/form_test/src/Form/JavascriptStatesForm.php index 5bb45c8..695ac3a 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/JavascriptStatesForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/JavascriptStatesForm.php @@ -6,9 +6,9 @@ use Drupal\Core\Form\FormStateInterface; /** - * Builds a simple form that redirects on submit. + * Builds a simple form to test states. * - * @see \Drupal\form_test\Plugin\Block\RedirectFormBlock + * @see \Drupal\FunctionalJavascriptTests\Core\Form\JavascriptStatesTest */ class JavascriptStatesForm extends FormBase { @@ -24,243 +24,243 @@ public function getFormId() { */ public function buildForm(array $form, FormStateInterface $form_state) { // Hide name field when the anonymous checkbox is checked. - $form['name'] = array( + $form['name'] = [ '#type' => 'textfield', '#title' => $this->t('Name'), - '#states' => array( - 'invisible' => array( - ':input[name="anonymous"]' => array('checked' => TRUE), - ), - ), - ); + '#states' => [ + 'invisible' => [ + ':input[name="anonymous"]' => ['checked' => TRUE], + ], + ], + ]; // Uncheck anonymous field when the name field is filled. - $form['anonymous'] = array( + $form['anonymous'] = [ '#type' => 'checkbox', '#title' => $this->t('I prefer to remain anonymous'), '#default_value' => '1', - '#states' => array( - 'unchecked' => array( - ':input[name="name"]' => array('filled' => TRUE), - ), - ), - ); + '#states' => [ + 'unchecked' => [ + ':input[name="name"]' => ['filled' => TRUE], + ], + ], + ]; - $form['student_type'] = array( + $form['student_type'] = [ '#type' => 'radios', - '#options' => array( - 'high_school' => $this->t('High School'), + '#options' => [ + 'high_school' => $this->t('High School'), 'undergraduate' => $this->t('Undergraduate'), - 'graduate' => $this->t('Graduate'), - ), + 'graduate' => $this->t('Graduate'), + ], '#title' => $this->t('What type of student are you?'), - ); - $form['high_school'] = array( + ]; + $form['high_school'] = [ '#type' => 'fieldset', '#title' => $this->t('High School Information'), // This #states rule says that the "high school" fieldset should only // be shown if the "student_type" form element is set to "High School". - '#states' => array( - 'visible' => array( - ':input[name="student_type"]' => array('value' => 'high_school'), - ), - ), - ); + '#states' => [ + 'visible' => [ + ':input[name="student_type"]' => ['value' => 'high_school'], + ], + ], + ]; // High school information. - $form['high_school']['tests_taken'] = array( + $form['high_school']['tests_taken'] = [ '#type' => 'checkboxes', - '#options' => array_combine(array('SAT', 'ACT'), array(t('SAT'), t('ACT'))), + '#options' => array_combine(['SAT', 'ACT'], [t('SAT'), t('ACT')]), '#title' => $this->t('What standardized tests did you take?'), // This #states rule says that this checkboxes array will be visible only // when $form['student_type'] is set to t('High School'). // It uses the jQuery selector :input[name=student_type] to choose the // element which triggers the behavior, and then defines the "High School" // value as the one that triggers visibility. - '#states' => array( + '#states' => [ // Action to take. - 'visible' => array( - ':input[name="student_type"]' => array('value' => 'high_school'), - ), - ), - ); + 'visible' => [ + ':input[name="student_type"]' => ['value' => 'high_school'], + ], + ], + ]; - $form['high_school']['sat_score'] = array( + $form['high_school']['sat_score'] = [ '#type' => 'textfield', '#title' => $this->t('Your SAT score:'), '#size' => 4, // This #states rule limits visibility to when the $form['tests_taken'] // 'SAT' checkbox is checked." - '#states' => array( + '#states' => [ // Action to take. - 'visible' => array( - ':input[name="tests_taken[SAT]"]' => array('checked' => TRUE), - ), - ), - ); - $form['high_school']['act_score'] = array( + 'visible' => [ + ':input[name="tests_taken[SAT]"]' => ['checked' => TRUE], + ], + ], + ]; + $form['high_school']['act_score'] = [ '#type' => 'textfield', '#title' => $this->t('Your ACT score:'), '#size' => 4, // Set this element visible if the ACT checkbox above is checked. - '#states' => array( + '#states' => [ // Action to take. - 'visible' => array( - ':input[name="tests_taken[ACT]"]' => array('checked' => TRUE), - ), - ), - ); + 'visible' => [ + ':input[name="tests_taken[ACT]"]' => ['checked' => TRUE], + ], + ], + ]; // Undergrad information. - $form['undergraduate'] = array( + $form['undergraduate'] = [ '#type' => 'fieldset', '#title' => $this->t('Undergraduate Information'), // This #states rule says that the "undergraduate" fieldset should only // be shown if the "student_type" form element is set to "Undergraduate". - '#states' => array( - 'visible' => array( - ':input[name="student_type"]' => array('value' => 'undergraduate'), - ), - ), - ); + '#states' => [ + 'visible' => [ + ':input[name="student_type"]' => ['value' => 'undergraduate'], + ], + ], + ]; - $form['undergraduate']['how_many_years'] = array( + $form['undergraduate']['how_many_years'] = [ '#type' => 'select', '#title' => $this->t('How many years have you completed?'), // The options here are integers, but since all the action here happens // using the DOM on the client, we will have to use strings to work with // them. - '#options' => array( + '#options' => [ 1 => $this->t('One'), 2 => $this->t('Two'), 3 => $this->t('Three'), 4 => $this->t('Four'), 5 => $this->t('Lots'), - ), - ); + ], + ]; - $form['undergraduate']['comment'] = array( + $form['undergraduate']['comment'] = [ '#type' => 'item', '#description' => $this->t("Wow, that's a long time."), - '#states' => array( - 'visible' => array( + '#states' => [ + 'visible' => [ // Note that '5' must be used here instead of the integer 5. // The information is coming from the DOM as a string. - ':input[name="how_many_years"]' => array('value' => '5'), - ), - ), - ); - $form['undergraduate']['school_name'] = array( + ':input[name="how_many_years"]' => ['value' => '5'], + ], + ], + ]; + $form['undergraduate']['school_name'] = [ '#type' => 'textfield', '#title' => $this->t('Your college or university:'), - ); - $form['undergraduate']['school_country'] = array( + ]; + $form['undergraduate']['school_country'] = [ '#type' => 'select', - '#options' => array_combine(array('UK', 'Other'), array(t('UK'), t('Other'))), + '#options' => array_combine(['UK', 'Other'], [t('UK'), t('Other')]), '#title' => $this->t('In what country is your college or university located?'), - ); - $form['undergraduate']['country_writein'] = array( + ]; + $form['undergraduate']['country_writein'] = [ '#type' => 'textfield', '#size' => 20, '#title' => $this->t('Please enter the name of the country where your college or university is located.'), // Only show this field if school_country is set to 'Other'. - '#states' => array( + '#states' => [ // Action to take: Make visible. - 'visible' => array( - ':input[name="school_country"]' => array('value' => $this->t('Other')), - ), - ), - ); + 'visible' => [ + ':input[name="school_country"]' => ['value' => $this->t('Other')], + ], + ], + ]; - $form['undergraduate']['thanks'] = array( + $form['undergraduate']['thanks'] = [ '#type' => 'item', '#description' => $this->t('Thanks for providing both your school and your country.'), - '#states' => array( + '#states' => [ // Here visibility requires that two separate conditions be true. - 'visible' => array( - ':input[name="school_country"]' => array('value' => $this->t('Other')), - ':input[name="country_writein"]' => array('filled' => TRUE), - ), - ), - ); - $form['undergraduate']['go_away'] = array( + 'visible' => [ + ':input[name="school_country"]' => ['value' => $this->t('Other')], + ':input[name="country_writein"]' => ['filled' => TRUE], + ], + ], + ]; + $form['undergraduate']['go_away'] = [ '#type' => 'submit', '#value' => $this->t('Done with form'), - '#states' => array( + '#states' => [ // Here visibility requires that two separate conditions be true. - 'visible' => array( - ':input[name="school_country"]' => array('value' => $this->t('Other')), - ':input[name="country_writein"]' => array('filled' => TRUE), - ), - ), - ); + 'visible' => [ + ':input[name="school_country"]' => ['value' => $this->t('Other')], + ':input[name="country_writein"]' => ['filled' => TRUE], + ], + ], + ]; // Graduate student information. - $form['graduate'] = array( + $form['graduate'] = [ '#type' => 'fieldset', '#title' => $this->t('Graduate School Information'), // This #states rule says that the "graduate" fieldset should only // be shown if the "student_type" form element is set to "Graduate". - '#states' => array( - 'visible' => array( - ':input[name="student_type"]' => array('value' => 'graduate'), - ), - ), - ); - $form['graduate']['more_info'] = array( + '#states' => [ + 'visible' => [ + ':input[name="student_type"]' => ['value' => 'graduate'], + ], + ], + ]; + $form['graduate']['more_info'] = [ '#type' => 'textarea', '#title' => $this->t('Please describe your graduate studies'), - ); + ]; - $form['graduate']['info_provide'] = array( + $form['graduate']['info_provide'] = [ '#type' => 'checkbox', '#title' => $this->t('Check here if you have provided information above'), '#disabled' => TRUE, - '#states' => array( + '#states' => [ // Mark this checkbox checked if the "more_info" textarea has something // in it, if it's 'filled'. - 'checked' => array( - ':input[name="more_info"]' => array('filled' => TRUE), - ), - ), - ); + 'checked' => [ + ':input[name="more_info"]' => ['filled' => TRUE], + ], + ], + ]; - $form['average'] = array( + $form['average'] = [ '#type' => 'textfield', '#title' => $this->t('Enter your average'), // To trigger a state when the same controlling element can have more than // one possible value, put all values in a higher-level array. - '#states' => array( - 'visible' => array( - ':input[name="student_type"]' => array( - array('value' => 'high_school'), - array('value' => 'undergraduate'), - ), - ), - ), - ); + '#states' => [ + 'visible' => [ + ':input[name="student_type"]' => [ + ['value' => 'high_school'], + ['value' => 'undergraduate'], + ], + ], + ], + ]; - $form['expand_more_info'] = array( + $form['expand_more_info'] = [ '#type' => 'checkbox', '#title' => $this->t('Check here if you want to add more information.'), - ); - $form['more_info'] = array( + ]; + $form['more_info'] = [ '#type' => 'details', '#title' => $this->t('Additional Information'), // Expand the expand_more_info fieldset if the box is checked. - '#states' => array( - 'expanded' => array( - ':input[name="expand_more_info"]' => array('checked' => TRUE), - ), - ), - ); - $form['more_info']['feedback'] = array( + '#states' => [ + 'expanded' => [ + ':input[name="expand_more_info"]' => ['checked' => TRUE], + ], + ], + ]; + $form['more_info']['feedback'] = [ '#type' => 'textarea', '#title' => $this->t('What do you have to say?'), - ); + ]; return $form; } diff --git a/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index f44c224..c3b3d9a 100644 --- a/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -23,30 +23,29 @@ public function testToolbarToggling() { $admin_user = $this->drupalCreateUser([ 'access toolbar', 'administer site configuration', - 'access content overview' + 'access content overview', ]); $this->drupalLogin($admin_user); $this->drupalGet(''); + $page = $this->getSession()->getPage(); // Test that it is possible to toggle the toolbar tray. - $this->assertElementVisible('#toolbar-link-system-admin_content', 'Toolbar tray is open by default.'); - $this->click('#toolbar-item-administration'); - $this->assertElementNotVisible('#toolbar-link-system-admin_content', 'Toolbar tray is closed after clicking the "Manage" button.'); - $this->click('#toolbar-item-administration'); - $this->assertElementVisible('#toolbar-link-system-admin_content', 'Toolbar tray is visible again after clicking the "Manage" button a second time.'); + $content = $page->findLink('Content'); + $this->assertTrue($content->isVisible(), 'Toolbar tray is open by default.'); + $page->clickLink('Manage'); + $this->assertFalse($content->isVisible(), 'Toolbar tray is closed after clicking the "Manage" link.'); + $page->clickLink('Manage'); + $this->assertTrue($content->isVisible(), 'Toolbar tray is visible again after clicking the "Manage" button a second time.'); // Test toggling the toolbar tray between horizontal and vertical. - $this->assertElementVisible('#toolbar-item-administration-tray.toolbar-tray-horizontal', 'Toolbar tray is horizontally oriented by default.'); - $this->assertElementNotPresent('#toolbar-item-administration-tray.toolbar-tray-vertical', 'Toolbar tray is not vertically oriented by default.'); + $tray = $page->findById('toolbar-item-administration-tray'); + $this->assertFalse($tray->hasClass('toolbar-tray-vertical'), 'Toolbar tray is not vertically oriented by default.'); + $page->pressButton('Vertical orientation'); + $this->assertTrue($tray->hasClass('toolbar-tray-vertical'), 'After toggling the orientation the toolbar tray is now displayed vertically.'); - $this->click('#toolbar-item-administration-tray button.toolbar-icon-toggle-vertical'); - $this->assertJsCondition('jQuery("#toolbar-item-administration-tray").hasClass("toolbar-tray-vertical")'); - $this->assertElementVisible('#toolbar-item-administration-tray.toolbar-tray-vertical', 'After toggling the orientation the toolbar tray is now displayed vertically.'); - - $this->click('#toolbar-item-administration-tray button.toolbar-icon-toggle-horizontal'); - $this->assertJsCondition('jQuery("#toolbar-item-administration-tray").hasClass("toolbar-tray-horizontal")'); - $this->assertElementVisible('#toolbar-item-administration-tray.toolbar-tray-horizontal', 'After toggling the orientation a second time the toolbar tray is displayed horizontally again.'); + $page->pressButton('Horizontal orientation'); + $this->assertTrue($tray->hasClass('toolbar-tray-horizontal'), 'After toggling the orientation a second time the toolbar tray is displayed horizontally again.'); } } diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php index beee43b..bfa090f 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php @@ -5,7 +5,7 @@ use Drupal\FunctionalJavascriptTests\JavascriptTestBase; /** - * Tests the state of an element based on another element. + * Tests the state of elements based on another elements. * * @group javascript */ @@ -17,89 +17,93 @@ class JavascriptStatesTest extends JavascriptTestBase { public static $modules = ['form_test']; /** - * #states + * Test states of elements. */ public function testJavascriptStates() { $this->drupalGet('form-test/javascript-states-form'); $page = $this->getSession()->getPage(); - $this->assertElementNotVisible('#edit-name'); + $this->assertSession()->assertFieldNotVisible('Name'); // This should make the name element visible. $page->uncheckField('I prefer to remain anonymous'); - $this->assertElementVisible('#edit-name'); + $this->assertSession()->assertFieldVisible('Name'); // This should make the name element invisible. $page->checkField('I prefer to remain anonymous'); - $this->assertElementNotVisible('#edit-name'); + $this->assertSession()->assertFieldNotVisible('Name'); // This should make the name element visible. $page->uncheckField('I prefer to remain anonymous'); - $this->assertElementVisible('#edit-name'); + $this->assertSession()->assertFieldVisible('Name'); // Ensure all the details elements are invisible. - $this->assertElementNotVisible('#edit-high-school'); - $this->assertElementNotVisible('#edit-undergraduate'); - $this->assertElementNotVisible('#edit-graduate'); - $this->assertElementNotVisible('#edit-average'); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'High School Information']); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'Undergraduate Information']); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'Graduate School Information']); + $this->assertSession()->assertFieldNotVisible('Enter your average'); // High school form testing. - $this->click('#edit-student-type-high-school'); - $this->assertElementVisible('#edit-high-school'); - $this->assertElementNotVisible('#edit-undergraduate'); - $this->assertElementNotVisible('#edit-graduate'); - $this->assertElementVisible('#edit-average'); - - $this->assertElementNotVisible('#edit-sat-score'); - $this->assertElementNotVisible('#edit-act-score'); - $this->click('#edit-tests-taken-sat'); - $this->assertElementVisible('#edit-sat-score'); - $this->assertElementNotVisible('#edit-act-score'); - $this->click('#edit-tests-taken-act'); - $this->assertElementVisible('#edit-act-score'); - $this->assertElementVisible('#edit-sat-score'); - $this->click('#edit-tests-taken-sat'); - $this->click('#edit-tests-taken-act'); - $this->assertElementNotVisible('#edit-sat-score'); - $this->assertElementNotVisible('#edit-act-score'); + // There is no default method in MinkDriver to select a radio button. This + // process of selecting a radio button is copied from + // \Behat\Mink\Tests\Driver\Form\RadioTest::testSelectOption(). + $page->findById('edit-student-type-high-school')->selectOption('high_school'); + $this->assertSession()->assertElementVisible('named', ['fieldset', 'High School Information']); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'Undergraduate Information']); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'Graduate School Information']); + $this->assertSession()->assertFieldVisible('Enter your average'); + + $this->assertSession()->assertFieldNotVisible('Your SAT score:'); + $this->assertSession()->assertFieldNotVisible('Your ACT score:'); + $page->checkField('SAT'); + $this->assertSession()->assertFieldVisible('Your SAT score:'); + $this->assertSession()->assertFieldNotVisible('Your ACT score:'); + $page->checkField('ACT'); + $this->assertSession()->assertFieldVisible('Your ACT score:'); + $this->assertSession()->assertFieldVisible('Your SAT score:'); + $page->uncheckField('SAT'); + $page->uncheckField('ACT'); + $this->assertSession()->assertFieldNotVisible('Your SAT score:'); + $this->assertSession()->assertFieldNotVisible('Your ACT score:'); // Undergraduate form testing. - $this->click('#edit-student-type-undergraduate'); - $this->assertElementNotVisible('#edit-high-school'); - $this->assertElementVisible('#edit-undergraduate'); - $this->assertElementNotVisible('#edit-graduate'); - $this->assertElementVisible('#edit-average'); + $page->findById('edit-student-type-undergraduate')->selectOption('undergraduate'); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'High School Information']); + $this->assertSession()->assertElementVisible('named', ['fieldset', 'Undergraduate Information']); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'Graduate School Information']); + $this->assertSession()->assertFieldVisible('Enter your average'); - $this->assertElementNotVisible('#edit-comment--description'); + $this->assertSession()->assertElementNotVisible('css', '#edit-comment--description'); $page->selectFieldOption('How many years have you completed?', '5'); - $this->assertElementVisible('#edit-comment--description'); - $this->assertElementNotVisible('#edit-country-writein'); + $this->assertSession()->assertElementVisible('css', '#edit-comment--description'); + $this->assertSession()->assertFieldNotVisible('Please enter the name of the country where your college or university is located.'); $page->selectFieldOption('In what country is your college or university located?', 'Other'); - $this->assertElementVisible('#edit-country-writein'); + $this->assertSession()->assertFieldVisible('Please enter the name of the country where your college or university is located.'); $page->selectFieldOption('In what country is your college or university located?', 'UK'); - $this->assertElementNotVisible('#edit-country-writein'); + $this->assertSession()->assertFieldNotVisible('Please enter the name of the country where your college or university is located.'); // Graduate form testing. - $this->click('#edit-student-type-graduate'); - $this->assertElementNotVisible('#edit-high-school'); - $this->assertElementNotVisible('#edit-undergraduate'); - $this->assertElementVisible('#edit-graduate'); - $this->assertElementNotVisible('#edit-average'); + $page->findById('edit-student-type-graduate')->selectOption('graduate'); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'High School Information']); + $this->assertSession()->assertElementNotVisible('named', ['fieldset', 'Undergraduate Information']); + $this->assertSession()->assertElementVisible('named', ['fieldset', 'Graduate School Information']); + $this->assertSession()->assertFieldNotVisible('Enter your average'); $this->assertSession()->checkboxNotChecked('Check here if you have provided information above'); $page->fillField('Please describe your graduate studies', 'Some text'); $this->assertSession()->checkboxChecked('Check here if you have provided information above'); $page->fillField('Please describe your graduate studies', ''); - // Empty and filled states are triggered on keyup event. - $page->findField('Please describe your graduate studies')->keyUp('a'); + // Empty and filled states are triggered on keyup event. The ASCII code of + // 'backspace' is '8'. + $page->findField('Please describe your graduate studies')->keyUp(8); $this->assertSession()->checkboxNotChecked('Check here if you have provided information above'); // Feedback testing. - $this->assertElementNotVisible('#edit-feedback'); + $this->assertSession()->assertFieldNotVisible('What do you have to say?'); $page->checkField('Check here if you want to add more information.'); - $this->assertElementVisible('#edit-feedback'); + $this->assertSession()->assertFieldVisible('What do you have to say?'); $page->uncheckField('Check here if you want to add more information.'); - $this->assertElementNotVisible('#edit-feedback'); + $this->assertSession()->assertFieldNotVisible('What do you have to say?'); } } diff --git a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php index 2862d4c..928e37d 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php @@ -35,32 +35,6 @@ protected function initMink() { } /** - * Asserts that the element with the given CSS selector is visible. - * - * @param string $css_selector - * The CSS selector identifying the element to check. - * @param string $message - * Optional message to show alongside the assertion. - */ - protected function assertElementVisible($css_selector, $message = '') { - $message = $message ?: "Element ($css_selector) is visible."; - $this->assertTrue($this->getSession()->getDriver()->isVisible(CssSelector::toXPath($css_selector)), $message); - } - - /** - * Asserts that the element with the given CSS selector is not visible. - * - * @param string $css_selector - * The CSS selector identifying the element to check. - * @param string $message - * Optional message to show alongside the assertion. - */ - protected function assertElementNotVisible($css_selector, $message = '') { - $message = $message ?: "Element ($css_selector) is not visible."; - $this->assertFalse($this->getSession()->getDriver()->isVisible(CssSelector::toXPath($css_selector)), $message); - } - - /** * Waits for the given time or until the given JS condition becomes TRUE. * * @param string $condition @@ -77,7 +51,7 @@ protected function assertElementNotVisible($css_selector, $message = '') { */ protected function assertJsCondition($condition, $timeout = 1000, $message = '') { $message = $message ?: "Javascript condition met:\n" . $condition; - $result = $this->getSession()->getDriver()->wait($timeout, $condition); + $result = $this->getSession()->wait($timeout, $condition); $this->assertTrue($result, $message); } diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 8d8ea2b..9c5c992 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -1441,40 +1441,6 @@ protected function drupalUserIsLoggedIn(UserInterface $account) { } /** - * Asserts that the element with the given CSS selector is present. - * - * @param string $css_selector - * The CSS selector identifying the element to check. - * @param string $message - * Optional message to show alongside the assertion. - */ - protected function assertElementPresent($css_selector, $message = '') { - $this->assertNotEmpty($this->getSession()->getDriver()->find(CssSelector::toXPath($css_selector)), $message); - } - - /** - * Asserts that the element with the given CSS selector is not present. - * - * @param string $css_selector - * The CSS selector identifying the element to check. - * @param string $message - * Optional message to show alongside the assertion. - */ - protected function assertElementNotPresent($css_selector, $message = '') { - $this->assertEmpty($this->getSession()->getDriver()->find(CssSelector::toXPath($css_selector)), $message); - } - - /** - * Clicks the element with the given CSS selector. - * - * @param string $css_selector - * The CSS selector identifying the element to click. - */ - protected function click($css_selector) { - $this->getSession()->getDriver()->click(CssSelector::toXPath($css_selector)); - } - - /** * Prevents serializing any properties. * * Browser tests are run in a separate process. To do this PHPUnit creates a