You are here

public function BlazyFilterJavaScriptTest::testFilterDisplay in Blazy 8.2

Test the Blazy filter has media-wrapper--blazy for IMG and IFRAME elements.

File

tests/src/FunctionalJavascript/BlazyFilterJavaScriptTest.php, line 84

Class

BlazyFilterJavaScriptTest
Tests the Blazy Filter JavaScript using Selenium, or Chromedriver.

Namespace

Drupal\Tests\blazy\FunctionalJavascript

Code

public function testFilterDisplay() {
  $image_path = $this
    ->getImagePath(TRUE);

  // Prevents execessive width with aspect ratio.
  $settings['extra_text'] = '<div style="width: 640px;">';
  $settings['extra_text'] .= '<img data-unblazy src="' . $this->url . '" width="320" height="320" />';
  $settings['extra_text'] .= '<iframe src="https://www.youtube.com/watch?v=uny9kbh4iOEd" width="640" height="360"></iframe>';
  $settings['extra_text'] .= '<img src="' . $this->url . '" width="320" height="320" />';
  $settings['extra_text'] .= '<img src="https://www.drupal.org/files/project-images/slick-carousel-drupal.png" width="215" height="162" />';
  $settings['extra_text'] .= '</div>';
  $this
    ->setUpContentTypeTest($this->bundle);
  $this
    ->setUpContentWithItems($this->bundle, $settings);
  $session = $this
    ->getSession();
  $this
    ->drupalGet('node/' . $this->entity
    ->id());

  // Ensures Blazy is not loaded on page load.
  // @todo with Native lazyload, b-loaded is enforced on page load. And
  // since the testing browser Chrome support it, it is irrelevant.
  // @todo $this->assertSession()->elementNotExists('css', '.b-loaded');
  // Capture the initial page load moment.
  $this
    ->createScreenshot($image_path . '/1_blazy_filter_initial.png');
  $this
    ->assertSession()
    ->elementExists('css', '.b-lazy');

  // Trigger Blazy to load images by scrolling down window.
  $session
    ->executeScript('window.scrollTo(0, document.body.scrollHeight);');

  // Capture the loading moment after scrolling down the window.
  $this
    ->createScreenshot($image_path . '/2_blazy_filter_loading.png');

  // Verifies that our filter works identified by media-wrapper--blazy class.
  $this
    ->assertSession()
    ->elementExists('css', '.media-wrapper--blazy');
  $this
    ->assertSession()
    ->elementContains('css', '.media-wrapper--blazy', 'b-lazy');

  // Also verifies that [data-unblazy] should not be touched, nor lazyloaded.
  $this
    ->assertSession()
    ->elementNotContains('css', '.media-wrapper--blazy', 'data-unblazy');

  // Verifies that one of the images is there once loaded.
  $result = $this
    ->assertSession()
    ->waitForElement('css', '.b-loaded');
  $this
    ->assertNotEmpty($result);

  // Capture the loaded moment.
  // The screenshots are at sites/default/files/simpletest/blazy.
  $this
    ->createScreenshot($image_path . '/3_blazy_filter_loaded.png');
}