GithubHelp home page GithubHelp logo

elaichenkov / cy-verify-downloads Goto Github PK

View Code? Open in Web Editor NEW
67.0 2.0 17.0 110.61 MB

Cypress custom command to wait and verify that file has been downloaded

License: MIT License

JavaScript 91.96% HTML 8.04%
cypress cypress-io cypress-plugin cypress-example javascript wait download verify assert cypress-custom-command

cy-verify-downloads's Introduction

Stand With Ukraine

cy-verify-downloads tests Total npm downloads

logo

Custom Cypress command to wait and verify that file is downloaded.

Installation

npm i -D cy-verify-downloads

Extend Cypress command

This package extends Cypress' cy command.

For Cypress v10+:

Add this line to your project's cypress/support/e2e.js:

require('cy-verify-downloads').addCustomCommand();

Then you need to add the following lines of code to your project's cypress.config.js:

const { verifyDownloadTasks } = require('cy-verify-downloads');

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('task', verifyDownloadTasks);
    },
  },
});

For Cucumber:

Additionally, you may need to install and add node polyfill in your support/e2e.js file:

  1. Install the polyfill module:
npm i -D esbuild-plugin-polyfill-node
  1. Import the following code in your support/e2e.js file:
const { polyfillNode } = require('esbuild-plugin-polyfill-node');
  1. Add the following code in your plugins property:
module.exports = defineConfig({
  e2e: {
    async setupNodeEvents(on, config) {
      const bundler = createBundler({
        plugins: [polyfillNode({ polyfills: { crypto: true } }), createEsbuildPlugin(config)],
      });
    }
  }
});

For Cypress v9:

So, you need to add this line to your project's cypress/support/commands.js:

require('cy-verify-downloads').addCustomCommand();

And add the following lines to your project's cypress/plugins/index.js:

const { verifyDownloadTasks } = require('cy-verify-downloads');

module.exports = (on, config) => {
  on('task', verifyDownloadTasks)
}

Usage

Then, in your test, you can use it like this:

cy.verifyDownload('picture.png');

// verify download by file extension or partial filename
cy.verifyDownload('.png', { contains: true });
cy.verifyDownload('pic', { contains: true });

// or increase timeout
cy.verifyDownload('archive.zip', { timeout: 25000 });

// or increase timeout and interval pooling
cy.verifyDownload('archive.zip', { timeout: 25000, interval: 600 });

Types

Autocompletion

To enable IntelliSense information and autocomplete you have to include types in the tsconfig.json file:

{
  "compilerOptions": {
    "types": ["cypress", "cy-verify-downloads"]
  }
}

Author

Yevhen Laichenkov [email protected]

License

MIT

cy-verify-downloads's People

Contributors

ctharings avatar dependabot[bot] avatar elaichenkov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cy-verify-downloads's Issues

Test argument "title" should be a string. Received type "object"

Immediately after installing the cy-verify-downloads plugin, I started receiving this error message and could not run any further tests:

TypeError
The following error originated from your test code, not from Cypress.
> Test argument "title" should be a string. Received type "object"

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

Cypress could not associate this error to any specific test.

Here are the steps I followed, as per the instructions in the installation:

  1. Run npm i -D cy-verify-downloads
  2. Added require('cy-verify-downloads').addCustomCommand(); to cypress/support/commands.js:
  3. Added the mentioned code to cypress/plugins/index.js:

I ran the tests as they were before, and I got the error message. After uninstalling, I didn't get the error anymore.

Can you please take a look and let me know if there's something I'm missing?

I'm on Cypress version 9.0.0.

Problems with adding polyfill

Hi, Thanks for the contribution.
as some another guys i having a problem with "fs" error. but i cant realize how to apply the decision with polyfill because i having such settings in setupNodeEvents as you can see at the picture and i catching TS errors. Do you have some thoughts how to attach polyfill code here?
And is it possible that in the future major versions this problem will be solved in some maybe another way?
settings

