diff --git a/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index c22443c..326effb 100644 --- a/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -30,23 +30,23 @@ public function testToolbarToggling() { $this->drupalGet(''); // Test that it is possible to toggle the toolbar tray. - $this->assertTrue($this->getSession()->getDriver()->isVisible('.//a[@id="toolbar-link-system-admin_content"]'), 'Toolbar tray is open by default.'); - $this->getSession()->getDriver()->click('.//a[@id="toolbar-item-administration"]'); - $this->assertFalse($this->getSession()->getDriver()->isVisible('//a[@id="toolbar-link-system-admin_content"]'), 'Toolbar tray is closed after clicking the "Manage" button.'); - $this->getSession()->getDriver()->click('.//a[@id="toolbar-item-administration"]'); - $this->assertTrue($this->getSession()->getDriver()->isVisible('//a[@id="toolbar-link-system-admin_content"]'), '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('a#toolbar-item-administration'); + $this->assertElementNotVisible('#toolbar-link-system-admin_content', 'Toolbar tray is closed after clicking the "Manage" button.'); + $this->click('a#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. - $this->assertTrue($this->getSession()->getDriver()->isVisible('.//div[@id="toolbar-item-administration-tray" and contains(concat(" ", @class, " "), " toolbar-tray-horizontal ")]'), 'Toolbar tray is horizontally oriented by default.'); - $this->assertEmpty($this->getSession()->getDriver()->find('.//div[@id="toolbar-item-administration-tray" and contains(concat(" ", @class, " "), " toolbar-tray-vertical ")]'), 'Toolbar tray not vertically oriented by default.'); + $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.'); - $this->getSession()->getDriver()->click('.//div[@id="toolbar-item-administration-tray"]//button[contains(concat(" ", @class, " "), " toolbar-icon-toggle-vertical ")]'); - $this->assertTrue($this->getSession()->getDriver()->wait(1000, 'jQuery("#toolbar-item-administration-tray").hasClass("toolbar-tray-vertical")')); - $this->assertTrue($this->getSession()->getDriver()->isVisible('.//div[@id="toolbar-item-administration-tray" and contains(concat(" ", @class, " "), " 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->assertTrue($this->wait(1000, 'jQuery("#toolbar-item-administration-tray").hasClass("toolbar-tray-vertical")')); + $this->assertElementVisible('div#toolbar-item-administration-tray.toolbar-tray-vertical', 'After toggling the orientation the toolbar tray is now displayed vertically.'); - $this->getSession()->getDriver()->click('.//div[@id="toolbar-item-administration-tray"]//button[contains(concat(" ", @class, " "), " toolbar-icon-toggle-horizontal ")]'); - $this->assertTrue($this->getSession()->getDriver()->wait(1000, 'jQuery("#toolbar-item-administration-tray").hasClass("toolbar-tray-horizontal")')); - $this->assertTrue($this->getSession()->getDriver()->isVisible('.//div[@id="toolbar-item-administration-tray" and contains(concat(" ", @class, " "), " 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-horizontal'); + $this->assertTrue($this->wait(1000, 'jQuery("#toolbar-item-administration-tray").hasClass("toolbar-tray-horizontal")')); + $this->assertElementVisible('div#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/JavascriptTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php index 00daa43..043ba84 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php @@ -33,4 +33,90 @@ protected function initMink() { parent::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($this->selectorToXpath($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($this->selectorToXpath($css_selector)), $message); + } + + /** + * 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($this->selectorToXpath($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($this->selectorToXpath($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($this->selectorToXpath($css_selector)); + } + + /** + * Waits for the given time or until the given JS condition becomes TRUE. + * + * @param int $timeout + * Timeout in milliseconds. + * @param string|bool $condition + * JS condition, or FALSE to wait for the full duration of the timeout. + * + * @return bool + * The result of the JS condition. + */ + protected function wait($timeout, $condition = FALSE) { + return $this->getSession()->getDriver()->wait($timeout, $condition); + } + + /** + * Converts the given CSS selector to an XPath expression. + * + * @param string $css_selector + * The CSS selector to convert. + * + * @return string + * The converted XPath expression. + */ + protected function selectorToXpath($css_selector) { + return $this->getSession()->getSelectorsHandler()->selectorToXpath('css', $css_selector); + } + }