Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Drag and drop zip file #13364

Closed
LiutskoOlga opened this issue Apr 6, 2022 · 7 comments
Closed

[Feature] Drag and drop zip file #13364

LiutskoOlga opened this issue Apr 6, 2022 · 7 comments

Comments

@LiutskoOlga
Copy link

LiutskoOlga commented Apr 6, 2022

Hello, could you help me please with drug and drop zip file?

const buffer = fs.promises.readFile('FILE_PATH')
    const dataTransfer = await this.page.evaluateHandle((data) => {
      const dt = new DataTransfer()
      const file = new File([data.toString()], 'equity.box.gz', { type: 'application/gzip' })
      dt.items.add(file)
      return dt
    }, buffer)
    await this.page.dispatchEvent('LOCATOR', 'drop', { dataTransfer })
```

but when run it in browser, I got and error

```
Uncaught (in promise) Error: invalid gzip data
    at gzs (browser.js:937:9)
    at gunzipSync (browser.js:1266:32)
    at gunZip (compress.ts:22:10)
    at BoxImportStrategy.maybeUnzip (ImportStrategy.ts:122:14)
    at async BoxImportStrategy.preProcess (ImportStrategy.ts:131:22)
    at async importFileUX (importFileUx.tsx:74:22)
```



Thank you for your help!
@pavelfeldman
Copy link
Member

pavelfeldman commented Apr 6, 2022

Let's consider translating Buffer into the ArrayBuffer as a value-type.

@LiutskoOlga
Copy link
Author

Resolved with next code

const buffer = fs.readFileSync('PATH-TO-FILE').toString('base64')

    const dataTransfer = await this.page.evaluateHandle(async (data) => {
      const dt = new DataTransfer()

      const blobData = await fetch(data).then(res => res.blob())

      const file = new File([blobData], 'equity.box.gz', { type: 'application/gzip' })
      dt.items.add(file)
      return dt
    }, 'data:application/octet-stream;base64,' + buffer)

    await this.page.dispatchEvent('LOCATOR', 'drop', { dataTransfer })
  }

@Jeromearsene
Copy link

From example of @LiutskoOlga, I've make a generic function:

import { Page } from '@playwright/test';
import { readFileSync } from 'fs';

const dragAndDropFile = async (
  page: Page,
  selector: string,
  filePath: string,
  fileName: string,
  fileType = ''
) => {
  const buffer = readFileSync(filePath).toString('base64');

  const dataTransfer = await page.evaluateHandle(
    async ({ bufferData, localFileName, localFileType }) => {
      const dt = new DataTransfer();

      const blobData = await fetch(bufferData).then((res) => res.blob());

      const file = new File([blobData], localFileName, { type: localFileType });
      dt.items.add(file);
      return dt;
    },
    {
      bufferData: `data:application/octet-stream;base64,${buffer}`,
      localFileName: fileName,
      localFileType: fileType,
    }
  );

  await page.dispatchEvent(selector, 'drop', { dataTransfer });
};

It will be nice to offer this directly in Playwright. Currently there is page.dragAndDrop. With this function, we can imagine adding the page.dragAndDropFile method.

@punkpeye
Copy link

I released a utility createDataTransfer that abstracts this.

@pavelfeldman
Copy link
Member

Why was this issue closed?

Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors.

If you disagree with this closure, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated.

@OmarovIV
Copy link

What is "data" here? Should it have been announced before?

Resolved with next code

const buffer = fs.readFileSync('PATH-TO-FILE').toString('base64')

    const dataTransfer = await this.page.evaluateHandle(async (data) => {
      const dt = new DataTransfer()

      const blobData = await fetch(data).then(res => res.blob())

      const file = new File([blobData], 'equity.box.gz', { type: 'application/gzip' })
      dt.items.add(file)
      return dt
    }, 'data:application/octet-stream;base64,' + buffer)

    await this.page.dispatchEvent('LOCATOR', 'drop', { dataTransfer })
  }

@sergtimosh
Copy link

sergtimosh commented May 14, 2024

What is "data" here? Should it have been announced before?

The second argument to this.page.evaluateHandle() is 'data:application/octet-stream;base64,' + buffer.
This is the value that gets passed as data to the function being evaluated on the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants