diff --git a/core/modules/simpletest/src/WebAssert.php b/core/modules/simpletest/src/WebAssert.php index bc7302f..21c413b 100644 --- a/core/modules/simpletest/src/WebAssert.php +++ b/core/modules/simpletest/src/WebAssert.php @@ -70,17 +70,9 @@ public function selectExists($select, TraversableElement $container = NULL) { * 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)); + $this->assertElementVisible('named', ['field', $locator], $container); } /** @@ -95,12 +87,34 @@ public function assertFieldVisible($locator, TraversableElement $container = NUL * 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)); + $this->assertElementNotVisible('named', ['field', $locator], $container); + } + + /** + * Asserts that the given fieldset 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. + */ + public function assertFieldsetVisible($locator, TraversableElement $container = NULL) { + $this->assertElementVisible('named', ['fieldset', $locator], $container); + } + + /** + * Asserts that the given fieldset 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 assertFieldsetNotVisible($locator, TraversableElement $container = NULL) { + $this->assertElementNotVisible('named', ['fieldset', $locator], $container); } /** * Asserts that the specific element is visible on the current page. @@ -163,17 +177,6 @@ public function assertElementNotVisible($selector_type, $selector, ElementInterf /** * {@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; diff --git a/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index c3b3d9a..f44c224 100644 --- a/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -23,29 +23,30 @@ 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. - $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.'); + $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.'); // Test toggling the toolbar tray between horizontal and vertical. - $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->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.'); - $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.'); + $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.'); } } diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php index bfa090f..994d374 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php @@ -22,88 +22,86 @@ class JavascriptStatesTest extends JavascriptTestBase { public function testJavascriptStates() { $this->drupalGet('form-test/javascript-states-form'); $page = $this->getSession()->getPage(); + $web_session = $this->assertSession(); - $this->assertSession()->assertFieldNotVisible('Name'); + $web_session->assertFieldNotVisible('Name'); // This should make the name element visible. $page->uncheckField('I prefer to remain anonymous'); - $this->assertSession()->assertFieldVisible('Name'); + $web_session->assertFieldVisible('Name'); // This should make the name element invisible. $page->checkField('I prefer to remain anonymous'); - $this->assertSession()->assertFieldNotVisible('Name'); + $web_session->assertFieldNotVisible('Name'); // This should make the name element visible. $page->uncheckField('I prefer to remain anonymous'); - $this->assertSession()->assertFieldVisible('Name'); + $web_session->assertFieldVisible('Name'); // Ensure all the details elements are invisible. - $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'); + $web_session->assertFieldsetNotVisible('High School Information'); + $web_session->assertFieldsetNotVisible('Undergraduate Information'); + $web_session->assertFieldsetNotVisible('Graduate School Information'); + $web_session->assertFieldNotVisible('Enter your average'); // High school form testing. - // 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->selectFieldOption('edit-student-type-high-school', 'high_school'); + $web_session->assertFieldsetVisible('High School Information'); + $web_session->assertFieldsetNotVisible('Undergraduate Information'); + $web_session->assertFieldsetNotVisible('Graduate School Information'); + $web_session->assertFieldVisible('Enter your average'); + + $web_session->assertFieldNotVisible('Your SAT score:'); + $web_session->assertFieldNotVisible('Your ACT score:'); $page->checkField('SAT'); - $this->assertSession()->assertFieldVisible('Your SAT score:'); - $this->assertSession()->assertFieldNotVisible('Your ACT score:'); + $web_session->assertFieldVisible('Your SAT score:'); + $web_session->assertFieldNotVisible('Your ACT score:'); $page->checkField('ACT'); - $this->assertSession()->assertFieldVisible('Your ACT score:'); - $this->assertSession()->assertFieldVisible('Your SAT score:'); + $web_session->assertFieldVisible('Your ACT score:'); + $web_session->assertFieldVisible('Your SAT score:'); $page->uncheckField('SAT'); $page->uncheckField('ACT'); - $this->assertSession()->assertFieldNotVisible('Your SAT score:'); - $this->assertSession()->assertFieldNotVisible('Your ACT score:'); + $web_session->assertFieldNotVisible('Your SAT score:'); + $web_session->assertFieldNotVisible('Your ACT score:'); // Undergraduate form testing. - $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->assertSession()->assertElementNotVisible('css', '#edit-comment--description'); + $page->selectFieldOption('edit-student-type-undergraduate', 'undergraduate'); + $web_session->assertFieldsetNotVisible('High School Information'); + $web_session->assertFieldsetVisible('Undergraduate Information'); + $web_session->assertFieldsetNotVisible('Graduate School Information'); + $web_session->assertFieldVisible('Enter your average'); + + $comment = $page->findById('edit-comment--description'); + $this->assertFalse($comment->isVisible(), 'Comment item is not visible'); $page->selectFieldOption('How many years have you completed?', '5'); - $this->assertSession()->assertElementVisible('css', '#edit-comment--description'); - $this->assertSession()->assertFieldNotVisible('Please enter the name of the country where your college or university is located.'); + $this->assertTrue($comment->isVisible(), 'Comment item is visible'); + $web_session->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->assertSession()->assertFieldVisible('Please enter the name of the country where your college or university is located.'); + $web_session->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->assertSession()->assertFieldNotVisible('Please enter the name of the country where your college or university is located.'); + $web_session->assertFieldNotVisible('Please enter the name of the country where your college or university is located.'); // Graduate form testing. - $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'); + $page->selectFieldOption('edit-student-type-graduate', 'graduate'); + $web_session->assertFieldsetNotVisible('High School Information'); + $web_session->assertFieldsetNotVisible('Undergraduate Information'); + $web_session->assertFieldsetVisible('Graduate School Information'); + $web_session->assertFieldNotVisible('Enter your average'); - $this->assertSession()->checkboxNotChecked('Check here if you have provided information above'); + $web_session->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'); + $web_session->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. 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'); + // Empty and filled states are triggered on keyup event. + $page->findField('Please describe your graduate studies')->keyUp(PHP_EOL); + $web_session->checkboxNotChecked('Check here if you have provided information above'); // Feedback testing. - $this->assertSession()->assertFieldNotVisible('What do you have to say?'); + $web_session->assertFieldNotVisible('What do you have to say?'); $page->checkField('Check here if you want to add more information.'); - $this->assertSession()->assertFieldVisible('What do you have to say?'); + $web_session->assertFieldVisible('What do you have to say?'); $page->uncheckField('Check here if you want to add more information.'); - $this->assertSession()->assertFieldNotVisible('What do you have to say?'); + $web_session->assertFieldNotVisible('What do you have to say?'); } } diff --git a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php index 928e37d..41e5b79 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php @@ -35,6 +35,30 @@ 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 = '') { + $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 = '') { + $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 @@ -51,7 +75,7 @@ protected function initMink() { */ protected function assertJsCondition($condition, $timeout = 1000, $message = '') { $message = $message ?: "Javascript condition met:\n" . $condition; - $result = $this->getSession()->wait($timeout, $condition); + $result = $this->getSession()->getDriver()->wait($timeout, $condition); $this->assertTrue($result, $message); } diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 9c5c992..8d8ea2b 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -1441,6 +1441,40 @@ 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