Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload in Testcafe with hidden input

I want to test upload folder/ multiple files using testcafe. It has couple of steps to upload files.

  • Step - 1: Click on file browse button to select files
  • Step - 2: Native Confirmation box to confirm the Upload
  • Step - 3: Click on Upload button to upload files

In the HTML code, input [type=file] is hidden. HTML Code:

<div class="col-md-12">
    <input type="button" class="btn btn-primary btn-exec" value="Select Files" id="fileBrowseBtn">
    <input type="file" id="selectFiles" webkitdirectory="" style="display: none">
</div>

I tried with the following code but was not working at all.

 await t
.click(Selector('#fileBrowseBtn'))
.setNativeDialogHandler(() => true)
.setFilesToUpload(Selector('input').withAttribute('type','file'), [
            './uploads/1.jpg',
            './uploads/2.jpg',
            './uploads/3.jpg'
        ])
.setNativeDialogHandler(() => true)
.click(Selector('#uploadWizard').find('button').withText('Upload'))

Can anyone help me with workable example? I tried lots but may be I missed something. Thanks in advance.

like image 455
Panda Avatar asked May 17 '19 01:05

Panda


1 Answers

According to an example, you should not open the 'Choose file' dialog.

Try to simplify your code as follows:

await t
.setFilesToUpload(Selector('input').withAttribute('type','file'), [
            './uploads/1.jpg',
            './uploads/2.jpg',
            './uploads/3.jpg'
        ])
.click(Selector('#uploadWizard').find('button').withText('Upload'))
like image 196
mlosev Avatar answered Oct 05 '22 14:10

mlosev