minkphp / Mink

PHP web browser emulator abstraction
https://mink.behat.org/
MIT License
1.6k stars 280 forks source link

Upload multiple files. #358

Open bakie opened 11 years ago

bakie commented 11 years ago

Is there a way to upload multiple files in a filefield ? I have a filefield in which I can upload multiple files by hand, but how can I do this with behat testing. I found the method which allows you to upload a file, but if I use this 2 times in sequence it only uses the last file upload.

aik099 commented 11 years ago

Please provide used HTML markup. Are you using regular <input type="file"/>?

aik099 commented 11 years ago

If you're using something like this http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_multiple , then it overwrites previously selected file if you select file one-by-one. However if you give a set of filenames to it, then it would work.

bakie commented 11 years ago

The html looks like <input type="file" id="kunstmaan_mediabundle_bulkupload_files" name="kunstmaan_mediabundle_bulkupload[files][]" accept="*/*" multiple="multiple" />

I tried to use When I attach the file "image.jpg,image.png" to "kunstmaan_mediabundle_bulkupload[files][]" But this gives an error saying the 'image.jpg,image.png' does not exist on the file system

dharmalingam commented 11 years ago

Me too getting the same error('D:/looks.jpg' does not exist on the file system) and my codes are in FeatureContext.php as bellow

$page = $this->getSession()->getPage(); $element = $page->find('css', '#ImageID'); $element->attachFile('D:/looks.jpg');

aik099 commented 11 years ago

Selenium itself doesn't support uploading multiple files into a single upload field via JSON Wire protocol (see https://code.google.com/p/selenium/issues/detail?id=2239).

Setting upload field value via JavaScript is forbidden for security reasons.

das-peter commented 5 years ago

As a reference - following code seems to work at least with some browsers:

  /**
   * Attaches multiple files to field with specified id|name|label|value
   *
   * @see https://tuyennta.com/upload-multiple-files-in-selenium/
   *
   * @When /^(?:|I )attach following files to "(?P<field>(?:[^"]|\\")*)":$/
   * | file_path_1 |
   * | file_path_2 |
   * | file_path_3 |
   */
  public function attachFilesToField($field, \Behat\Gherkin\Node\TableNode $files)
  {
    $field = $this->fixStepArgument($field);

    $filePaths = [];
    foreach ($files->getTable() as $file) {
      $path = current($file);
      $fullPath = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
      if (is_file($fullPath)) {
        $path = $fullPath;
      }
      $filePaths[] = $path;
    }

    /** @var Session $session */
    $session = $this->getSession();
    $session->getPage()->findField($field)->setValue(implode("\n ", $filePaths));
  }