Skip to content

Commit 9c23624

Browse files
adrielcodecoCodeco
and
Codeco
authored
feat: adding faker.js support to gherkin (#2854)
* feat: adding faker.js support to gherkin * fix: rollback "Different cookies for different sessions" test * fix: fakerTransform docs Co-authored-by: Codeco <adriel.codeco@raizen.com>
1 parent 4024ea6 commit 9c23624

File tree

11 files changed

+242
-31
lines changed

11 files changed

+242
-31
lines changed

.circleci/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ defaults: &defaults
3131
command: docker-compose run --rm test-acceptance.protractor
3232
working_directory: test
3333
when: always
34+
- run:
35+
command: docker-compose run --rm test-bdd.faker
36+
working_directory: test
37+
when: always
3438

3539
jobs:
3640
docker:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ website
1111
docs/build
1212
test/data/output
1313
test/acceptance/output
14+
test/bdd/output
1415
examples/output
1516
examples/selenoid-example/output
1617
test/data/app/db

docs/plugins.md

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,49 @@ I.click('=sign-up'); // matches => [data-qa=sign-up]
478478
479479
- `config`
480480
481+
## fakerTransform
482+
483+
Use the [faker.js][4] package to generate fake data inside examples on your gherkin tests
484+
485+
![Faker.js][5]
486+
487+
#### Usage
488+
489+
To start please install `faker.js` package
490+
491+
npm install -D faker
492+
493+
yarn add -D faker
494+
495+
Add this plugin to config file:
496+
497+
```js
498+
plugins: {
499+
fakerTransform: {
500+
enabled: true
501+
}
502+
}
503+
```
504+
505+
Add the faker API using a mustache string format inside examples tables in your gherkin scenario outline
506+
507+
```feature
508+
Scenario Outline: ...
509+
Given ...
510+
When ...
511+
Then ...
512+
Examples:
513+
| productName | customer | email | anythingMore |
514+
| {{commerce.product}} | Dr. {{name.findName}} | {{internet.email}} | staticData |
515+
```
516+
517+
### Parameters
518+
519+
- `config`
520+
481521
## pauseOnFail
482522
483-
Automatically launches [interactive pause][4] when a test fails.
523+
Automatically launches [interactive pause][6] when a test fails.
484524
485525
Useful for debugging flaky tests on local environment.
486526
Add this plugin to config file:
@@ -527,9 +567,9 @@ Possible config options:
527567
528568
Links:
529569
530-
- [https://github.com/GoogleChrome/puppeteer/blob/v1.12.2/docs/api.md#class-coverage][5]
531-
- [https://github.com/istanbuljs/puppeteer-to-istanbul][6]
532-
- [https://github.com/gotwarlost/istanbul][7]
570+
- [https://github.com/GoogleChrome/puppeteer/blob/v1.12.2/docs/api.md#class-coverage][7]
571+
- [https://github.com/istanbuljs/puppeteer-to-istanbul][8]
572+
- [https://github.com/gotwarlost/istanbul][9]
533573
534574
### Parameters
535575
@@ -633,14 +673,14 @@ Possible config options:
633673
634674
## selenoid
635675
636-
[Selenoid][8] plugin automatically starts browsers and video recording.
676+
[Selenoid][10] plugin automatically starts browsers and video recording.
637677
Works with WebDriver helper.
638678
639679
### Prerequisite
640680
641681
This plugin **requires Docker** to be installed.
642682
643-
> If you have issues starting Selenoid with this plugin consider using the official [Configuration Manager][9] tool from Selenoid
683+
> If you have issues starting Selenoid with this plugin consider using the official [Configuration Manager][11] tool from Selenoid
644684
645685
### Usage
646686
@@ -669,7 +709,7 @@ plugins: {
669709
}
670710
```
671711
672-
When `autoCreate` is enabled it will pull the [latest Selenoid from DockerHub][10] and start Selenoid automatically.
712+
When `autoCreate` is enabled it will pull the [latest Selenoid from DockerHub][12] and start Selenoid automatically.
673713
It will also create `browsers.json` file required by Selenoid.
674714
675715
In automatic mode the latest version of browser will be used for tests. It is recommended to specify exact version of each browser inside `browsers.json` file.
@@ -681,10 +721,10 @@ In automatic mode the latest version of browser will be used for tests. It is re
681721
While this plugin can create containers for you for better control it is recommended to create and launch containers manually.
682722
This is especially useful for Continous Integration server as you can configure scaling for Selenoid containers.
683723
684-
> Use [Selenoid Configuration Manager][9] to create and start containers semi-automatically.
724+
> Use [Selenoid Configuration Manager][11] to create and start containers semi-automatically.
685725
686726
1. Create `browsers.json` file in the same directory `codecept.conf.js` is located
687-
[Refer to Selenoid documentation][11] to know more about browsers.json.
727+
[Refer to Selenoid documentation][13] to know more about browsers.json.
688728
689729
_Sample browsers.json_
690730
@@ -709,7 +749,7 @@ _Sample browsers.json_
709749
710750
2. Create Selenoid container
711751
712-
Run the following command to create a container. To know more [refer here][12]
752+
Run the following command to create a container. To know more [refer here][14]
713753
714754
```bash
715755
docker create \
@@ -742,15 +782,15 @@ When `allure` plugin is enabled a video is attached to report automatically.
742782
| enableVideo | Enable video recording and use `video` folder of output (default: false) |
743783
| enableLog | Enable log recording and use `logs` folder of output (default: false) |
744784
| deletePassed | Delete video and logs of passed tests (default : true) |
745-
| additionalParams | example: `additionalParams: '--env TEST=test'` [Refer here][13] to know more |
785+
| additionalParams | example: `additionalParams: '--env TEST=test'` [Refer here][15] to know more |
746786
747787
### Parameters
748788
749789
- `config`
750790
751791
## stepByStepReport
752792
753-
![step-by-step-report][14]
793+
![step-by-step-report][16]
754794
755795
Generates step by step report for a test.
756796
After each step in a test a screenshot is created. After test executed screenshots are combined into slideshow.
@@ -857,7 +897,7 @@ This plugin allows to run webdriverio services like:
857897
- browserstack
858898
- appium
859899
860-
A complete list of all available services can be found on [webdriverio website][15].
900+
A complete list of all available services can be found on [webdriverio website][17].
861901
862902
#### Setup
863903
@@ -869,7 +909,7 @@ See examples below:
869909
870910
#### Selenium Standalone Service
871911
872-
Install `@wdio/selenium-standalone-service` package, as [described here][16].
912+
Install `@wdio/selenium-standalone-service` package, as [described here][18].
873913
It is important to make sure it is compatible with current webdriverio version.
874914
875915
Enable `wdio` plugin in plugins list and add `selenium-standalone` service:
@@ -888,7 +928,7 @@ Please note, this service can be used with Protractor helper as well!
888928
889929
#### Sauce Service
890930
891-
Install `@wdio/sauce-service` package, as [described here][17].
931+
Install `@wdio/sauce-service` package, as [described here][19].
892932
It is important to make sure it is compatible with current webdriverio version.
893933
894934
Enable `wdio` plugin in plugins list and add `sauce` service:
@@ -924,30 +964,34 @@ In the same manner additional services from webdriverio can be installed, enable
924964
925965
[3]: https://codecept.io/locators#custom-locators
926966
927-
[4]: /basics/#pause
967+
[4]: https://www.npmjs.com/package/faker
968+
969+
[5]: https://raw.githubusercontent.com/Marak/faker.js/master/logo.png
970+
971+
[6]: /basics/#pause
928972
929-
[5]: https://github.com/GoogleChrome/puppeteer/blob/v1.12.2/docs/api.md#class-coverage
973+
[7]: https://github.com/GoogleChrome/puppeteer/blob/v1.12.2/docs/api.md#class-coverage
930974
931-
[6]: https://github.com/istanbuljs/puppeteer-to-istanbul
975+
[8]: https://github.com/istanbuljs/puppeteer-to-istanbul
932976
933-
[7]: https://github.com/gotwarlost/istanbul
977+
[9]: https://github.com/gotwarlost/istanbul
934978
935-
[8]: https://aerokube.com/selenoid/
979+
[10]: https://aerokube.com/selenoid/
936980
937-
[9]: https://aerokube.com/cm/latest/
981+
[11]: https://aerokube.com/cm/latest/
938982
939-
[10]: https://hub.docker.com/u/selenoid
983+
[12]: https://hub.docker.com/u/selenoid
940984
941-
[11]: https://aerokube.com/selenoid/latest/#_prepare_configuration
985+
[13]: https://aerokube.com/selenoid/latest/#_prepare_configuration
942986
943-
[12]: https://aerokube.com/selenoid/latest/#_option_2_start_selenoid_container
987+
[14]: https://aerokube.com/selenoid/latest/#_option_2_start_selenoid_container
944988
945-
[13]: https://docs.docker.com/engine/reference/commandline/create/
989+
[15]: https://docs.docker.com/engine/reference/commandline/create/
946990
947-
[14]: https://codecept.io/img/codeceptjs-slideshow.gif
991+
[16]: https://codecept.io/img/codeceptjs-slideshow.gif
948992
949-
[15]: https://webdriver.io
993+
[17]: https://webdriver.io
950994
951-
[16]: https://webdriver.io/docs/selenium-standalone-service.html
995+
[18]: https://webdriver.io/docs/selenium-standalone-service.html
952996
953-
[17]: https://webdriver.io/docs/sauce-service.html
997+
[19]: https://webdriver.io/docs/sauce-service.html

lib/interfaces/gherkin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const event = require('../event');
66
const scenario = require('../scenario');
77
const Step = require('../step');
88
const DataTableArgument = require('../data/dataTableArgument');
9+
const transform = require('../transform');
910

1011
const parser = new Parser();
1112
parser.stopAtFirstError = false;
@@ -81,7 +82,7 @@ module.exports = (text) => {
8182
const current = {};
8283
for (const index in example.cells) {
8384
const placeholder = fields[index];
84-
const value = example.cells[index].value;
85+
const value = transform('gherkin.examples', example.cells[index].value);
8586
current[placeholder] = value;
8687
exampleSteps = exampleSteps.map((step) => {
8788
step = { ...step };

lib/plugin/fakerTransform.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const faker = require('faker');
2+
const transform = require('../transform');
3+
4+
/**
5+
* Use the [faker.js](https://www.npmjs.com/package/faker) package to generate fake data inside examples on your gherkin tests
6+
*
7+
* ![Faker.js](https://raw.githubusercontent.com/Marak/faker.js/master/logo.png)
8+
*
9+
* #### Usage
10+
*
11+
* To start please install `faker.js` package
12+
*
13+
* ```
14+
* npm install -D faker
15+
* ```
16+
*
17+
* ```
18+
* yarn add -D faker
19+
* ```
20+
*
21+
* Add this plugin to config file:
22+
*
23+
* ```js
24+
* plugins: {
25+
* fakerTransform: {
26+
* enabled: true
27+
* }
28+
* }
29+
* ```
30+
*
31+
* Add the faker API using a mustache string format inside examples tables in your gherkin scenario outline
32+
*
33+
* ```feature
34+
* Scenario Outline: ...
35+
* Given ...
36+
* When ...
37+
* Then ...
38+
* Examples:
39+
* | productName | customer | email | anythingMore |
40+
* | {{commerce.product}} | Dr. {{name.findName}} | {{internet.email}} | staticData |
41+
* ```
42+
*
43+
*/
44+
module.exports = function (config) {
45+
transform.addTransformerBeforeAll('gherkin.examples', (value) => {
46+
if (typeof value === 'string' && value.length > 0) {
47+
return faker.fake(value);
48+
}
49+
return value;
50+
});
51+
};

lib/transform.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const transformers = {
2+
'gherkin.examples': [],
3+
};
4+
5+
function transform(target, value) {
6+
if (target in transformers) {
7+
for (const transform of transformers[target]) {
8+
value = transform(value);
9+
}
10+
}
11+
return value;
12+
}
13+
14+
transform.addTransformer = function (target, transformer) {
15+
if (target in transformers) {
16+
transformers[target].push(transformer);
17+
}
18+
};
19+
20+
transform.addTransformerBeforeAll = function (target, transformer) {
21+
if (target in transformers) {
22+
transformers[target].unshift(transformer);
23+
}
24+
};
25+
26+
module.exports = transform;

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"eslint-plugin-mocha": "^6.3.0",
111111
"expect": "^26.6.2",
112112
"express": "^4.17.1",
113-
"faker": "^4.1.0",
113+
"faker": "^5.5.1",
114114
"form-data": "^3.0.0",
115115
"graphql": "^14.6.0",
116116
"husky": "^4.3.5",
@@ -142,6 +142,9 @@
142142
"node": ">=8.9.1",
143143
"npm": ">=5.6.0"
144144
},
145+
"peerDependencies": {
146+
"faker": "^5.5.1"
147+
},
145148
"es6": true,
146149
"husky": {
147150
"hooks": {

test/bdd/codecept.faker.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const TestHelper = require('../support/TestHelper');
2+
3+
module.exports.config = {
4+
timeout: 10000,
5+
output: './output',
6+
helpers: {
7+
Puppeteer: {
8+
url: TestHelper.siteUrl(),
9+
show: false,
10+
chrome: {
11+
args: [
12+
'--no-sandbox',
13+
'--disable-setuid-sandbox',
14+
],
15+
},
16+
},
17+
ScreenshotSessionHelper: {
18+
require: '../support/ScreenshotSessionHelper.js',
19+
outputPath: './output',
20+
},
21+
},
22+
include: {},
23+
bootstrap: false,
24+
mocha: {},
25+
plugins: {
26+
screenshotOnFail: {
27+
enabled: true,
28+
},
29+
fakerTransform: {
30+
enabled: true,
31+
},
32+
},
33+
name: 'bdd',
34+
gherkin: {
35+
features: './features/faker.feature',
36+
steps: ['./defs/faker.js'],
37+
},
38+
};

0 commit comments

Comments
 (0)