-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic test from CSV using Webpack loader #834
Open
vineet-kothari
wants to merge
4
commits into
cypress-io:master
Choose a base branch
from
vineet-kothari:vk/dynamicTestCSV-usingWebpackLoader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
examples/fundamentals__dynamic-tests-from-csv-using-webpack/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Dynamic tests from CSV data using Webpack Preprocessor | ||
|
||
There are times when you need to generate tests using external sources such as CSV. We cannot load the CSV file directly from the spec file (unless we have a special CSV file bundler), and we cannot use the `cy.readFile` command - because by then it is too late to define new tests. However we can use `@cypress/webpack-preprocessor` and `csv-loader` thus giving us the ability to load the files and best part of you dont have to specify them in the your `cypress.config.js` each time a new CSV file is to be used. | ||
|
||
We can do this by adding those two new devDependencies in `package.json` and then load them in [setupNodeEvents](cypress.config.js) function. This config object will be available in each spec file. You then can get the list of records before the tests are created, see [csv-spec-using-WebpackLoader.cy.js](./cypress/e2e/csv-spec-using-WebpackLoader.cy.js) | ||
|
||
```js | ||
//package.json | ||
"devDependencies": { | ||
"@cypress/webpack-preprocessor": "^5.17.1", | ||
"csv-loader": "^3.0.5" | ||
} | ||
|
||
// `setupNodeEvents` | ||
const webpackPreprocessor = require('@cypress/webpack-preprocessor') | ||
const { defineConfig } = require('cypress') | ||
module.exports = defineConfig({ | ||
e2e: { | ||
supportFile: false, | ||
setupNodeEvents (on, config) { | ||
const webpackDefaults = webpackPreprocessor.defaultOptions | ||
webpackDefaults.webpackOptions.module.rules.push({ | ||
test: /\.csv$/, | ||
loader: 'csv-loader', | ||
options: { | ||
dynamicTyping: true, | ||
header: true, | ||
skipEmptyLines: true, | ||
}, | ||
}) | ||
on('file:preprocessor', webpackPreprocessor(webpackDefaults)) | ||
}, | ||
}, | ||
}) | ||
|
||
|
||
// cypress/e2e/csv-spec.cy.js | ||
const csvUsers = require('../fixtures/users.csv') | ||
|
||
csvUsers.forEach((user) => { | ||
it(`has the user ${user['first name']} ${user['last name']}`, () => { | ||
cy.contains('td[data-cy=userId]', user['user id']) | ||
.parent('tr') | ||
.within(() => { | ||
cy.contains('td[data-cy=firstName]', user['first name']) | ||
cy.contains('td[data-cy=lastName]', user['last name']) | ||
}) | ||
}) | ||
}) | ||
``` | ||
|
||
![CSV spec](./images/csv-using-WebpackLoader.png) |
27 changes: 27 additions & 0 deletions
27
examples/fundamentals__dynamic-tests-from-csv-using-webpack/cypress.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable no-console */ | ||
const webpackPreprocessor = require('@cypress/webpack-preprocessor') | ||
const { defineConfig } = require('cypress') | ||
|
||
// This function is called when a project is opened or re-opened (e.g. due to | ||
// the project's config changing) | ||
module.exports = defineConfig({ | ||
e2e: { | ||
supportFile: false, | ||
setupNodeEvents (on, config) { | ||
// implement node event listeners here | ||
const webpackDefaults = webpackPreprocessor.defaultOptions | ||
|
||
webpackDefaults.webpackOptions.module.rules.push({ | ||
test: /\.csv$/, | ||
loader: 'csv-loader', | ||
options: { | ||
dynamicTyping: true, | ||
header: true, | ||
skipEmptyLines: true, | ||
}, | ||
}) | ||
|
||
on('file:preprocessor', webpackPreprocessor(webpackDefaults)) | ||
}, | ||
}, | ||
}) |
20 changes: 20 additions & 0 deletions
20
...tals__dynamic-tests-from-csv-using-webpack/cypress/e2e/csv-spec-using-WebpackLoader.cy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe('Users from CSV', () => { | ||
const csvUsers = require('../fixtures/users.csv') | ||
|
||
beforeEach(() => { | ||
cy.visit('index.html') | ||
}) | ||
|
||
csvUsers.forEach((user) => { | ||
it(`has the user ${user['first name']} ${user['last name']}`, () => { | ||
cy.contains('td[data-cy=userId]', user['user id']) | ||
.parent('tr') | ||
.within(() => { | ||
cy.contains('td[data-cy=firstName]', user['first name']) | ||
cy.contains('td[data-cy=lastName]', user['last name']) | ||
}) | ||
}) | ||
}) | ||
}) |
4 changes: 4 additions & 0 deletions
4
examples/fundamentals__dynamic-tests-from-csv-using-webpack/cypress/fixtures/users.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
user id,first name,last name | ||
101,Joe,Smith | ||
102,Mary,Jane | ||
103,Adam,Peterson |
Binary file added
BIN
+1.53 MB
...entals__dynamic-tests-from-csv-using-webpack/images/csv-using-WebpackLoader.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions
27
examples/fundamentals__dynamic-tests-from-csv-using-webpack/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<body> | ||
<h1>Users table</h1> | ||
<table> | ||
<tbody> | ||
<tr> | ||
<th>ID</th> | ||
<th>First name</th> | ||
<th>Last name</th> | ||
</tr> | ||
<tr> | ||
<td data-cy="userId">101</td> | ||
<td data-cy="firstName">Joe</td> | ||
<td data-cy="lastName">Smith</td> | ||
</tr> | ||
<tr> | ||
<td data-cy="userId">102</td> | ||
<td data-cy="firstName">Mary</td> | ||
<td data-cy="lastName">Jane</td> | ||
</tr> | ||
<tr> | ||
<td data-cy="userId">103</td> | ||
<td data-cy="firstName">Adam</td> | ||
<td data-cy="lastName">Peterson</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</body> |
16 changes: 16 additions & 0 deletions
16
examples/fundamentals__dynamic-tests-from-csv-using-webpack/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "dynamic-tests-from-csv", | ||
"version": "1.0.0", | ||
"description": "Create Cypress tests dynamically from CSV data", | ||
"private": true, | ||
"scripts": { | ||
"cypress:open": "../../node_modules/.bin/cypress open", | ||
"cypress:run": "../../node_modules/.bin/cypress run", | ||
"test:ci": "npm run cypress:run", | ||
"test:ci:record": "npm run cypress:run -- --record" | ||
}, | ||
"devDependencies": { | ||
"@cypress/webpack-preprocessor": "^5.17.1", | ||
"csv-loader": "^3.0.5" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filename in this code comment should be
csv-spec-using-WebpackLoader.cy.js
to match the actual implementation file shown in this example.Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.