Laravel dusk: test file upload with Dropzone.js

十年热恋 提交于 2019-12-06 11:37:33

Dropzonejs adds an input field with a specific 'dz-hidden-input' class. You can find it at the bottom of your html page, likely just before the </body> tag:

<input type="file" multiple="multiple" class="dz-hidden-input">

So you can tell Dusk to match that exact selector through the attach method:

$browser->attach('input.dz-hidden-input', storage_path('app/public/testing/test-file.jpg'));

If you have a dropzone preview showing the file name and a 'Remove file' button, you can then chain some assertions like this to make sure the file can also be removed:

$browser->attach('input.dz-hidden-input', storage_path('app/public/testing/test-file.jpg'))
 ->assertSee('test-file.jpg')
 ->assertSeeLink('Remove file')
 ->clickLink('Remove file')
 ->assertDontSee('test-file.jpg');

As mentioned in another answer, the approach lies in the dummy input field that dropzone.js creates. However, if you have multiple, separate dropzones, this is not a viable solution, since there's no way to tell which input field is the right one to attach a file to.

The best solution lies in the hiddenInputContainer configuration value for the dropzone. Specify a value you can use to distinguish the dropzones, such as the div containing the dropzone you're configuring.

Then, you can attach a file to it (using Faker as a shortcut for generating the file):

$image = $faker->image('/tmp', 640, 480);
$browser->attach('#dropzone-image1 input.dz-hidden-input', $image);
$browser->attach('#dropzone-image2 input.dz-hidden-input', $image);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!