1- import { DataTable , Given , When } from '@cucumber/cucumber' ;
1+ import { DataTable , Given , Then , When } from '@cucumber/cucumber' ;
22
33import { CreateRopeRecord } from '@/pages/CreateRopeRecord' ;
44import { HoistWorld , IHoistWorld } from '@/support/hoist-world' ;
@@ -12,47 +12,52 @@ Given('I add a new rope', async function (this: IHoistWorld) {
1212} ) ;
1313
1414Given (
15- 'I provide the following rope information - greg' ,
16- async function ( this : IHoistWorld , dataTable : DataTable ) : Promise < void > {
17- const page = this . page ! ;
18- const uniqueGenerator = new UniqueIdentifierGenerator ( ) ;
19- const ropeInfo = dataTable . rowsHash ( ) ;
20- const ropeRecord = new CreateRopeRecord ( page ) ; // Create an instance of CreateRopeRecord
21-
22- for ( const [ fieldName , value ] of Object . entries ( ropeInfo ) ) {
23- let cleanValue = value ;
24-
25- // Handle specific fields like Serial number
26- if ( fieldName === 'Serial number' ) {
27- cleanValue = uniqueGenerator . generateUniqueValue ( 'CUCSNO' , 6 ) ; // Generate unique serial number
28- this . generatedSerialNumber = cleanValue ;
29- HoistWorld . sharedState . generatedSerialNumber = cleanValue ;
30- }
31-
32- if ( ! ropeRecord . ropeMetadata ) {
33- throw new Error ( `No metadata found for field: ${ fieldName } ` ) ;
34- }
35-
36- // Pass metadata wrapped in an array
37- await ropeRecord . setFieldValue ( fieldName , cleanValue , ropeRecord . ropeMetadata ) ;
15+ 'I provide the following rope information with {string} serial number' ,
16+ async function (
17+ this : IHoistWorld ,
18+ serialNumberType : string ,
19+ dataTable : DataTable ,
20+ ) : Promise < void > {
21+ const ropeInfo = dataTable . rowsHash ( ) ; // Convert Gherkin table to object
22+ const ropeRecord = new CreateRopeRecord ( this . page ! ) ;
23+
24+ // Generating unique serial and caching it
25+ if ( serialNumberType === 'Unique' ) {
26+ const uniqueSerialNumber = new UniqueIdentifierGenerator ( ) . generateUniqueValue ( 'CUCSNO' , 6 ) ;
27+ ropeInfo [ 'Serial number' ] = uniqueSerialNumber ;
28+ this . generatedSerialNumber = uniqueSerialNumber ;
29+ HoistWorld . sharedState . generatedSerialNumber = uniqueSerialNumber ;
3830 }
3931
32+ // Caching ropeInfo
33+ HoistWorld . sharedState . ropeInfo = ropeInfo ;
34+
35+ await ropeRecord . fillRopeFields ( ropeInfo ) ;
4036 this . ropeRecord = ropeRecord ;
41- await executeWithDelay ( ) ;
37+
38+ await executeWithDelay ( ) ; // Add delay to simulate real-world conditions
4239 } ,
4340) ;
4441
45- // Define the sleep function
46- async function sleep ( ms : number ) : Promise < void > {
47- return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
48- }
42+ Given (
43+ 'I reuse the same serial number and rope information from the previous rope' ,
44+ async function ( this : IHoistWorld ) {
45+ if ( ! this . generatedSerialNumber ) {
46+ throw new Error ( 'No previously generated serial number found to reuse.' ) ;
47+ }
48+ const ropeRecord = new CreateRopeRecord ( this . page ! ) ;
4949
50- // Usage of the sleep function
51- async function executeWithDelay ( ) {
52- console . log ( 'Taking a break' ) ;
53- await sleep ( 5000 ) ; // Pause for 5 seconds
54- console . log ( 'Done' ) ;
55- }
50+ // Reuse the previous ropeInfo
51+ const ropeInfo = HoistWorld . sharedState . ropeInfo ?? { } ;
52+
53+ // Reuse the previous serial number
54+ ropeInfo [ 'Serial number' ] = this . generatedSerialNumber ;
55+
56+ // Fill the form with the cached ropeInfo
57+ await ropeRecord . fillRopeFields ( ropeInfo ) ;
58+ this . ropeRecord = ropeRecord ;
59+ } ,
60+ ) ;
5661
5762When ( 'I click on Save' , async function ( this : IHoistWorld ) {
5863 const timestamp = new Date ( ) . toISOString ( ) . replace ( / [: .] / g, '-' ) ;
@@ -67,3 +72,27 @@ When('I click on Save', async function (this: IHoistWorld) {
6772
6873 await this . page ?. getByRole ( 'button' , { name : 'Save' } ) . click ( ) ;
6974} ) ;
75+
76+ Then (
77+ 'I should get a duplicate serial number error as {string}' ,
78+ async function ( this : IHoistWorld , errorMessage : string ) {
79+ const page = this . page ! ;
80+ await page
81+ . locator ( 'div' )
82+ . filter ( { hasText : errorMessage } )
83+ . nth ( 1 )
84+ . waitFor ( { state : 'visible' , timeout : 180000 } ) ;
85+ } ,
86+ ) ;
87+
88+ // Define the sleep function
89+ async function sleep ( ms : number ) : Promise < void > {
90+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
91+ }
92+
93+ // Usage of the sleep function
94+ async function executeWithDelay ( ) {
95+ console . log ( 'Taking a break' ) ;
96+ await sleep ( 5000 ) ; // Pause for 5 seconds
97+ console . log ( 'Done' ) ;
98+ }
0 commit comments