Issue 3167034: better handling elementAttributeExists assert method

merge-requests/25/head
Edys Meza 2020-09-25 15:10:56 -06:00
parent ef1bb0f1b3
commit 2175e1f04d
1 changed files with 36 additions and 6 deletions

View File

@ -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;
}
}