Nir Tzezana
Nir Tzezana

Reputation: 2342

Is there a way to upload a file in React Dropzone with Puppeteer?

I tried a few suggestions online but none worked.
Currently I'm trying this:

  await page.goto('http://localhost:3000/');
  await page.waitForSelector('input[type=file]');
  const fileInput  = await page.$('input[type=file]');
  await fileInput.uploadFile("file.png");  

This doesn't work (or do anything).
Is there a way of doing this?

Upvotes: 4

Views: 1057

Answers (1)

hardkoded
hardkoded

Reputation: 21695

It seems that the dropzone needs a little push. You could trigger a change so the dropzone knows that there is a new file in the file input.

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    ignoreDefaultArgs: true
  });
  const page = await browser.newPage();
  await page.goto('https://react-dropzone.js.org/');
  await page.waitForSelector('input[type=file]');
  const fileInput  = await page.$('#rsg-root > div > main > section > section:nth-child(3) > section > section:nth-child(1) > article > div:nth-child(2) > div.rsg--preview-60 > div > section > div > input[type=file]');
  await fileInput.uploadFile("./test/playground.js");

  ///HERE
  await fileInput.evaluate(upload => upload.dispatchEvent(new Event('change', { bubbles: true })));
  ///

  await page.close();
})();

Upvotes: 8

Related Questions