Empty file when uploaded via Playwright test

87 Views Asked by At

I use the function to upload a PDF file in a playwright test. The file gets uploaded but the problem is that the file is uploaded empty. If I have 3 pages with text and images in the initial PDF, I get 3 pages in the uploaded PDF but they are all blank.

When I use this when playwright for the upload it does not work. It crashes in await fileChooserPromise aparament because I use not an input I click on a div when I upload.

When I follow the playwright documentation on how to upload a file, it crashes in await fileChooserPromise because I don't have an input to upload the file, but I click on a div to do.

const uploadFile = async(page, documentPathCopy) => {
  const buffer = fs.readFileSync(documentPathCopy, 'utf8');
  // Create the DataTransfer and File
  let dataTransfer = await page.evaluateHandle((data) => {

    let dt = new DataTransfer();
    // Convert the buffer to a hex array
    const blob = new Blob([data]);
    const file = new File([blob], 'testFile2.pdf', {
      type: 'application/pdf'
    });

    dt.items.add(file);
    return dt;
  }, buffer);
  await page.dispatchEvent('[data-testid="fileUpload"]', 'drop', {
    dataTransfer
  });
}
0

There are 0 best solutions below