【问题标题】:How to implement the Drop or Trigger properly inside of Cypress?如何在 Cypress 内部正确实现 Drop 或 Trigger?
【发布时间】:2021-06-23 21:41:24
【问题描述】:

我目前正在使用“Drag File Here”组件与 Salesforce 合作,似乎如果我使用“Drag and Drop”NPM 依赖项,它将不会出现任何错误,但屏幕永远不会弹出。

注意:删除文件后,我们应该会看到一个弹出窗口,其中包含用于存储该文件的所有文件选项。

如果我使用“触发操作”执行此操作,我将收到以下错误消息:

我的代码如下:

describe("Checklist - Drag and Drop Functionality", function () {
  beforeEach(() => {
    cy.login();
  });

  it("Drag and Drop Files", () => {
    cy.viewOpportunityRecord();
    cy.wait(10000);
    // Click to view  More Tabs inside of the opportunity
    cy.get('button[title="More Tabs"]', { delay: 1000, force: true }).click({multiple: true, force: true});

    cy.get("span.slds-truncate")
      .contains("Checklist (beta)")
      .dblclick({ multiple: true });

    cy.wait(10000)

    const filePath = 'html-cheat-sheet.csv'
    
    cy.xpath('//*[@id="tab-18"]/slot/flexipage-component2/slot/flexipage-aura-wrapper/div/section[1]/h2/div[2]/button[3]').click({force: true})

    cy.wait(10000)
    cy.xpath('/html/body/div[4]/div[1]/section/div[1]/div[2]/div[2]/div[1]/div/div/div/div/div/one-record-home-flexipage2/forcegenerated-adg-rollup_component___force-generated__flexipage_-record-page___-opportunity_-record_-page___-opportunity___-v-i-e-w/forcegenerated-flexipage_opportunity_record_page_opportunity__view_js/record_flexipage-record-page-decorator/div[1]/records-record-layout-event-broker/slot/slot/flexipage-record-home-with-subheader-template-desktop2/div/div[3]/div[1]/slot/slot/flexipage-component2[1]/slot/flexipage-tabset2/div/lightning-tabset/div/slot/slot/slot/flexipage-tab2[16]/slot/flexipage-component2/slot/flexipage-aura-wrapper/div/div[1]/div[1]/ul/li[1]/section/div[2]/div[2]/div[4]').trigger('drop', {filePath})

  })

})


另外,要运行拖动文件,我认为我可以这样做:

const fileLocated = cy.xpath('/html/body/div[4]/div[1]/section/div[1]/div[2]/div[2]/div[1]/div/div/div/div/div/one-record-home-flexipage2/forcegenerated-adg-rollup_component___force-generated__flexipage_-record-page___-opportunity_-record_-page___-opportunity___-v-i-e-w/forcegenerated-flexipage_opportunity_record_page_opportunity__view_js/record_flexipage-record-page-decorator/div[1]/records-record-layout-event-broker/slot/slot/flexipage-record-home-with-subheader-template-desktop2/div/div[3]/div[1]/slot/slot/flexipage-component2[1]/slot/flexipage-tabset2/div/lightning-tabset/div/slot/slot/slot/flexipage-tab2[16]/slot/flexipage-component2/slot/flexipage-aura-wrapper/div/div[1]/div[1]/ul/li[1]/section/div[2]/div[2]/div[4]')
    
    cy.xpath('//*[@id="tab-18"]/slot/flexipage-component2/slot/flexipage-aura-wrapper/div/section[1]/h2/div[2]/button[3]').click({force: true})

    cy.wait(10000)
    cy.xpath('//*[@id="fileTable"]').xpath('//*[@id="fileTable"]/tbody/tr[1]/td[1]/div/button').contains('12 Month Payment History').drag(fileLocated, { force: true})

主要问题是我遇到了以下问题:

CypressError
Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

  > cy.wrap()

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: javascript drag-and-drop automated-tests cypress salesforce-lightning


    【解决方案1】:

    不知道这样能不能解决,但是你不能这样做

    const fileLocated = cy.xpath(<horrendously-long-xpath-selector>)
    ...
    cy.xpath(<another-long-xpath-selector>).drag(fileLocated, { force: true})
    

    因为fileLocated 不是一个元素(这是您作为放置目标所需要的),它将是一个 Chainer。

    相反,这样做

    cy.xpath(<horrendously-long-xpath-selector>)
      .then( targetItem => {
    
        ...
        cy.xpath(<another-long-xpath-selector>).drag( targetItem, { force: true})
      })
    

    【讨论】:

      【解决方案2】:

      为了消除这个问题,我们可以在元素所在的 HTML 页面中添加一个数据属性。之后,我能够从该元素获取 xpath,并且能够附加文件。

      例子:

      cy.xpath('//*[@id="cy-dropzone"]').attachFile('latestLead.json', {subjectType: 'drag-n-drop'})
      

      现在,问题已经解决了!

      【讨论】:

      • 看来您还需要安装包cypress-file-upload 才能正常工作。
      猜你喜欢
      • 2021-11-29
      • 2016-12-05
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      相关资源
      最近更新 更多