简体   繁体   English

在Jest中使用puppeteer上传文件

[英]Upload a file with puppeteer in Jest

I am using Jest and have puppeteer set up as in this repository , which is linked to from the Jest documentation . 我正在使用Jest,并在此存储库中设置了puppeteer, 该存储库Jest文档链接。

I am trying to write some automated smoke tests on a WordPress website using puppeteer. 我正在尝试使用puppeteer在WordPress网站上编写一些自动烟雾测试。 One of the tests attempts to upload an image to the WordPress Media Library. 其中一个测试尝试将图像上载到WordPress媒体库。

This is the test: 这是测试:

it('Create test media', async () => {
  // go to Media > Add New
  await page.goto(`${env.WP_HOME}/wp/wp-admin/media-new.php`)
  const display = await page.evaluate(() => {
    const el = document.querySelector('#html-upload-ui')
    return window.getComputedStyle(el).display
  })
  if (display !== 'block') {
    // ensure we use "built-in uploader" as it has `input[type=file]`
    await page.click('.upload-flash-bypass > a')
  }
  const input = await page.$('#async-upload')
  await input.uploadFile(testMedia.path)
})

The file input field's value is populated as expected (I know this because if I save out a screenshot after the call to uploadFile it shows the path of the file in the input), and the form is submitted, however when I go to view the media library there are no items. 文件输入字段的值按预期填充(我知道这是因为如果我在调用uploadFile之后保存了屏幕截图,它会在输入中显示文件的路径),并且表单已提交,但是当我去查看时媒体库没有物品。

I have tried the following amendments to the uploadFile part of the test, to no avail: 我已尝试对uploadFile部分测试进行以下修改,但无济于事:

// 1. attempt to give time for the upload to complete
await input.uploadFile(testMedia.path)
await page.waitFor(5000)

// 2. attempt to wait until there is no network activity
await Promise.all([
  input.uploadFile(testMedia.path),
  page.waitForNavigation({waitUntil: 'networkidle0'})  
])

// 3. attempt to submit form manually (programmatic)
input.uploadFile(testMedia.path)
page.evaluate(() => document.querySelector('#file-form').submit())
await page.waitFor(5000) // or w/ `waitForNavigation()`

// 4. attempt to submit form manually (by interaction)
input.uploadFile(testMedia.path)
page.click('#html-upload')
await page.waitFor(5000) // or w/ `waitForNavigation()`

The problem is that file uploading doesn't work when connecting to a Browser instance via WebSocket as in jest-puppeteer-example . 问题是当通过WebSocket连接到浏览器实例时文件上传不起作用,就像在jest-puppeteer-example (GitHub issue here: #2120 .) (GitHub问题: #2120 。)

So instead of doing that just use puppeteer.launch() directly when setting up your test suite (instead of via the custom "Jest Node environment" ): 因此,在设置测试套件时(而不是通过自定义的“Jest Node环境” ),不要直接使用puppeteer.launch() ):

let browser
  , page

beforeAll(async () => {
  // get a page via puppeteer
  browser = await puppeteer.launch({headless: false})
  page = await browser.newPage()
})

afterAll(async () => {
  await browser.close()
})

You also then have to manually submit the form on the page, as in my experience uploadFile() doesn't do this. 然后您还必须在页面上手动提交表单,因为在我的经验中uploadFile()不会这样做。 So in your case, for the WordPress Media Library single file upload form, the test would become: 因此,对于您的情况,对于WordPress媒体库单文件上载表单,测试将变为:

it('Create test media', async () => {
  // go to Media > Add New
  await page.goto(`${env.WP_HOME}/wp/wp-admin/media-new.php`)
  const display = await page.evaluate(() => {
    const el = document.querySelector('#html-upload-ui')
    return window.getComputedStyle(el).display
  })
  if (display !== 'block') {
    // ensure we use the built-in uploader as it has an `input[type=file]`
    await page.click('.upload-flash-bypass > a')
  }
  const input = await page.$('#async-upload')
  await input.uploadFile(testMedia.path)
  // now manually submit the form and wait for network activity to stop
  await page.click('#html-upload')
  await page.waitForNavigation({waitUntil: 'networkidle0'})
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 puppeteer 将 PDF 保存到文件 - Save PDF to File using puppeteer Github 推送错误。 尝试使用 git-lfs 跟踪 puppeteer chromium 后仍然出现大文件错误。 为什么它甚至试图上传这个? - Github push error. Getting large file errors still after trying to track puppeteer chromium with git-lfs. Why is it even trying to upload this? 错误:开玩笑:运行 globalSetup 时出错 - /home/pptruser/app/node_modules/jest-environment-puppeteer/setup.js - Error: Jest: Got error running globalSetup - /home/pptruser/app/node_modules/jest-environment-puppeteer/setup.js 如何使用 headless: true 使用 puppeteer 下载文件? - How to download file with puppeteer using headless: true? Puppeteer chrome 允许多个文件下载 - Puppeteer chrome allow multiple file download 安装 puppeteer 导致“文件已存在”错误 - Installing puppeteer results in “file already exists” error 如何使用 Puppeteer 将数据写入文件? - How to write data to a file using Puppeteer? Puppeteer UnhandledPromiseRejectionWarning - Puppeteer UnhandledPromiseRejectionWarning Cef-上传文件但未显示OpenFileDialog - Cef - upload file without shown OpenFileDialog 从没有 puppeteer 功能的 puppeteer 启动 chromium - Launch chromium from puppeteer without puppeteer functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM