【问题标题】:How to remove uploaded image(if any) from canvas?如何从画布中删除上传的图像(如果有)?
【发布时间】:2021-07-23 07:10:51
【问题描述】:

在我的代码中,如果有图像被成功删除,但是当画布中有多个图像时,我怎么知道我必须调用 Remove_image() 函数多少次? 我的条件是如果 body 发现 canvas 为 null 然后上传图片,否则删除上传的图片。

这是我的代码

cy.get('body').each((body) => {
        if (body.find(':nth-child(2) > .front-upload > :nth-child(2) > .table > .table-cell').length > 0) {

            const filepath = "Front.jpg"
            imageupload.fileuploadInput().attachFile(filepath, { subjectType: 'drag-n-drop' })
            cy.wait(60000)
            cy.Flip_image()
        }
        else {

            // REMOVE ELEMENT
            cy.log('Remove Image')
           // There is 5 uploaded image that's why this function call 5 times But this is not right structure.       
            cy.Remove_image()
            cy.Remove_image()
            cy.Remove_image()
            cy.Remove_image()
            cy.Remove_image()
            
            // UPLOAD IMAGE
            cy.wait(1000)
            const filepath = "Front.jpg"
            imageupload.fileuploadInput().attachFile(filepath, { subjectType: 'drag-n-drop' })
            cy.wait(60000)
            cy.Flip_image()
        }

    })

【问题讨论】:

  • 如果你在这里做一个sn-p,我们可以更好地帮助你

标签: javascript jquery cypress


【解决方案1】:

这就是我的处理方式,

假设图像与单元格一样多

cy.get(':nth-child(2) > .front-upload > :nth-child(2) > .table')
  .then($table => {

    const imageCount = $table.find('.table-cell').length;  

    for (let i = 0; i < imageCount; i++) {
      cy.Remove_image()
    })

    // UPLOAD IMAGE
    cy.wait(1000)
    const filepath = "Front.jpg"
    imageupload.fileuploadInput().attachFile(filepath, { subjectType: 'drag-n-drop' })
    cy.wait(60000)
    cy.Flip_image()

  })

如果某些单元格可能为空,则可以改为计数&lt;img&gt; 标签

const imageCount = $table.find('img').length;  

【讨论】:

  • 我喜欢你的解决方案。虽然有 1 条建议 - 添加断言图像已作为 for 循环的一部分被删除。
  • @RosenMihaylov 这将是一个好主意,这取决于 cy.Remove_image() 内部的内容,即对 DOM 的影响。我无法真正解决这里的所有行为。
猜你喜欢
  • 1970-01-01
  • 2020-10-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 2012-09-04
  • 1970-01-01
  • 2011-03-28
相关资源
最近更新 更多