cypress-file-upload icon indicating copy to clipboard operation
cypress-file-upload copied to clipboard

pdf upload is blank, DOCX and DOC are corrupted on v3.5.3 onwards

Open cooperisduhace opened this issue 5 years ago • 6 comments

Hi!

We are looking into using cypress for our automation needs and one of our features include uploading files varying from pngs, pdf, and documents. As I tried to do this with my code(to be posted below), All files were uploaded using your library BUT it seems pdf, docx and doc were corrupted. pdf file was all white while docx and doc were corrupted as a file. Below is my code below

` // Login as Admin cy.visit('http://localhost/') cy.get(loginSelectors.userNameField).type(loginData.userName) cy.get(loginSelectors.passwordField).type(loginData.password) cy.get(loginSelectors.loginButton).click() cy.get(loginSelectors.welcomeUserLabel).should('to.have.text', 'Hello, Will Glass, Administrator.')

	// Upload File on Implementation > Org > Resources > Documents
	//PNG file upload
	const wallywest_png = 'wallywest.png'
	cy.visit(createDocumentUrl)
	cy.get(createDocumentSelectors.typeSelect).select(companyCorner)
	cy.get(createDocumentSelectors.languageSelect).select(languageEnglishUS)
	cy.get(createDocumentSelectors.nameField).type('Wally West PNG File')
	cy.get(createDocumentSelectors.descriptionField).type(description)
	// For cypress-upload-file v4.0.0+
	// cy.get(createDocumentSelectors.fileUploadInput).attachFile(wallywest_png)
	cy.fixture(wallywest_png).then(fileContent => {
		cy.get(createDocumentSelectors.fileUploadInput).upload({fileContent, fileName: wallywest_png, mimeType: 'image/png'}, {subjectType : 'input'})
	})
	cy.get(createDocumentSelectors.saveButton).click()
	cy.wait(3000) // testing purposes

	//PDF file upload
	const wallywest_pdf = 'WelcomeWallyWest_20200211.pdf'
	cy.visit(createDocumentUrl)
	cy.get(createDocumentSelectors.typeSelect).select(companyCorner)
	cy.get(createDocumentSelectors.languageSelect).select(languageEnglishUS)
	cy.get(createDocumentSelectors.nameField).type('Wally West PDF File')
	cy.get(createDocumentSelectors.descriptionField).type(description)
	cy.fixture(wallywest_pdf).then(fileContent => {
		cy.get(createDocumentSelectors.fileUploadInput).upload({fileContent, fileName: wallywest_pdf, mimeType: 'application/pdf', encoding: 'utf8'})
	})
	cy.get(createDocumentSelectors.saveButton).click()
	cy.wait(3000) // testing purposes

	//DOCX file upload
	const wallywest_docx = 'WallyWestDOCX.docx'
	cy.visit(createDocumentUrl)
	cy.get(createDocumentSelectors.typeSelect).select(companyCorner)
	cy.get(createDocumentSelectors.languageSelect).select(languageEnglishUS)
	cy.get(createDocumentSelectors.nameField).type('Wally West DOCX File')
	cy.get(createDocumentSelectors.descriptionField).type(description)
	cy.fixture(wallywest_docx).then(fileContent => {
		cy.get(createDocumentSelectors.fileUploadInput).upload({fileContent, fileName: wallywest_docx, mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', encoding: 'utf8'})
	})
	cy.get(createDocumentSelectors.saveButton).click()
	cy.wait(3000) // testing purposes

	//DOC file upload
	const wallywest_doc = 'WallyWestDOC.doc'
	cy.visit(createDocumentUrl)
	cy.get(createDocumentSelectors.typeSelect).select(companyCorner)
	cy.get(createDocumentSelectors.languageSelect).select(languageEnglishUS)
	cy.get(createDocumentSelectors.nameField).type('Wally West DOC File')
	cy.get(createDocumentSelectors.descriptionField).type(description)
	cy.fixture(wallywest_doc).then(fileContent => {
		cy.get(createDocumentSelectors.fileUploadInput).upload({fileContent, fileName: wallywest_doc, mimeType: 'application/msword', encoding: 'utf8'})
	})
	// For cypress-upload-file v4.0.0+
	//cy.get(createDocumentSelectors.fileUploadInput).upload(wallywest_doc) // UPLOADS CORRUPTED FILE
	cy.get(createDocumentSelectors.saveButton).click()`

I tried using different encodings and adding subjectTypes but to no avail on this. Can anyone look on this. I've used both v3.5.3 and the latest v4.0.3

Current Behavior: Uploads PNG, PDF, DOCX and DOC files BUT all files aside from png were corrupted. PDF was blank, DOCX and DOC are corrupted and cannot be opened.

Desired Behavior All files mentioned should not be corrupted and uploaded properly.

Versions: Cypress : 4.5.0 cypress-file-upload : 3.5.3/latest

cooperisduhace avatar May 06 '20 14:05 cooperisduhace

Hi I filed this issue weeks ago and still no reply while other issues have been looked at.

cooperisduhace avatar May 19 '20 15:05 cooperisduhace

Hi @cooperisduhace Thanks for submitting the issue! Sincere apologies for not having a look at it since I am the only main contributor to the repo.

As for the issue, I remember same stuff reproducible for XLS, and fix for that was using encoding: 'binary'. Can you please try setting that explicitly? If that works, I'll update the plugin internals to support it out of the box.

abramenal avatar May 25 '20 13:05 abramenal

As for PDF, please see related comment https://github.com/abramenal/cypress-file-upload/issues/117#issuecomment-546696082 Is that relevant for your case?

abramenal avatar May 25 '20 13:05 abramenal

.pdf defaults to encoding of utf-8. I had to change it to binary or else the pages were all blank.

akinnee avatar Jul 23 '20 22:07 akinnee

Hi, I'm having the same issue here.

this is my code:

Cypress.Commands.add('uploadContent', (file, type) => {
  var name = type + 'TestContent' + Math.floor(Math.random() * Math.floor(999999))

  cy.get('[data-element=tab-issues]').click({ force: true })
  cy.get('#button-upload-issue').click()
  cy.get('#file').attachFile({filePath: file, mimeType: 'application/pdf', encoding: 'binary'})
  cy.get('#name').clear().type(name)
  cy.get('button[type="submit"]').click()
  cy.get('.modal-body').should('not.be.visible')

I'm still getting a Blank pdf when I upload using cypress. I tried uploading the file manually and it works fine.

I'm using "cypress": "^7.0.0", "cypress-file-upload": "^5.0.2",

Please help

gparrar avatar Apr 08 '21 20:04 gparrar

Same issue here with the very same version of cypress-file-upload and cypress 9.6.1. and

.attachFile(filename);

but this fixed it

.attachFile({filePath: filename, mimeType: 'application/pdf', encoding: 'binary'})

carlosvega avatar May 23 '22 15:05 carlosvega