You are here

public function ThunderEntityBrowserTestTrait::uploadFile in Thunder 6.1.x

Same name and namespace in other branches
  1. 8.5 tests/src/FunctionalJavascript/ThunderEntityBrowserTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderEntityBrowserTestTrait::uploadFile()
  2. 8.2 tests/src/FunctionalJavascript/ThunderEntityBrowserTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderEntityBrowserTestTrait::uploadFile()
  3. 8.3 tests/src/FunctionalJavascript/ThunderEntityBrowserTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderEntityBrowserTestTrait::uploadFile()
  4. 8.4 tests/src/FunctionalJavascript/ThunderEntityBrowserTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderEntityBrowserTestTrait::uploadFile()
  5. 6.2.x tests/src/FunctionalJavascript/ThunderEntityBrowserTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderEntityBrowserTestTrait::uploadFile()
  6. 6.0.x tests/src/FunctionalJavascript/ThunderEntityBrowserTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderEntityBrowserTestTrait::uploadFile()

Upload file inside entity browser.

NOTE: It will search for first tab with upload widget and file will be uploaded there. Upload is done over input file field and it has to be visible for selenium to work.

Parameters

\Behat\Mink\Element\DocumentElement $page: Current active page.

string $filePath: Path to file that should be uploaded.

Throws

\Exception

1 call to ThunderEntityBrowserTestTrait::uploadFile()
MediaGalleryModifyTest::testAddRemove in tests/src/FunctionalJavascript/MediaGalleryModifyTest.php
Test add/remove Images in Gallery.

File

tests/src/FunctionalJavascript/ThunderEntityBrowserTestTrait.php, line 64

Class

ThunderEntityBrowserTestTrait
Trait with support for handling Entity Browser actions.

Namespace

Drupal\Tests\thunder\FunctionalJavascript

Code

public function uploadFile(DocumentElement $page, $filePath) {

  // Click all tabs until we find upload Tab.
  $tabLinks = $page
    ->findAll('css', '.eb-tabs a');
  if (empty($tabLinks)) {
    throw new \Exception(sprintf('Unable to find tabs in entity browser iframe on page %s', $this
      ->getSession()
      ->getCurrentUrl()));
  }

  // Click all tabs until input file field for upload is found.
  $fileFieldSelector = "input[type='file'].dz-hidden-input";
  $fileField = NULL;
  foreach ($tabLinks as $tabLink) {

    /** @var \Behat\Mink\Element\NodeElement $tabLink */
    $tabLink
      ->click();
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
    $fileField = $page
      ->find('css', $fileFieldSelector);
    if (!empty($fileField)) {
      break;
    }
  }
  if (empty($fileField)) {
    throw new \Exception(sprintf('The drop-down file field was not found on the page %s', $this
      ->getSession()
      ->getCurrentUrl()));
  }

  // Make file field visible and isolate possible problems with "multiple".
  $this
    ->getSession()
    ->executeScript('jQuery("' . $fileFieldSelector . '").show(0).css("visibility","visible").width(200).height(30).removeAttr("multiple");');
  $fileField
    ->attachFile($filePath);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Wait up to 10 sec that "Use selected" button is active.
  $this
    ->getSession()
    ->wait(10000, '(typeof jQuery === "undefined" || !jQuery(\'input[name="op"]\').is(":disabled"))');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // In case of gallery image upload we should wait additionally so that all
  // command for auto selection are executed.
  $this
    ->getSession()
    ->wait(200);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
}