Skip to content

Commit f47e408

Browse files
added capability to define Template (#247)
1 parent e5601b7 commit f47e408

File tree

9 files changed

+64
-7
lines changed

9 files changed

+64
-7
lines changed

CHANGELOG.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
1010
:pencil: - chore
1111
:microscope: - experimental
1212

13+
## [Unreleased]
14+
- :microscope: added capability to define `Template`
15+
1316
## 2.3.0
1417
- :microscope: added capability to define `Fixture`
1518
```typescript

README.MD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,16 @@ Feature: feature with fixture
154154
When I click 'qavajs T-shirt'
155155
And I click 'cart icon'
156156
Then I expect 'qavajs T-shirt cart item' to be visible
157+
```
158+
159+
### Template
160+
`Template` provides a way to define step definition using Gherkin language
161+
162+
```typescript
163+
import { When, Template } from '@qavajs/core';
164+
165+
When('I click {string} and verify {string}', Template((locator, expected) => `
166+
I click '${locator}'
167+
I expect '${locator} > Value' to equal '${expected}'
168+
`));
157169
```

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DataTable, IWorld, When } from '@cucumber/cucumber';
33
import { IConfiguration, IRunResult } from '@cucumber/cucumber/api';
44
export const Override: typeof When;
55
export { Fixture } from 'src/Fixture';
6+
export { Template } from 'src/Template';
67

78
/**
89
* Validation function

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const cucumber = require('@cucumber/cucumber');
22
const { Override } = require('./lib/Override');
33
const { Fixture } = require('./lib/Fixture');
4+
const { Template } = require('./lib/Template');
45

56
module.exports = {
67
...cucumber,
78
Override,
8-
Fixture
9+
Fixture,
10+
Template
911
}

index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { Override } from './lib/Override.js';
22
export { Fixture } from './lib/Fixture.js';
3+
export { Template } from './lib/Template.js';
34
export * from '@cucumber/cucumber';

src/Template.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function Template(scenario: (...args: any[]) => string) {
2+
return new Proxy(scenario, {
3+
apply: async function (template, world, args) {
4+
const scenario = template(...args) as string;
5+
const steps = scenario
6+
.split('\n')
7+
.map(step => step.trim())
8+
.filter(step => step);
9+
for (const step of steps) {
10+
await world.executeStep(step);
11+
}
12+
},
13+
})
14+
}

test-e2e/cjs/step_definitions/custom.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Override, Fixture, When, DataTable } = require('../../../index');
1+
const { Template, Override, Fixture, When, DataTable } = require('../../../index');
22
const { expect } = require('chai');
33

44
Fixture('testFixture', async function() {
@@ -79,4 +79,9 @@ When('write {string} to {value} value', async function(value, key) {
7979

8080
When('I expect {value} {validation} {value}', async function(value1, validate, value2) {
8181
validate(value1.value(), value2.value());
82-
});
82+
});
83+
84+
When('I click {string} and verify {string}', Template((locator, expected) => `
85+
I expect '${expected}' to equal '42'
86+
I expect '42' to equal '${expected}'
87+
`));

test-e2e/esm/step_definitions/custom.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DataTable, When, Override, Fixture } from '../../../index.mjs';
1+
import { DataTable, When, Override, Fixture, Template } from '../../../index.mjs';
22
import { expect } from 'chai';
33
import moduleESM from '../../modules/module.mjs';
44
import moduleCJS from '../../modules/module.cjs';
@@ -75,4 +75,9 @@ When('write {string} to {value} value', async function(value, key) {
7575

7676
When('I expect {value} {validation} {value}', async function(value1, validate, value2) {
7777
validate(value1.value(), value2.value());
78-
});
78+
});
79+
80+
When('I click {string} and verify {string}', Template((locator, expected) => `
81+
I expect '${expected}' to equal '42'
82+
I expect '42' to equal '${expected}'
83+
`));

test-e2e/ts/step_definitions/custom.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { DataTable, When, Override, Fixture, IQavajsWorld, Validation, MemoryValue } from '../../../index';
1+
import {
2+
DataTable,
3+
When,
4+
Override,
5+
Fixture,
6+
IQavajsWorld,
7+
Validation,
8+
MemoryValue,
9+
Template
10+
} from '../../../index';
211
import { expect } from 'chai';
312
//@ts-ignore
413
import moduleCJS from '../../modules/module.cjs';
@@ -77,4 +86,9 @@ When('write {string} to {value} value', async function(value: string, key: Memor
7786

7887
When('I expect {value} {validation} {value}', async function(value1: MemoryValue, validate: Validation, value2: MemoryValue) {
7988
validate(value1.value(), value2.value());
80-
});
89+
});
90+
91+
When('I click {string} and verify {string}', Template((locator: string, expected: string) => `
92+
I expect '${expected}' to equal '42'
93+
I expect '42' to equal '${expected}'
94+
`));

0 commit comments

Comments
 (0)