From 2175e1f04d15b40dac0c6c3d467f8569910f8983 Mon Sep 17 00:00:00 2001 From: Edys Meza Date: Fri, 25 Sep 2020 15:10:56 -0600 Subject: [PATCH] Issue 3167034: better handling elementAttributeExists assert method --- .../Theme/ImageLoadingAttributeTest.php | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/core/modules/system/tests/src/Functional/Theme/ImageLoadingAttributeTest.php b/core/modules/system/tests/src/Functional/Theme/ImageLoadingAttributeTest.php index 8e30450611f..f2f1f107d0b 100644 --- a/core/modules/system/tests/src/Functional/Theme/ImageLoadingAttributeTest.php +++ b/core/modules/system/tests/src/Functional/Theme/ImageLoadingAttributeTest.php @@ -2,7 +2,9 @@ namespace Drupal\Tests\system\Functional\Theme; +use Behat\Mink\Exception\ElementHtmlException; use Drupal\Tests\BrowserTestBase; +use Drupal\Tests\WebAssert; /** * Tests lazy loading for images. @@ -27,20 +29,48 @@ class ImageLoadingAttributeTest extends BrowserTestBase { * Tests that loading attribute is enabled for images. */ public function testImageLoadingAttribute() { + $assert = $this->assertSession(); + // Get page under test. $this->drupalGet('image-lazy-load-test'); // Loading attribute is added when image dimensions has been set. - $this->assertSession()->elementAttributeExists('css', '#with-dimensions img', 'loading'); - $this->assertSession()->elementAttributeContains('css', '#with-dimensions img', 'loading', 'lazy'); + $assert->elementAttributeExists('css', '#with-dimensions img', 'loading'); + $assert->elementAttributeContains('css', '#with-dimensions img', 'loading', 'lazy'); // Loading attribute with lazy default value can be overriden. - $this->assertSession()->elementAttributeContains('css', '#override-loading-attribute img', 'loading', 'eager'); + $assert->elementAttributeContains('css', '#override-loading-attribute img', 'loading', 'eager'); // Without image dimensions loading attribute is not generated. - $this->assertSession()->elementAttributeContains('css', '#without-dimensions img', 'alt', 'Image lazy load testing image without dimensions'); - $this->expectExceptionMessage('The attribute "loading" was not found in the element matching css "#without-dimensions img".'); - $this->assertSession()->elementAttributeExists('css', '#without-dimensions img', 'loading'); + $this->assertFalse($this->elementAttributeExists($assert, 'css', '#without-dimensions img', 'loading')); + } + + /** + * Checks that an attribuet exists in an element. + * + * Exends Drupal\Tests\WebAssert::elementAttributeExists() method to returns a + * boolean type instead throwing an execption when attribuet is not found. + * + * @param \Drupal\Tests\WebAssert $assert + * @param string $selectorType + * @param string|array $selector + * @param string $attribute + * + * @see Drupal\Tests\WebAssert::elementAttributeExists() + * + * @return bool + * Returns TRUE if $attribute exists, FALSE otherwise. + */ + protected function elementAttributeExists(WebAssert $assert, $selectorType, $selector, $attribute) { + $attribute_exists = TRUE; + try { + $assert->elementAttributeExists($selectorType, $selector, $attribute); + } + catch (ElementHtmlException $th) { + $attribute_exists = FALSE; + } + + return $attribute_exists; } }