Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
elaichenkov committed Sep 8, 2023
2 parents 9e664fa + ef956bf commit 7793447
Show file tree
Hide file tree
Showing 15 changed files with 8,777 additions and 1,712 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: "weekly"
time: "11:00"
open-pull-requests-limit: 10
versioning-strategy: increase-if-necessary
68 changes: 68 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '43 18 * * 6'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
11 changes: 7 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
node: [ 14, 16, 18 ]

steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
- uses: actions/checkout@v3
- name: Use Node ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: '14'
node-version: ${{ matrix.node }}
cache: 'npm'
- run: npm ci
- run: npm test
89 changes: 79 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,107 @@
# cy-verify-downloads ![tests](https://github.com/elaichenkov/cy-verify-downloads/actions/workflows/test.yml/badge.svg)
[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg)](https://vshymanskyy.github.io/StandWithUkraine)

This is a Cypress custom command to wait and to verify that a file has been successfully downloaded.
# cy-verify-downloads ![tests](https://github.com/elaichenkov/cy-verify-downloads/actions/workflows/test.yml/badge.svg) [![Total npm downloads](https://img.shields.io/npm/dt/cy-verify-downloads.svg)](https://www.npmjs.com/package/cy-verify-downloads)

<div align="center">
<img
alt="logo"
src="./assets/cy.verify.png"
/>
<p>Custom Cypress command to wait and verify that file is downloaded.</p>
</div>

## Installation

```shell
npm i -D cy-verify-downloads
```

## Usage
## Extend Cypress command

cy-verify-downloads extends Cypress' cy command.
This package extends Cypress' `cy` command.

So, you need to add this line to your project's `cypress/support/commands.js`:
**For Cypress v10+:**

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

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

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

And add the following lines to your project's `cypress/plugins/index.js`:
```javascript
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:

```bash
npm i -D @esbuild-plugins/node-modules-polyfill
```
2. Import the following code in your `support/e2e.js` file:

```js
const { NodeModulesPolyfillPlugin } = require('@esbuild-plugins/node-modules-polyfill');
```

3. Add the following code in your `plugins` property:

```js
module.exports = defineConfig({
e2e: {
async setupNodeEvents(on, config) {
const bundler = createBundler({
// add polyfill ⬇ NodeModulesPolyfillPlugin
plugins: [NodeModulesPolyfillPlugin(), createEsbuildPlugin(config)],
});
}
}
});
```
Moreover, you can take a look at the [comment](https://github.com/elaichenkov/cy-verify-downloads/issues/51#issuecomment-1237978973) with detailed example of adding node polyfill in your config.

**For Cypress v9:**

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

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

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

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

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

## Usage

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

```javascript
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 });

Expand All @@ -48,6 +114,7 @@ cy.verifyDownload('archive.zip', { timeout: 25000, interval: 600 });
![Autocompletion](./assets/autocompletion.gif?raw=true)

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

```json
{
"compilerOptions": {
Expand All @@ -57,7 +124,9 @@ To enable IntelliSense information and autocomplete you have to include types in
```

## Author

Yevhen Laichenkov <[email protected]>

## License

[MIT](LICENSE)
Binary file added assets/cy.verify.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { defineConfig } = require('cypress');
const { verifyDownloadTasks } = require('./src/index');

module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', verifyDownloadTasks);
},
baseUrl: 'http://localhost:8039',
},
});
3 changes: 0 additions & 3 deletions cypress.json

This file was deleted.

31 changes: 31 additions & 0 deletions cypress/e2e/verify-download.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
describe('verify download functionality', () => {
beforeEach(() => {
cy.visit('/');
});

it('downloads small file', () => {
cy.get('[data-cy="small"]').click();

cy.verifyDownload('small-file-10MB.zip');
});

it('downloads large file', () => {
cy.get('[data-cy="large"]').click();

cy.verifyDownload('large-file-100MB.zip', { timeout: 55000, interval: 500 });
});

it('downloads with contains option', () => {
cy.exec(`rm -rf ${Cypress.config('downloadsFolder')}`);
cy.get('[data-cy="large"]').click();

cy.verifyDownload('large-file-100MB', { contains: true });
});

it('downloads file with extension and contains option', () => {
cy.exec(`rm -rf ${Cypress.config('downloadsFolder')}`);
cy.get('[data-cy="large"]').click();

cy.verifyDownload('large-file-100MB.zip', { contains: true });
});
});
17 changes: 0 additions & 17 deletions cypress/integration/verify-download.test.js

This file was deleted.

5 changes: 0 additions & 5 deletions cypress/plugins/index.js

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 7793447

Please sign in to comment.