diff --git a/README.md b/README.md index 8c76cba..ccd4f85 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,13 @@ If autocompletion does not work out of the box you can add the following line ab ``` -## Example of command +## Example of basic command ```javascript cy.downloadFile('https://library.concordia.ca/help/technology/recovering_saved_files.pdf','mydownloads','demo.pdf') ``` +## In Version 1.5 you can now also pass in the User-Agent. If no User-Agent is passed it will give a default User-Agent called request. +```javascript +cy.downloadFile('https://library.concordia.ca/help/technology/recovering_saved_files.pdf','mydownloads','demo.pdf','MyCustomAgentName') +``` + diff --git a/cypress/integration/test-plugin/download-files.spec.js b/cypress/integration/test-plugin/download-files.spec.js index 8eb99e3..307476e 100644 --- a/cypress/integration/test-plugin/download-files.spec.js +++ b/cypress/integration/test-plugin/download-files.spec.js @@ -6,7 +6,8 @@ describe('Cypress Downloadfile Testing', () => { cy.downloadFile( 'https://speed.hetzner.de/100MB.bin', 'mydownloads', - '100MB.bin' + '100MB.bin', + 'MyAgent' ) }) it('Small Size test', () => { diff --git a/package.json b/package.json index 779ae2f..3c4783e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cypress-downloadfile", - "version": "1.1.1", + "version": "1.1.5", "description": "Cypress custom command to download files", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/addPlugin.js b/src/addPlugin.js index 5d135e1..8d59ae1 100644 --- a/src/addPlugin.js +++ b/src/addPlugin.js @@ -4,13 +4,14 @@ const path = require('path') export function downloadFile(args) { const directory = args.directory const cookieHeader = args.cookies.map(e => e.name + '=' + e.value).join(';') + const userAgent = args.userAgent || 'request' const fileName = args.fileName return new Promise((resolve, reject) => { request( { url: args.url, encoding: null, - headers: { Cookie: cookieHeader }, + headers: { Cookie: cookieHeader, 'User-Agent': userAgent }, }, function(err, res, body) { if (!res) { diff --git a/src/downloadFileCommand.js b/src/downloadFileCommand.js index 53fb8dd..2b2f15f 100644 --- a/src/downloadFileCommand.js +++ b/src/downloadFileCommand.js @@ -1,10 +1,11 @@ -Cypress.Commands.add('downloadFile', (url, dir, fileName) => { +Cypress.Commands.add('downloadFile', (url, dir, fileName, userAgent) => { return cy.getCookies().then(cookies => { return cy.task('downloadFile', { url: url, directory: dir, cookies: cookies, fileName: fileName, + userAgent: userAgent, }) }) }) diff --git a/types/index.d.ts b/types/index.d.ts index 5fff9f5..f52c5f8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -7,6 +7,6 @@ declare namespace Cypress { * @example * cy.downloadFile('http://demourl','example','demo.pdf') */ - downloadFile(url: string, directory: string, filename:string): Chainable + downloadFile(url: string, directory: string, filename:string, userAgent?:string): Chainable } } \ No newline at end of file