【问题标题】:how can i upload file by using xpath in cypress如何在 cypress 中使用 xpath 上传文件
【发布时间】:2019-08-16 09:43:56
【问题描述】:

如何在cypress中使用xpath上传文件?

得到如下所述的错误...

const xpath = require('cypress-xpath')
describe('File upload Demo', () => {
  
  Cypress.Commands.add('uploadFile', { prevSubject: 'element' }, (subject, fileName) => {
      console.log('subject', subject)
      return cy.fixture(fileName, 'base64')
        .then(Cypress.Blob.base64StringToBlob)
        .then(blob => {
          console.log('blob', blob)
          const el = subject[0]
          if (el != null) {
            const testFile = new File([blob], fileName)
            const dataTransfer = new DataTransfer()
            dataTransfer.items.add(testFile)
            el.files = dataTransfer.files
          }
          return subject          
        })
      }
    )
  
    it('upload file test', () => {

      cy.visit('https://tus.io/demo.html')

      cy.xpath('//input[@type="file"]').upload('401k_deferral.xlsx')
      
      cy.get('[class="button primary"]').should('have.class', 'button primary')
    })
  
  })

TypeError: cy.xpath(...).upload 不是函数

【问题讨论】:

    标签: cypress


    【解决方案1】:

    您添加的自定义命令名为uploadFile,但您尝试调用upload

    试试这个:

          cy.xpath('//input[@type="file"]').uploadFile('401k_deferral.xlsx')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多