@@ -5,134 +5,196 @@ sidebar_position: 1
5
5
import Tabs from ' @theme/Tabs' ;
6
6
import TabItem from ' @theme/TabItem' ;
7
7
8
- # Memory
9
- Framework provides the capability to transit variables between step and access them from gherkin definitions
8
+ # Test Data Management
9
+
10
+ The ` @qavajs/memory ` module provides powerful capabilities to store, retrieve, and manipulate variables between test steps. This allows you to create more dynamic and data-driven test scenarios.
11
+
12
+ ## Basic Usage
13
+
14
+ Memory allows you to save values during test execution and reference them later:
10
15
11
16
``` gherkin
12
- When I save text of 'Answer' to equal 'answer '
13
- Then I expect text of 'Another Answer' to equal '$answer '
17
+ When I save text of 'Answer' as 'userAnswer '
18
+ Then I expect text of 'Another Answer' to equal '$userAnswer '
14
19
```
15
20
16
- ### Memory value parameter type
17
- ` value ` parameter type provides API to access memory
21
+ ## Memory API
22
+
23
+ ### Using the Memory Value Parameter Type
24
+ All built-in steps can consume memory params.
25
+
26
+ ``` gherkin
27
+ Then I expect text of 'Title' to equal '$userAnswer'
28
+ ```
29
+
30
+ The ` value ` parameter type provides a strongly-typed API to access memory and can be used in custom step:
18
31
19
32
``` typescript
33
+ // Reading from memory
20
34
When (' Read memory {value}' , async function (memoryValue ) {
21
- expect (memoryValue .value ()).to .equal (' ts' );
35
+ // memoryValue.value() retrieves the actual value from memory
36
+ expect (memoryValue .value ()).to .equal (' stored value' );
22
37
});
23
38
39
+ // Writing to memory
24
40
When (' Set memory {value} as {string}' , async function (memoryKey , value ) {
41
+ // memoryKey.set() stores a value in memory
25
42
memoryKey .set (value );
26
43
});
27
44
```
28
45
29
- ### Custom steps
30
- Memory value can be set and read from memory object
46
+ ### Direct Memory Access in Custom Steps
47
+
48
+ You can also access the memory object directly in your step definitions:
31
49
32
50
<Tabs >
33
51
<TabItem value = " js" label = " JavaScript" default >
34
52
``` javascript
35
53
const memory = require (' @qavajs/memory' );
36
54
37
- When (/ ^ save variable as '(. + )'$ / , async function (key ) {
38
- memory .setValue (key, 42 );
55
+ When (/ ^ I save value '( . + )' as '(. + )'$ / , async function (value , key ) {
56
+ memory .setValue (key, value );
39
57
});
40
58
41
- Then (/ ^ value '(. + )' should be equal to '(. + )'$ / , async function (variable1 , variable2 ) {
42
- const val = memory .getValue (variable1 );
43
- expect (val ).to .equal (variable2 );
59
+ Then (/ ^ value in '(. + )' should equal '(. + )'$ / , async function (variable , expectedValue ) {
60
+ const actualValue = memory .getValue (variable );
61
+ expect (actualValue ).to .equal (expectedValue );
44
62
});
45
63
```
46
64
</TabItem >
47
65
<TabItem value = " ts" label = " TypeScript" >
48
66
``` typescript
49
- import memory = from '@qavajs /memory ';
67
+ import memory from ' @qavajs/memory' ;
50
68
51
- When (/ ^ save variable as '(. + )'$ / , async function (key : string ) {
52
- memory .setValue (key , 42 );
69
+ When (/ ^ I save value '( . + )' as '(. + )'$ / , async function (value : string , key : string ) {
70
+ memory .setValue (key , value );
53
71
});
54
72
55
- Then (/ ^ value '(. + )' should be equal to '(. + )'$ / , async function (variable1 : string , variable2 : string ) {
56
- const val = memory .getValue (variable1 );
57
- expect (val ).to .equal (variable2 );
73
+ Then (/ ^ value in '(. + )' should equal '(. + )'$ / , async function (variable : string , expectedValue : string ) {
74
+ const actualValue = memory .getValue (variable );
75
+ expect (actualValue ).to .equal (expectedValue );
58
76
});
59
77
```
60
78
</TabItem >
61
79
</Tabs >
62
80
81
+ In your Gherkin scenarios, you can access previously saved variables using the ` $ ` prefix:
82
+
63
83
``` gherkin
64
- When save variable as 'variable '
65
- # previously saved variable can be accessed via $identifier
66
- Then value of '$variable' should be equal to '42 '
84
+ When I save value '42' as 'myNumber '
85
+ Then value in 'myNumber' should equal '42'
86
+ And I expect text of 'Result Field' to equal '$myNumber '
67
87
```
68
88
69
- ### Constants and computed
89
+ ## Advanced Features
70
90
71
- Lib provides capability to set constant values and computed (JavaScript function references that can be called from feature file)
91
+ ### Constants and Computed Values
92
+
93
+ You can define constant values and computed functions that can be referenced in your feature files:
72
94
73
95
<Tabs >
74
96
<TabItem value = " js" label = " JavaScript" default >
75
97
``` javascript
98
+ // memory.js
76
99
module .exports = {
77
- constant: 42 ,
78
- computed : function () {
79
- return Date .now ()
100
+ // Constants
101
+ maxRetries: 3 ,
102
+ baseUrl: ' https://example.com' ,
103
+
104
+ // Computed values (functions that return dynamic values)
105
+ timestamp : function () {
106
+ return Date .now ();
107
+ },
108
+ randomUsername : function () {
109
+ return ' user_' + Math .floor (Math .random () * 10000 );
80
110
}
81
111
};
82
112
```
83
113
</TabItem >
84
114
<TabItem value = " ts" label = " TypeScript" >
85
115
``` typescript
116
+ // memory.ts
86
117
export default {
87
- constant: 42 ,
88
- computed : function () {
89
- return Date .now ()
118
+ // Constants
119
+ maxRetries: 3 ,
120
+ baseUrl: ' https://example.com' ,
121
+
122
+ // Computed values (functions that return dynamic values)
123
+ timestamp : function () {
124
+ return Date .now ();
125
+ },
126
+ randomUsername : function () {
127
+ return ' user_' + Math .floor (Math .random () * 10000 );
90
128
}
91
129
};
92
130
```
93
131
</TabItem >
94
132
</Tabs >
95
133
134
+ Reference these values in your Gherkin scenarios:
135
+
96
136
``` gherkin
97
- Then I expect text of 'Answer' to equal '$constant'
98
- Then I expect text of 'What Time Is It' to equal '$computed()'
137
+ # Using constants
138
+ Then I expect retry count to be '$maxRetries'
139
+ When I navigate to '$baseUrl/login'
140
+
141
+ # Using computed values
142
+ Then I expect registration timestamp to be '$timestamp()'
143
+ When I register with username '$randomUsername()'
99
144
```
100
145
101
- ### String interpolation
102
- Module also provides capability to use string interpolation in your Gherkin scenarios
146
+ ### String Interpolation
147
+
148
+ Use curly braces to interpolate memory values within strings:
149
+
103
150
``` gherkin
104
- When I save '42' to memory as 'variable '
105
- Then I expect text of 'Answer ' to equal 'answer is {$variable}' #expected value will be 'answer is 42 '
151
+ When I save '42' as 'score '
152
+ Then I expect text of 'Result ' to equal 'Your score is {$score} points '
106
153
```
107
154
108
- ### $js
109
- Built-in ` $js ` computed provides a way to execute JavaScript code.
155
+ ### Built-in JavaScript Execution with $js
156
+
157
+ Use the built-in ` $js ` computed function to execute JavaScript code directly from your Gherkin scenarios:
110
158
111
159
``` gherkin
112
- When I expect text of 'Current Date' to equal '$js(Date.now())'
160
+ # Math operations
161
+ When I expect the result to equal '$js(42 * 2.5)'
162
+
163
+ # String manipulation
164
+ Then I expect the username to be '$js("user_" + Math.floor(Math.random() * 1000))'
165
+
166
+ # Date formatting
167
+ And I expect the date format to be '$js(new Date().toLocaleDateString())'
113
168
```
114
169
115
- ### Escape $
116
- ` $ ` can be escaped with double backslash
170
+ ### Escaping the $ Symbol
171
+
172
+ When you need to use a literal ` $ ` character in your tests, escape it with a double backslash:
117
173
118
- ``` Gherkin
119
- When I expect text of 'Currency Label' to equal '\\$42'
174
+ ``` gherkin
175
+ When I expect the price to be '\\$42.99'
176
+ # This will expect the actual text to be "$42.99"
120
177
```
121
178
122
- ### Parallel
123
- In case you need to assign uniq value for each Cucumber thread you can use parallel function.
124
- It will assign value based on ` CUCUMBER_WORKER_ID ` env variable.
179
+ ### Parallel Execution Support
180
+
181
+ For parallel test execution, you can assign unique values for each Cucumber thread using the ` parallel ` function:
125
182
126
183
<Tabs >
127
184
<TabItem value = " js" label = " JavaScript" default >
128
185
``` javascript
129
186
const { parallel } = require (' @qavajs/memory/utils' );
130
187
131
188
class Memory {
132
- user = parallel ([
133
- { username: ' user1' , password: ' password' },
134
- { username: ' user2' , password: ' password' }
189
+ // Each parallel thread will get a different user from this array
190
+ testUser = parallel ([
191
+ { username: ' user1@example.com' , password: ' password1' },
192
+ { username: ' user2@example.com' , password: ' password2' },
193
+ { username: ' user3@example.com' , password: ' password3' }
135
194
]);
195
+
196
+ // You can also use parallel with primitive values
197
+ port = parallel ([8080 , 8081 , 8082 ]);
136
198
}
137
199
138
200
module .exports = Memory;
@@ -143,12 +205,31 @@ It will assign value based on `CUCUMBER_WORKER_ID` env variable.
143
205
import { parallel } from ' @qavajs/memory/utils' ;
144
206
145
207
export class Memory {
146
- user = parallel ([
147
- { username: ' user1' , password: ' password' },
148
- { username: ' user2' , password: ' password' }
208
+ // Each parallel thread will get a different user from this array
209
+ testUser = parallel ([
210
+ { username: ' user1@example.com' , password: ' password1' },
211
+ { username: ' user2@example.com' , password: ' password2' },
212
+ { username: ' user3@example.com' , password: ' password3' }
149
213
]);
214
+
215
+ // You can also use parallel with primitive values
216
+ port = parallel ([8080 , 8081 , 8082 ]);
150
217
}
151
218
```
152
219
</TabItem >
153
220
</Tabs >
154
221
222
+ Use these values in your scenarios:
223
+
224
+ ``` gherkin
225
+ When I login with username '$testUser.username' and password '$testUser.password'
226
+ And I connect to port '$port'
227
+ ```
228
+
229
+ ## Best Practices
230
+
231
+ 1 . ** Use descriptive names** for your memory variables to make scenarios more readable
232
+ 2 . ** Prefer string interpolation** for complex strings rather than concatenating multiple parts
233
+ 3 . ** Create reusable computed functions** for common operations or data generation
234
+ 4 . ** Use TypeScript interfaces** to define the structure of complex memory objects
235
+ 5 . ** Document memory constants and functions** for better team collaboration
0 commit comments