Adding a timeout option does not wait enough as it should

I have a heavy file download operation, that on Jenkins takes more than a minute to fully have the file downloaded, so I counted on setting the timeout option to some high value, around 2 minutes, but it just didn't wait till then. It always waits for 60 seconds no matter how big/small my timeout is

Webpack compilation error

When running with Typescript + Cypress 10

Error: Webpack Compilation Error
Module not found: Error: Can't resolve 'path' in '/Users/xxxx/WebstormProjects/canopy-angular/node_modules/cy-verify-downloads/src'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
	- install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "path": false }

Module not found: Error: Can't resolve 'fs' in '/Users/xxxx/WebstormProjects/canopy-angular/node_modules/cy-verify-downloads/src'

Module not found: Error: Can't resolve 'crypto' in '/Users/xxxx/WebstormProjects/canopy-angular/node_modules/cy-verify-downloads/src'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
	- install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "crypto": false }
    at Watching.handle [as handler] (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/@cypress/webpack-preprocessor/dist/index.js:180:23)
    at /Users/xxxx/WebstormProjects/canopy-angular/node_modules/webpack/lib/Watching.js:290:9
    at Hook.eval [as callAsync] (eval at create (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/tapable/lib/Hook.js:18:14)
    at Watching._done (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/webpack/lib/Watching.js:287:28)
    at /Users/xxxx/WebstormProjects/canopy-angular/node_modules/webpack/lib/Watching.js:209:21
    at Compiler.emitRecords (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/webpack/lib/Compiler.js:919:5)
    at /Users/xxxx/WebstormProjects/canopy-angular/node_modules/webpack/lib/Watching.js:187:22
    at /Users/xxxx/WebstormProjects/canopy-angular/node_modules/webpack/lib/Compiler.js:885:14
    at Hook.eval [as callAsync] (eval at create (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/tapable/lib/Hook.js:18:14)
    at /Users/xxxx/WebstormProjects/canopy-angular/node_modules/webpack/lib/Compiler.js:882:27
    at /Users/xxxx/WebstormProjects/canopy-angular/node_modules/neo-async/async.js:2818:7
    at done (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/neo-async/async.js:3522:9)
    at Hook.eval [as callAsync] (eval at create (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/xxxx/WebstormProjects/canopy-angular/node_modules/tapable/lib/Hook.js:18:14)
    at /Users/xxxx/WebstormProjects/canopy-angular/node_modules/webpack/lib/Compiler.js:736:33
    at /Users/xxxx/WebstormProjects/canopy-angular/node_modules/graceful-fs/graceful-fs.js:143:16
    at /Users/xxxx/Library/Caches/Cypress/10.3.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/graceful-fs/graceful-fs.js:143:16
    at /Users/xxxx/Library/Caches/Cypress/10.3.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/graceful-fs/graceful-fs.js:143:16
    at /Users/xxxx/Library/Caches/Cypress/10.3.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/graceful-fs/graceful-fs.js:61:14

.crdownload will pass the verification with {contains: true}

When downloading a file in google Chrome the ending .crdownlaod gets appended to the downloaded file and removed when the file is downloaded completly. The file is already in the downloads folder when first downloaded.

Example

01_01_22_text.txt.crdownload will pass cy.verifyDownload('text.txt', {contains: true, timeout: 10000, interval: 250}); even though it is still downloading the file.

Expected behaviour

Only let files pass that are fully downloaded to ensure they are really downloaded. After the validation with the plugin the contents of the file can be validated.

Unable to verify the downloaded file

Hi,

Thanks for the contribution!

I'm unable to verify the downloaded file where I used another npm package cypress-downloadfile
Here is the video recording of the issue: https://www.youtube.com/watch?v=Gpv8wCdJsKo
Please go through it and let me know the solution.

Note: I feel its all about the location where file is downloaded and the location where the verification is being done.

Regards,
Tejkumar

[Enhancement] Should we enhance verify download with filename contains a substring for dynamic validation

Thank you very much for your great effort to develop this package @elaichenkov . Could you please enhance ability to validate the filename with regex validation? For example in my test, the downloaded file is named with <downloadedFilename>_DD-mmm-yyyy_hhmmss. So could you please do an enhancement so that this verifyDownload method can validate if the file whose name includes substring exists?

Example:
We have a list of download files:

  • downloadFile1-25-Feb-2022_011111.txt
  • downloadFile2-25-Feb-2022_011133.txt

Then we can validate the downloaded file exist with something like: verifyDownloadContains('downloadFile2-25-Feb-2022') with Passed result

Thank you very much

In Cypress headless mode download.htm file is created

When starting the tests in a Cypress headless mode, .crdownload is not appended to the downloaded file and removed when the file is downloaded completely, instead the completely new file is being created inside cypress/downloads folder with the name downloads.htm.crdownload ('downloads.htm'). So at the end of testing there are 2 files:

valid_file.txt
download.htm

Why is it happening?

CY v12.17.0
cy-verify-downloads v0.1.15

Thank you in advance.

Option "contains" don't work if the file not exist (randomBytes method not found)

Hello,
Thanks for this project !

I have a question, I would check if a file was download and start by a prefix.
But I need to use a timeout too.

So I combinated options like this :

cy.verifyDownload(prefix, { contains: true, timeout: 20*1000, interval: 500 });

And it not work. The contains option work, but if the file don't exist, the timeout was ignore, because the method :

const getTempName = () => ${randomBytes(8)}-temp-file-name-${randomBytes(8)};

Was in error :
image

Thanks :)

The 'task' event has not been registered in the setupNodeEvents method.

Hello,

I have a little problem with your plugin. I get this error message:

cy.task('findFiles') failed with the following error:

The 'task' event has not been registered in the setupNodeEvents method. You must register it before using cy.task()

const { isFileExist } = require('cy-verify-downloads')

module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
on('task', isFileExist)
realHover.install(on)
},
},
})

