describe ('Upload Test' , function(){
it('Upload Test' , function(){
cy.visit('https://document.online-convert.com/convert/csv-to-excel')
cy.get('#fileUploadButton').click()
const catalogue ='../integration/Example.csv';
cy.get('#file').attachFile(catalogue);
cy.get('#multifile-submit-button-main').click()
})
})
我嘗試上傳 csv 檔案并單擊轉換它,在 cypress 測驗結果中顯示該檔案已上傳但實際上并未上傳。 在此處輸入圖片說明
uj5u.com熱心網友回復:
我希望您使用的是cypress-file-upload。您的檔案似乎在集成檔案夾中。我建議將 Cypress 測驗所需的所有檔案放在 cypress/fixtures 檔案夾中,并將它們稱為 Fixtures。
uj5u.com熱心網友回復:
我也強烈建議使用cypress-file-upload。
有了它,你可以這樣做:
首先,您獲得檔案的輸入欄位。然后你可以使用 .attachFile 來宣告源并給它一個名字
cy.get('.FileInput input[type=file]')
.attachFile({filePath: 'Example.csv', fileName});
uj5u.com熱心網友回復:
在您附加檔案后,有幾個附加到輸入的事件需要觸發。
用戶將單擊“選擇檔案”,作業系統將打開一個檔案選擇對話框。由于賽普拉斯無法控制該對話框,因此您可以有效地用.attachFile(...).
附加檔案后,上傳將自動開始,完成后,檔案名將出現在檔案串列元素中。
我們需要附加的元素是<input id="fileUploadInput" type="file">。
如果您檢查此元素,并查看它的事件偵聽器,則會有一個看起來很有用的change事件和自定義事件fileuploadsubmit
這是對我有用的代碼
const catalogue = "../fixtures/example.csv";
cy.get('#fileUploadInput')
.attachFile(catalogue)
// this just confirms the internal file property of the element
// i.e confirm the attachFile worked
// You can leave it out, it does not affect the process
cy.get('#fileUploadInput')
.its('0.files')
.its('0')
.its('name')
.should('eq', 'example.csv')
// Now trigger the change event
cy.get('#fileUploadInput')
.trigger('change', {force:true}) // force because the input is hidden
// And the fileuploadsubmit event
// Note that after change event, the input is detached from DOM
// so we need to re-query for the id
cy.get('#fileUploadInput')
.trigger('fileuploadsubmit', {force:true})
// Confirm the file is in the file list
// if it's a large file, allow enough time with a timeout option
cy.contains('span', 'example.csv', {timeout: 10000})
// Now start the conversion
cy.get('#multifile-submit-button-main').click()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/339304.html