Cypress version 12.12.00

Thx for your help

Can we enhance verify download within a specific folder using {contains:true}

Hi @elaichenkov,

Thank you so much for this plugin, but my use case is different , I need to verify text file i.e Download_error_DD_MM_YY"

I can simply do this by using cy.verifyDownload('.txt', { contains: true });.
but the issue is it is pointing to the default downloads folder i.e cypress/Downloads. the same is working without contains but not with contains.

Working Condition - cy.verifyDownload('cypress/downloads/My_downloads/test.pdf');
Not Working Condition - cy.verifyDownload('cypress/downloads/My_downloads/test.pdf', {contains; true);

Changing downloads directory is not an option in my case, because downloads are unzipping into My Downloads folder.

Can you please help me on this ?

Unable to addcustom command for verify downloads

require('cy-verify-downloads').addCustomCommand()

I have installed cy-verify-downloads and then add the above mentioned custom command on command.js file
But it through the error as 'cy-verify-downloads is not module'
So I am unable to proceed further please help me 5o resolve this error

Doesn't work with "cypress-vite" preprocessor

While it works fine with default Cypress preprocessor, it doesn't work with cypress-vite preprocessor.
I get the following error:

TypeError: join is not a function

It seems it cannot resolve the module in your plugin:

const { join } = require('path');

All other tests, which don't use verifyDownload command, work without any issues.
I don't know whether I'd better submit the issue in cypress-vite repo, but maybe you can suggest how to fix it?
Maybe some additional vite configuration is needed?

isFileExists is on a loop after file is downloaded

Hi!
Thanks for this amazing plugin!

However, i am facing an issue. Will describe in brief whats going on.

There is a isFileExist method used in the plugin, which checks for the file at the download location but doesn't register or assert that the file is found.
I physically checked during runtime, File is actually present.
And more over, the isFileExist is in a loop.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.